Skip to content

Commit 2f2a17a

Browse files
authored
Removing some unnecessary methods from host and function parameters (microsoft#51499)
1 parent 4d53a1f commit 2f2a17a

File tree

10 files changed

+71
-119
lines changed

10 files changed

+71
-119
lines changed

src/compiler/builder.ts

+37-64
Large diffs are not rendered by default.

src/compiler/builderPublic.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import {
88
export type AffectedFileResult<T> = { result: T; affected: SourceFile | Program; } | undefined;
99

1010
export interface BuilderProgramHost {
11-
/**
12-
* return true if file names are treated with case sensitivity
13-
*/
14-
useCaseSensitiveFileNames(): boolean;
1511
/**
1612
* If provided this would be used this hash instead of actual file shape text for detecting changes
1713
*/
@@ -33,14 +29,11 @@ export interface BuilderProgramHost {
3329
* @internal
3430
*/
3531
storeFilesChangingSignatureDuringEmit?: boolean;
36-
/**
37-
* Gets the current time
38-
*
39-
* @internal
40-
*/
41-
now?(): Date;
4232
}
4333

34+
/** @internal */
35+
export type HostForComputeHash = Pick<BuilderProgramHost, "createHash">;
36+
4437
/**
4538
* Builder to manage the program state changes
4639
*/

src/compiler/builderState.ts

+15-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
2-
arrayFrom, CancellationToken, computeSignatureWithDiagnostics, CustomTransformers, Debug, EmitOutput, emptyArray,
2+
arrayFrom, CancellationToken, computeSignatureWithDiagnostics, createGetCanonicalFileName, CustomTransformers, Debug, EmitOutput, emptyArray,
33
ExportedModulesFromDeclarationEmit, GetCanonicalFileName, getDirectoryPath, getSourceFileOfNode,
4+
HostForComputeHash,
45
isDeclarationFileName, isExternalOrCommonJsModule, isGlobalScopeAugmentation, isJsonSourceFile,
56
isModuleWithStringLiteralName, isStringLiteral, mapDefined, mapDefinedIterator, ModuleDeclaration,
67
ModuleKind, outFile, OutputFile, Path, Program, ResolutionMode, some, SourceFile, StringLiteralLike, Symbol,
@@ -158,11 +159,6 @@ export namespace BuilderState {
158159
return false;
159160
}
160161

161-
/**
162-
* Compute the hash to store the shape of the file
163-
*/
164-
export type ComputeHash = ((data: string) => string) | undefined;
165-
166162
function getReferencedFilesFromImportedModuleSymbol(symbol: Symbol): Path[] {
167163
return mapDefined(symbol.declarations, declaration => getSourceFileOfNode(declaration)?.resolvedPath);
168164
}
@@ -272,14 +268,15 @@ export namespace BuilderState {
272268
/**
273269
* Creates the state of file references and signature for the new program from oldState if it is safe
274270
*/
275-
export function create(newProgram: Program, getCanonicalFileName: GetCanonicalFileName, oldState?: Readonly<BuilderState>, disableUseFileVersionAsSignature?: boolean): BuilderState {
271+
export function create(newProgram: Program, oldState?: Readonly<BuilderState>, disableUseFileVersionAsSignature?: boolean): BuilderState {
276272
const fileInfos = new Map<Path, FileInfo>();
277273
const options = newProgram.getCompilerOptions();
278274
const isOutFile = outFile(options);
279275
const referencedMap = options.module !== ModuleKind.None && !isOutFile ?
280276
createManyToManyPathMap() : undefined;
281277
const exportedModulesMap = referencedMap ? createManyToManyPathMap() : undefined;
282278
const useOldState = canReuseOldState(referencedMap, oldState);
279+
const getCanonicalFileName = createGetCanonicalFileName(newProgram.useCaseSensitiveFileNames());
283280

284281
// Ensure source files have parent pointers set
285282
newProgram.getTypeChecker();
@@ -340,16 +337,14 @@ export namespace BuilderState {
340337
programOfThisState: Program,
341338
path: Path,
342339
cancellationToken: CancellationToken | undefined,
343-
computeHash: ComputeHash,
344-
getCanonicalFileName: GetCanonicalFileName,
340+
host: HostForComputeHash,
345341
): readonly SourceFile[] {
346342
const result = getFilesAffectedByWithOldState(
347343
state,
348344
programOfThisState,
349345
path,
350346
cancellationToken,
351-
computeHash,
352-
getCanonicalFileName,
347+
host,
353348
);
354349
state.oldSignatures?.clear();
355350
state.oldExportedModulesMap?.clear();
@@ -361,19 +356,18 @@ export namespace BuilderState {
361356
programOfThisState: Program,
362357
path: Path,
363358
cancellationToken: CancellationToken | undefined,
364-
computeHash: ComputeHash,
365-
getCanonicalFileName: GetCanonicalFileName,
359+
host: HostForComputeHash,
366360
): readonly SourceFile[] {
367361
const sourceFile = programOfThisState.getSourceFileByPath(path);
368362
if (!sourceFile) {
369363
return emptyArray;
370364
}
371365

372-
if (!updateShapeSignature(state, programOfThisState, sourceFile, cancellationToken, computeHash, getCanonicalFileName)) {
366+
if (!updateShapeSignature(state, programOfThisState, sourceFile, cancellationToken, host)) {
373367
return [sourceFile];
374368
}
375369

376-
return (state.referencedMap ? getFilesAffectedByUpdatedShapeWhenModuleEmit : getFilesAffectedByUpdatedShapeWhenNonModuleEmit)(state, programOfThisState, sourceFile, cancellationToken, computeHash, getCanonicalFileName);
370+
return (state.referencedMap ? getFilesAffectedByUpdatedShapeWhenModuleEmit : getFilesAffectedByUpdatedShapeWhenNonModuleEmit)(state, programOfThisState, sourceFile, cancellationToken, host);
377371
}
378372

379373
export function updateSignatureOfFile(state: BuilderState, signature: string | undefined, path: Path) {
@@ -389,9 +383,8 @@ export namespace BuilderState {
389383
programOfThisState: Program,
390384
sourceFile: SourceFile,
391385
cancellationToken: CancellationToken | undefined,
392-
computeHash: ComputeHash,
393-
getCanonicalFileName: GetCanonicalFileName,
394-
useFileVersionAsSignature = state.useFileVersionAsSignature
386+
host: HostForComputeHash,
387+
useFileVersionAsSignature = state.useFileVersionAsSignature,
395388
) {
396389
// If we have cached the result for this file, that means hence forth we should assume file shape is uptodate
397390
if (state.hasCalledUpdateShapeSignature?.has(sourceFile.resolvedPath)) return false;
@@ -405,10 +398,10 @@ export namespace BuilderState {
405398
(fileName, text, _writeByteOrderMark, _onError, sourceFiles, data) => {
406399
Debug.assert(isDeclarationFileName(fileName), `File extension for signature expected to be dts: Got:: ${fileName}`);
407400
latestSignature = computeSignatureWithDiagnostics(
401+
programOfThisState,
408402
sourceFile,
409403
text,
410-
computeHash,
411-
getCanonicalFileName,
404+
host,
412405
data,
413406
);
414407
if (latestSignature !== prevSignature) {
@@ -604,8 +597,7 @@ export namespace BuilderState {
604597
programOfThisState: Program,
605598
sourceFileWithUpdatedShape: SourceFile,
606599
cancellationToken: CancellationToken | undefined,
607-
computeHash: ComputeHash,
608-
getCanonicalFileName: GetCanonicalFileName,
600+
host: HostForComputeHash,
609601
) {
610602
if (isFileAffectingGlobalScope(sourceFileWithUpdatedShape)) {
611603
return getAllFilesExcludingDefaultLibraryFile(state, programOfThisState, sourceFileWithUpdatedShape);
@@ -629,7 +621,7 @@ export namespace BuilderState {
629621
if (!seenFileNamesMap.has(currentPath)) {
630622
const currentSourceFile = programOfThisState.getSourceFileByPath(currentPath)!;
631623
seenFileNamesMap.set(currentPath, currentSourceFile);
632-
if (currentSourceFile && updateShapeSignature(state, programOfThisState, currentSourceFile, cancellationToken, computeHash, getCanonicalFileName)) {
624+
if (currentSourceFile && updateShapeSignature(state, programOfThisState, currentSourceFile, cancellationToken, host)) {
633625
queue.push(...getReferencedByPaths(state, currentSourceFile.resolvedPath));
634626
}
635627
}

src/compiler/emitter.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
637637
if (sourceMapFilePath) {
638638
const sourceMap = sourceMapGenerator.toString();
639639
writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap, /*writeByteOrderMark*/ false, sourceFiles);
640-
if (printer.bundleFileInfo) printer.bundleFileInfo.mapHash = computeSignature(sourceMap, maybeBind(host, host.createHash));
640+
if (printer.bundleFileInfo) printer.bundleFileInfo.mapHash = computeSignature(sourceMap, host);
641641
}
642642
}
643643
else {
@@ -649,7 +649,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
649649
writeFile(host, emitterDiagnostics, jsFilePath, text, !!compilerOptions.emitBOM, sourceFiles, { sourceMapUrlPos, diagnostics: transform.diagnostics });
650650
// We store the hash of the text written in the buildinfo to ensure that text of the referenced d.ts file is same as whats in the buildinfo
651651
// This is needed because incremental can be toggled between two runs and we might use stale file text to do text manipulation in prepend mode
652-
if (printer.bundleFileInfo) printer.bundleFileInfo.hash = computeSignature(text, maybeBind(host, host.createHash));
652+
if (printer.bundleFileInfo) printer.bundleFileInfo.hash = computeSignature(text, host);
653653

654654
// Reset state
655655
writer.clear();
@@ -846,7 +846,6 @@ function emitUsingBuildInfoWorker(
846846
getCommandLine: (ref: ProjectReference) => ParsedCommandLine | undefined,
847847
customTransformers?: CustomTransformers
848848
): EmitUsingBuildInfoResult {
849-
const createHash = maybeBind(host, host.createHash);
850849
const { buildInfoPath, jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath } = getOutputPathsForBundle(config.options, /*forceDtsPaths*/ false);
851850
// If host directly provides buildinfo we can get it directly. This allows host to cache the buildinfo
852851
const buildInfo = host.getBuildInfo!(buildInfoPath!, config.options.configFilePath);
@@ -856,20 +855,20 @@ function emitUsingBuildInfoWorker(
856855
const jsFileText = host.readFile(Debug.checkDefined(jsFilePath));
857856
if (!jsFileText) return jsFilePath!;
858857
// If the jsFileText is not same has what it was created with, tsbuildinfo is stale so dont use it
859-
if (computeSignature(jsFileText, createHash) !== buildInfo.bundle.js.hash) return jsFilePath!;
858+
if (computeSignature(jsFileText, host) !== buildInfo.bundle.js.hash) return jsFilePath!;
860859
const sourceMapText = sourceMapFilePath && host.readFile(sourceMapFilePath);
861860
// error if no source map or for now if inline sourcemap
862861
if ((sourceMapFilePath && !sourceMapText) || config.options.inlineSourceMap) return sourceMapFilePath || "inline sourcemap decoding";
863-
if (sourceMapFilePath && computeSignature(sourceMapText!, createHash) !== buildInfo.bundle.js.mapHash) return sourceMapFilePath;
862+
if (sourceMapFilePath && computeSignature(sourceMapText!, host) !== buildInfo.bundle.js.mapHash) return sourceMapFilePath;
864863

865864
// read declaration text
866865
const declarationText = declarationFilePath && host.readFile(declarationFilePath);
867866
if (declarationFilePath && !declarationText) return declarationFilePath;
868-
if (declarationFilePath && computeSignature(declarationText!, createHash) !== buildInfo.bundle.dts!.hash) return declarationFilePath;
867+
if (declarationFilePath && computeSignature(declarationText!, host) !== buildInfo.bundle.dts!.hash) return declarationFilePath;
869868
const declarationMapText = declarationMapPath && host.readFile(declarationMapPath);
870869
// error if no source map or for now if inline sourcemap
871870
if ((declarationMapPath && !declarationMapText) || config.options.inlineSourceMap) return declarationMapPath || "inline sourcemap decoding";
872-
if (declarationMapPath && computeSignature(declarationMapText!, createHash) !== buildInfo.bundle.dts!.mapHash) return declarationMapPath;
871+
if (declarationMapPath && computeSignature(declarationMapText!, host) !== buildInfo.bundle.dts!.mapHash) return declarationMapPath;
873872

874873
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(buildInfoPath!, host.getCurrentDirectory()));
875874
const ownPrependInput = createInputFilesWithFileTexts(
@@ -936,7 +935,7 @@ function emitUsingBuildInfoWorker(
936935
const program = buildInfo.program;
937936
if (program && changedDtsText !== undefined && config.options.composite) {
938937
// Update the output signature
939-
(program as ProgramBundleEmitBuildInfo).outSignature = computeSignature(changedDtsText, createHash, changedDtsData);
938+
(program as ProgramBundleEmitBuildInfo).outSignature = computeSignature(changedDtsText, host, changedDtsData);
940939
}
941940
// Update sourceFileInfo
942941
const { js, dts, sourceFiles } = buildInfo.bundle!;
@@ -950,7 +949,7 @@ function emitUsingBuildInfoWorker(
950949
getSourceFileFromReference: returnUndefined,
951950
redirectTargetsMap: createMultiMap(),
952951
getFileIncludeReasons: notImplemented,
953-
createHash,
952+
createHash: maybeBind(host, host.createHash),
954953
};
955954
emitFiles(
956955
notImplementedResolver,

src/server/project.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
332332
autoImportProviderHost: AutoImportProviderProject | false | undefined;
333333
/** @internal */
334334
protected typeAcquisition: TypeAcquisition | undefined;
335+
/** @internal */
336+
createHash = maybeBind(this.projectService.host, this.projectService.host.createHash);
335337

336338
/** @internal */
337339
constructor(
@@ -740,15 +742,14 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
740742
return [];
741743
}
742744
updateProjectIfDirty(this);
743-
this.builderState = BuilderState.create(this.program!, this.projectService.toCanonicalFileName, this.builderState, /*disableUseFileVersionAsSignature*/ true);
745+
this.builderState = BuilderState.create(this.program!, this.builderState, /*disableUseFileVersionAsSignature*/ true);
744746
return mapDefined(
745747
BuilderState.getFilesAffectedBy(
746748
this.builderState,
747749
this.program!,
748750
scriptInfo.path,
749751
this.cancellationToken,
750-
maybeBind(this.projectService.host, this.projectService.host.createHash),
751-
this.getCanonicalFileName,
752+
this.projectService.host,
752753
),
753754
sourceFile => this.shouldEmitFile(this.projectService.getScriptInfoForPath(sourceFile.path)) ? sourceFile.fileName : undefined
754755
);

src/services/services.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,7 @@ export function createLanguageService(
13981398
trace: maybeBind(host, host.trace),
13991399
resolveModuleNames: maybeBind(host, host.resolveModuleNames),
14001400
getModuleResolutionCache: maybeBind(host, host.getModuleResolutionCache),
1401+
createHash: maybeBind(host, host.createHash),
14011402
resolveTypeReferenceDirectives: maybeBind(host, host.resolveTypeReferenceDirectives),
14021403
useSourceOfProjectReferenceRedirect: maybeBind(host, host.useSourceOfProjectReferenceRedirect),
14031404
getParsedCommandLine,

src/services/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ export interface LanguageServiceHost extends GetEffectiveTypeRootsHost, MinimalR
303303
*/
304304
readDirectory?(path: string, extensions?: readonly string[], exclude?: readonly string[], include?: readonly string[], depth?: number): string[];
305305
realpath?(path: string): string;
306+
/** @internal */ createHash?(data: string): string;
306307

307308
/*
308309
* Unlike `realpath and `readDirectory`, `readFile` and `fileExists` are now _required_

src/testRunner/unittests/builder.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe("unittests:: builder", () => {
7474
});
7575

7676
function makeAssertChanges(getProgram: () => ts.Program): (fileNames: readonly string[]) => void {
77-
const host: ts.BuilderProgramHost = { useCaseSensitiveFileNames: ts.returnTrue };
77+
const host: ts.BuilderProgramHost = { };
7878
let builderProgram: ts.EmitAndSemanticDiagnosticsBuilderProgram | undefined;
7979
return fileNames => {
8080
const program = getProgram();
@@ -88,7 +88,7 @@ function makeAssertChanges(getProgram: () => ts.Program): (fileNames: readonly s
8888
}
8989

9090
function makeAssertChangesWithCancellationToken(getProgram: () => ts.Program): (fileNames: readonly string[], cancelAfterEmitLength?: number) => void {
91-
const host: ts.BuilderProgramHost = { useCaseSensitiveFileNames: ts.returnTrue };
91+
const host: ts.BuilderProgramHost = { };
9292
let builderProgram: ts.EmitAndSemanticDiagnosticsBuilderProgram | undefined;
9393
let cancel = false;
9494
const cancellationToken: ts.CancellationToken = {

tests/baselines/reference/api/tsserverlibrary.d.ts

-4
Original file line numberDiff line numberDiff line change
@@ -9431,10 +9431,6 @@ declare namespace ts {
94319431
affected: SourceFile | Program;
94329432
} | undefined;
94339433
interface BuilderProgramHost {
9434-
/**
9435-
* return true if file names are treated with case sensitivity
9436-
*/
9437-
useCaseSensitiveFileNames(): boolean;
94389434
/**
94399435
* If provided this would be used this hash instead of actual file shape text for detecting changes
94409436
*/

tests/baselines/reference/api/typescript.d.ts

-4
Original file line numberDiff line numberDiff line change
@@ -5495,10 +5495,6 @@ declare namespace ts {
54955495
affected: SourceFile | Program;
54965496
} | undefined;
54975497
interface BuilderProgramHost {
5498-
/**
5499-
* return true if file names are treated with case sensitivity
5500-
*/
5501-
useCaseSensitiveFileNames(): boolean;
55025498
/**
55035499
* If provided this would be used this hash instead of actual file shape text for detecting changes
55045500
*/

0 commit comments

Comments
 (0)