Skip to content

Commit d024e27

Browse files
committed
Refactor
1 parent b8b9478 commit d024e27

File tree

7 files changed

+70
-86
lines changed

7 files changed

+70
-86
lines changed

src/compiler/builder.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1038,8 +1038,8 @@ namespace ts {
10381038
return { host, newProgram, oldProgram, configFileParsingDiagnostics: configFileParsingDiagnostics || emptyArray };
10391039
}
10401040

1041-
export function computeSignature(text: string, data: WriteFileCallbackData | undefined, computeHash: BuilderState.ComputeHash | undefined) {
1042-
return BuilderState.computeSignature(data?.sourceMapUrlPos !== undefined ? text.substring(0, data.sourceMapUrlPos) : text, computeHash);
1041+
export function computeSignature(text: string, computeHash: BuilderState.ComputeHash | undefined, data?: WriteFileCallbackData) {
1042+
return (computeHash || generateDjb2Hash)(data?.sourceMapUrlPos !== undefined ? text.substring(0, data.sourceMapUrlPos) : text);
10431043
}
10441044

10451045
export function createBuilderProgram(kind: BuilderProgramKind.SemanticDiagnosticsBuilderProgram, builderCreationParameters: BuilderCreationParameters): SemanticDiagnosticsBuilderProgram;
@@ -1170,7 +1170,7 @@ namespace ts {
11701170
const file = sourceFiles[0];
11711171
const info = state.fileInfos.get(file.resolvedPath)!;
11721172
if (info.signature === file.version) {
1173-
newSignature = computeSignature(text, data, computeHash);
1173+
newSignature = computeSignature(text, computeHash, data);
11741174
if (newSignature !== file.version) { // Update it
11751175
if (host.storeFilesChangingSignatureDuringEmit) (state.filesChangingSignature ||= new Set()).add(file.resolvedPath);
11761176
if (state.exportedModulesMap) BuilderState.updateExportedModules(state, file, file.exportedModulesFromDeclarationEmit);
@@ -1195,15 +1195,15 @@ namespace ts {
11951195
if (state.compilerOptions.composite) {
11961196
const filePath = sourceFiles[0].resolvedPath;
11971197
const oldSignature = state.emitSignatures?.get(filePath);
1198-
newSignature ||= computeSignature(text, data, computeHash);
1198+
newSignature ||= computeSignature(text, computeHash, data);
11991199
if (newSignature !== oldSignature) {
12001200
(state.emitSignatures ||= new Map()).set(filePath, newSignature);
12011201
state.hasChangedEmitSignature = true;
12021202
}
12031203
}
12041204
}
12051205
else if (state.compilerOptions.composite) {
1206-
const newSignature = computeSignature(text, data, computeHash);
1206+
const newSignature = computeSignature(text, computeHash, data);
12071207
if (newSignature !== state.outSignature) {
12081208
state.outSignature = newSignature;
12091209
state.hasChangedEmitSignature = true;

src/compiler/builderState.ts

+11-17
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ namespace ts {
33
export function getFileEmitOutput(program: Program, sourceFile: SourceFile, emitOnlyDtsFiles: boolean,
44
cancellationToken?: CancellationToken, customTransformers?: CustomTransformers, forceDtsEmit?: boolean): EmitOutput {
55
const outputFiles: OutputFile[] = [];
6-
const { emitSkipped, diagnostics, exportedModulesFromDeclarationEmit } = program.emit(sourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers, forceDtsEmit);
7-
return { outputFiles, emitSkipped, diagnostics, exportedModulesFromDeclarationEmit };
6+
const { emitSkipped, diagnostics } = program.emit(sourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers, forceDtsEmit);
7+
return { outputFiles, emitSkipped, diagnostics };
88

99
function writeFile(fileName: string, text: string, writeByteOrderMark: boolean) {
1010
outputFiles.push({ name: fileName, writeByteOrderMark, text });
@@ -357,22 +357,20 @@ namespace ts {
357357
const prevSignature = info.signature;
358358
let latestSignature: string | undefined;
359359
if (!sourceFile.isDeclarationFile && !useFileVersionAsSignature) {
360-
const emitOutput = getFileEmitOutput(
361-
programOfThisState,
360+
programOfThisState.emit(
362361
sourceFile,
363-
/*emitOnlyDtsFiles*/ true,
362+
(fileName, text, _writeByteOrderMark, _onError, sourceFiles, data) => {
363+
Debug.assert(isDeclarationFileName(fileName), `File extension for signature expected to be dts: Got:: ${fileName}`);
364+
latestSignature = computeSignature(text, computeHash, data);
365+
if (latestSignature !== prevSignature) {
366+
updateExportedModules(state, sourceFile, sourceFiles![0].exportedModulesFromDeclarationEmit);
367+
}
368+
},
364369
cancellationToken,
370+
/*emitOnlyDtsFiles*/ true,
365371
/*customTransformers*/ undefined,
366372
/*forceDtsEmit*/ true
367373
);
368-
const firstDts = firstOrUndefined(emitOutput.outputFiles);
369-
if (firstDts) {
370-
Debug.assert(isDeclarationFileName(firstDts.name), "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))}`);
371-
latestSignature = computeSignature(firstDts.text, computeHash);
372-
if (latestSignature !== prevSignature) {
373-
updateExportedModules(state, sourceFile, emitOutput.exportedModulesFromDeclarationEmit);
374-
}
375-
}
376374
}
377375
// Default is to use file version as signature
378376
if (latestSignature === undefined) {
@@ -395,10 +393,6 @@ namespace ts {
395393
return latestSignature !== prevSignature;
396394
}
397395

398-
export function computeSignature(text: string, computeHash: ComputeHash | undefined) {
399-
return (computeHash || generateDjb2Hash)(text);
400-
}
401-
402396
/**
403397
* Coverts the declaration emit result into exported modules map
404398
*/

src/compiler/builderStatePublic.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ namespace ts {
33
outputFiles: OutputFile[];
44
emitSkipped: boolean;
55
/* @internal */ diagnostics: readonly Diagnostic[];
6-
/* @internal */ exportedModulesFromDeclarationEmit?: ExportedModulesFromDeclarationEmit;
76
}
87

98
export interface OutputFile {

src/compiler/emitter.ts

+9-15
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ namespace ts {
288288
const { enter, exit } = performance.createTimer("printTime", "beforePrint", "afterPrint");
289289
let bundleBuildInfo: BundleBuildInfo | undefined;
290290
let emitSkipped = false;
291-
let exportedModulesFromDeclarationEmit: ExportedModulesFromDeclarationEmit | undefined;
292291

293292
// Emit each output file
294293
enter();
@@ -308,7 +307,6 @@ namespace ts {
308307
diagnostics: emitterDiagnostics.getDiagnostics(),
309308
emittedFiles: emittedFilesList,
310309
sourceMaps: sourceMapDataList,
311-
exportedModulesFromDeclarationEmit
312310
};
313311

314312
function emitSourceFileOrBundle({ jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, buildInfoPath }: EmitFileNames, sourceFileOrBundle: SourceFile | Bundle | undefined) {
@@ -453,7 +451,7 @@ namespace ts {
453451
noEmitHelpers: true,
454452
module: compilerOptions.module,
455453
target: compilerOptions.target,
456-
sourceMap: compilerOptions.sourceMap,
454+
sourceMap: !forceDtsEmit && compilerOptions.declarationMap,
457455
inlineSourceMap: compilerOptions.inlineSourceMap,
458456
extendedDiagnostics: compilerOptions.extendedDiagnostics,
459457
onlyPrintJsDocStyle: true,
@@ -481,17 +479,13 @@ namespace ts {
481479
declarationTransform.transformed[0],
482480
declarationPrinter,
483481
{
484-
sourceMap: !forceDtsEmit && compilerOptions.declarationMap,
482+
sourceMap: printerOptions.sourceMap,
485483
sourceRoot: compilerOptions.sourceRoot,
486484
mapRoot: compilerOptions.mapRoot,
487485
extendedDiagnostics: compilerOptions.extendedDiagnostics,
488486
// Explicitly do not passthru either `inline` option
489487
}
490488
);
491-
if (forceDtsEmit && declarationTransform.transformed[0].kind === SyntaxKind.SourceFile) {
492-
const sourceFile = declarationTransform.transformed[0];
493-
exportedModulesFromDeclarationEmit = sourceFile.exportedModulesFromDeclarationEmit;
494-
}
495489
}
496490
declarationTransform.dispose();
497491
if (bundleBuildInfo) bundleBuildInfo.dts = declarationPrinter.bundleFileInfo;
@@ -559,7 +553,7 @@ namespace ts {
559553
if (sourceMapFilePath) {
560554
const sourceMap = sourceMapGenerator.toString();
561555
writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap, /*writeByteOrderMark*/ false, sourceFiles);
562-
if (printer.bundleFileInfo) printer.bundleFileInfo.mapHash = BuilderState.computeSignature(sourceMap, maybeBind(host, host.createHash));
556+
if (printer.bundleFileInfo) printer.bundleFileInfo.mapHash = computeSignature(sourceMap, maybeBind(host, host.createHash));
563557
}
564558
}
565559
else {
@@ -571,7 +565,7 @@ namespace ts {
571565
writeFile(host, emitterDiagnostics, jsFilePath, text, !!compilerOptions.emitBOM, sourceFiles, { sourceMapUrlPos });
572566
// 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
573567
// 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
574-
if (printer.bundleFileInfo) printer.bundleFileInfo.hash = BuilderState.computeSignature(text, maybeBind(host, host.createHash));
568+
if (printer.bundleFileInfo) printer.bundleFileInfo.hash = computeSignature(text, maybeBind(host, host.createHash));
575569

576570
// Reset state
577571
writer.clear();
@@ -774,20 +768,20 @@ namespace ts {
774768
const jsFileText = host.readFile(Debug.checkDefined(jsFilePath));
775769
if (!jsFileText) return jsFilePath!;
776770
// If the jsFileText is not same has what it was created with, tsbuildinfo is stale so dont use it
777-
if (BuilderState.computeSignature(jsFileText, createHash) !== buildInfo.bundle.js.hash) return jsFilePath!;
771+
if (computeSignature(jsFileText, createHash) !== buildInfo.bundle.js.hash) return jsFilePath!;
778772
const sourceMapText = sourceMapFilePath && host.readFile(sourceMapFilePath);
779773
// error if no source map or for now if inline sourcemap
780774
if ((sourceMapFilePath && !sourceMapText) || config.options.inlineSourceMap) return sourceMapFilePath || "inline sourcemap decoding";
781-
if (sourceMapFilePath && BuilderState.computeSignature(sourceMapText!, createHash) !== buildInfo.bundle.js.mapHash) return sourceMapFilePath;
775+
if (sourceMapFilePath && computeSignature(sourceMapText!, createHash) !== buildInfo.bundle.js.mapHash) return sourceMapFilePath;
782776

783777
// read declaration text
784778
const declarationText = declarationFilePath && host.readFile(declarationFilePath);
785779
if (declarationFilePath && !declarationText) return declarationFilePath;
786-
if (declarationFilePath && BuilderState.computeSignature(declarationText!, createHash) !== buildInfo.bundle.dts!.hash) return declarationFilePath;
780+
if (declarationFilePath && computeSignature(declarationText!, createHash) !== buildInfo.bundle.dts!.hash) return declarationFilePath;
787781
const declarationMapText = declarationMapPath && host.readFile(declarationMapPath);
788782
// error if no source map or for now if inline sourcemap
789783
if ((declarationMapPath && !declarationMapText) || config.options.inlineSourceMap) return declarationMapPath || "inline sourcemap decoding";
790-
if (declarationMapPath && BuilderState.computeSignature(declarationMapText!, createHash) !== buildInfo.bundle.dts!.mapHash) return declarationMapPath;
784+
if (declarationMapPath && computeSignature(declarationMapText!, createHash) !== buildInfo.bundle.dts!.mapHash) return declarationMapPath;
791785

792786
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(buildInfoPath!, host.getCurrentDirectory()));
793787
const ownPrependInput = createInputFiles(
@@ -836,7 +830,7 @@ namespace ts {
836830
newBuildInfo.program = buildInfo.program;
837831
if (newBuildInfo.program && changedDtsText !== undefined && config.options.composite) {
838832
// Update the output signature
839-
(newBuildInfo.program as ProgramBundleEmitBuildInfo).outSignature = computeSignature(changedDtsText, changedDtsData, createHash);
833+
(newBuildInfo.program as ProgramBundleEmitBuildInfo).outSignature = computeSignature(changedDtsText, createHash, changedDtsData);
840834
newBuildInfo.program.dtsChangeTime = getCurrentTime(host).getTime();
841835
}
842836
// Update sourceFileInfo

src/compiler/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -4335,7 +4335,6 @@ namespace ts {
43354335
diagnostics: readonly Diagnostic[];
43364336
emittedFiles?: string[]; // Array of files the compiler wrote to disk
43374337
/* @internal */ sourceMaps?: SourceMapEmitResult[]; // Array of sourceMapData if compiler emitted sourcemaps
4338-
/* @internal */ exportedModulesFromDeclarationEmit?: ExportedModulesFromDeclarationEmit;
43394338
}
43404339

43414340
/* @internal */

src/testRunner/unittests/services/languageService.ts

-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export function Component(x: Config): any;`
6262
emitSkipped: true,
6363
diagnostics: emptyArray,
6464
outputFiles: emptyArray,
65-
exportedModulesFromDeclarationEmit: undefined
6665
}
6766
);
6867

@@ -80,7 +79,6 @@ export function Component(x: Config): any;`
8079
text: "export {};\r\n",
8180
writeByteOrderMark: false
8281
}],
83-
exportedModulesFromDeclarationEmit: undefined
8482
}
8583
);
8684
});

0 commit comments

Comments
 (0)