Skip to content

Commit 5ecdcef

Browse files
authored
Remove unnecessary requirement for createHash since we already handle absence of it (#40891)
1 parent 6a5527e commit 5ecdcef

File tree

6 files changed

+24
-13
lines changed

6 files changed

+24
-13
lines changed

src/compiler/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ namespace ts {
900900
/**
901901
* Computing hash to for signature verification
902902
*/
903-
const computeHash = host.createHash || generateDjb2Hash;
903+
const computeHash = maybeBind(host, host.createHash);
904904
let state = createBuilderProgramState(newProgram, getCanonicalFileName, oldState);
905905
let backupState: BuilderProgramState | undefined;
906906
newProgram.getProgramBuildInfo = () => getProgramBuildInfo(state, getCanonicalFileName);

src/compiler/builderState.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace ts {
7777
/**
7878
* Compute the hash to store the shape of the file
7979
*/
80-
export type ComputeHash = (data: string) => string;
80+
export type ComputeHash = ((data: string) => string) | undefined;
8181

8282
/**
8383
* Exported modules to from declaration emit being computed.
@@ -337,7 +337,7 @@ namespace ts {
337337
emitOutput.outputFiles.length > 0 ? emitOutput.outputFiles[0] : undefined;
338338
if (firstDts) {
339339
Debug.assert(fileExtensionIs(firstDts.name, Extension.Dts), "File extension for signature expected to be dts", () => `Found: ${getAnyExtensionFromPath(firstDts.name)} for ${firstDts.name}:: All output files: ${JSON.stringify(emitOutput.outputFiles.map(f => f.name))}`);
340-
latestSignature = computeHash(firstDts.text);
340+
latestSignature = (computeHash || generateDjb2Hash)(firstDts.text);
341341
if (exportedModulesMapCache && latestSignature !== prevSignature) {
342342
updateExportedModules(sourceFile, emitOutput.exportedModulesFromDeclarationEmit, exportedModulesMapCache);
343343
}
@@ -521,7 +521,7 @@ namespace ts {
521521
/**
522522
* When program emits modular code, gets the files affected by the sourceFile whose shape has changed
523523
*/
524-
function getFilesAffectedByUpdatedShapeWhenModuleEmit(state: BuilderState, programOfThisState: Program, sourceFileWithUpdatedShape: SourceFile, cacheToUpdateSignature: ESMap<Path, string>, cancellationToken: CancellationToken | undefined, computeHash: ComputeHash | undefined, exportedModulesMapCache: ComputingExportedModulesMap | undefined) {
524+
function getFilesAffectedByUpdatedShapeWhenModuleEmit(state: BuilderState, programOfThisState: Program, sourceFileWithUpdatedShape: SourceFile, cacheToUpdateSignature: ESMap<Path, string>, cancellationToken: CancellationToken | undefined, computeHash: ComputeHash, exportedModulesMapCache: ComputingExportedModulesMap | undefined) {
525525
if (isFileAffectingGlobalScope(sourceFileWithUpdatedShape)) {
526526
return getAllFilesExcludingDefaultLibraryFile(state, programOfThisState, sourceFileWithUpdatedShape);
527527
}
@@ -544,7 +544,7 @@ namespace ts {
544544
if (!seenFileNamesMap.has(currentPath)) {
545545
const currentSourceFile = programOfThisState.getSourceFileByPath(currentPath)!;
546546
seenFileNamesMap.set(currentPath, currentSourceFile);
547-
if (currentSourceFile && updateShapeSignature(state, programOfThisState, currentSourceFile, cacheToUpdateSignature, cancellationToken, computeHash!, exportedModulesMapCache)) { // TODO: GH#18217
547+
if (currentSourceFile && updateShapeSignature(state, programOfThisState, currentSourceFile, cacheToUpdateSignature, cancellationToken, computeHash, exportedModulesMapCache)) {
548548
queue.push(...getReferencedByPaths(state, currentSourceFile.resolvedPath));
549549
}
550550
}

src/compiler/program.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ namespace ts {
7373
export function createCompilerHostWorker(options: CompilerOptions, setParentNodes?: boolean, system = sys): CompilerHost {
7474
const existingDirectories = new Map<string, boolean>();
7575
const getCanonicalFileName = createGetCanonicalFileName(system.useCaseSensitiveFileNames);
76+
const computeHash = maybeBind(system, system.createHash) || generateDjb2Hash;
7677
function getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile | undefined {
7778
let text: string | undefined;
7879
try {
@@ -128,7 +129,7 @@ namespace ts {
128129

129130
let outputFingerprints: ESMap<string, OutputFingerprint>;
130131
function writeFileWorker(fileName: string, data: string, writeByteOrderMark: boolean) {
131-
if (!isWatchSet(options) || !system.createHash || !system.getModifiedTime) {
132+
if (!isWatchSet(options) || !system.getModifiedTime) {
132133
system.writeFile(fileName, data, writeByteOrderMark);
133134
return;
134135
}
@@ -137,7 +138,7 @@ namespace ts {
137138
outputFingerprints = new Map<string, OutputFingerprint>();
138139
}
139140

140-
const hash = system.createHash(data);
141+
const hash = computeHash(data);
141142
const mtimeBefore = system.getModifiedTime(fileName);
142143

143144
if (mtimeBefore) {

src/compiler/watch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,11 @@ namespace ts {
345345

346346
export function setGetSourceFileAsHashVersioned(compilerHost: CompilerHost, host: { createHash?(data: string): string; }) {
347347
const originalGetSourceFile = compilerHost.getSourceFile;
348-
const computeHash = host.createHash || generateDjb2Hash;
348+
const computeHash = maybeBind(host, host.createHash) || generateDjb2Hash;
349349
compilerHost.getSourceFile = (...args) => {
350350
const result = originalGetSourceFile.call(compilerHost, ...args);
351351
if (result) {
352-
result.version = computeHash.call(host, result.text);
352+
result.version = computeHash(result.text);
353353
}
354354
return result;
355355
};

src/server/editorServices.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,6 @@ namespace ts.server {
730730
this.syntaxOnly = false;
731731
}
732732

733-
Debug.assert(!!this.host.createHash, "'ServerHost.createHash' is required for ProjectService");
734733
if (this.host.realpath) {
735734
this.realpathToScriptInfos = createMultiMap();
736735
}

src/server/project.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,16 @@ namespace ts.server {
631631
}
632632
updateProjectIfDirty(this);
633633
this.builderState = BuilderState.create(this.program!, this.projectService.toCanonicalFileName, this.builderState);
634-
return mapDefined(BuilderState.getFilesAffectedBy(this.builderState, this.program!, scriptInfo.path, this.cancellationToken, data => this.projectService.host.createHash!(data)), // TODO: GH#18217
635-
sourceFile => this.shouldEmitFile(this.projectService.getScriptInfoForPath(sourceFile.path)) ? sourceFile.fileName : undefined);
634+
return mapDefined(
635+
BuilderState.getFilesAffectedBy(
636+
this.builderState,
637+
this.program!,
638+
scriptInfo.path,
639+
this.cancellationToken,
640+
maybeBind(this.projectService.host, this.projectService.host.createHash)
641+
),
642+
sourceFile => this.shouldEmitFile(this.projectService.getScriptInfoForPath(sourceFile.path)) ? sourceFile.fileName : undefined
643+
);
636644
}
637645

638646
/**
@@ -654,7 +662,10 @@ namespace ts.server {
654662
const dtsFiles = outputFiles.filter(f => fileExtensionIs(f.name, Extension.Dts));
655663
if (dtsFiles.length === 1) {
656664
const sourceFile = this.program!.getSourceFile(scriptInfo.fileName)!;
657-
BuilderState.updateSignatureOfFile(this.builderState, this.projectService.host.createHash!(dtsFiles[0].text), sourceFile.resolvedPath);
665+
const signature = this.projectService.host.createHash ?
666+
this.projectService.host.createHash(dtsFiles[0].text) :
667+
generateDjb2Hash(dtsFiles[0].text);
668+
BuilderState.updateSignatureOfFile(this.builderState, signature, sourceFile.resolvedPath);
658669
}
659670
}
660671
}

0 commit comments

Comments
 (0)