Skip to content

Commit 988fa85

Browse files
authored
Handle declaration file names consistently (microsoft#48647)
1 parent cce61d1 commit 988fa85

File tree

13 files changed

+25
-28
lines changed

13 files changed

+25
-28
lines changed

src/compiler/builderState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ namespace ts {
434434
);
435435
const firstDts = firstOrUndefined(emitOutput.outputFiles);
436436
if (firstDts) {
437-
Debug.assert(fileExtensionIsOneOf(firstDts.name, [Extension.Dts, Extension.Dmts, Extension.Dcts]), "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))}`);
437+
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))}`);
438438
latestSignature = (computeHash || generateDjb2Hash)(firstDts.text);
439439
if (exportedModulesMapCache && latestSignature !== prevSignature) {
440440
updateExportedModules(sourceFile, emitOutput.exportedModulesFromDeclarationEmit, exportedModulesMapCache);

src/compiler/emitter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ namespace ts {
167167
}
168168

169169
function getOwnOutputFileNames(configFile: ParsedCommandLine, inputFileName: string, ignoreCase: boolean, addOutput: ReturnType<typeof createAddOutput>["addOutput"], getCommonSourceDirectory?: () => string) {
170-
if (fileExtensionIs(inputFileName, Extension.Dts)) return;
170+
if (isDeclarationFileName(inputFileName)) return;
171171
const js = getOutputJSFileName(inputFileName, configFile, ignoreCase, getCommonSourceDirectory);
172172
addOutput(js);
173173
if (fileExtensionIs(inputFileName, Extension.Json)) return;
@@ -219,7 +219,7 @@ namespace ts {
219219
export function getCommonSourceDirectoryOfConfig({ options, fileNames }: ParsedCommandLine, ignoreCase: boolean): string {
220220
return getCommonSourceDirectory(
221221
options,
222-
() => filter(fileNames, file => !(options.noEmitForJsFiles && fileExtensionIsOneOf(file, supportedJSExtensionsFlat)) && !fileExtensionIs(file, Extension.Dts)),
222+
() => filter(fileNames, file => !(options.noEmitForJsFiles && fileExtensionIsOneOf(file, supportedJSExtensionsFlat)) && !isDeclarationFileName(file)),
223223
getDirectoryPath(normalizeSlashes(Debug.checkDefined(options.configFilePath))),
224224
createGetCanonicalFileName(!ignoreCase)
225225
);
@@ -263,7 +263,7 @@ namespace ts {
263263

264264
const getCommonSourceDirectory = memoize(() => getCommonSourceDirectoryOfConfig(configFile, ignoreCase));
265265
for (const inputFileName of configFile.fileNames) {
266-
if (fileExtensionIs(inputFileName, Extension.Dts)) continue;
266+
if (isDeclarationFileName(inputFileName)) continue;
267267
const jsFilePath = getOutputJSFileName(inputFileName, configFile, ignoreCase, getCommonSourceDirectory);
268268
if (jsFilePath) return jsFilePath;
269269
if (fileExtensionIs(inputFileName, Extension.Json)) continue;

src/compiler/moduleNameResolver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,9 +1498,9 @@ namespace ts {
14981498
}
14991499

15001500
function loadJSOrExactTSFileName(extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState): PathAndExtension | undefined {
1501-
if ((extensions === Extensions.TypeScript || extensions === Extensions.DtsOnly) && fileExtensionIsOneOf(candidate, [Extension.Dts, Extension.Dcts, Extension.Dmts])) {
1501+
if ((extensions === Extensions.TypeScript || extensions === Extensions.DtsOnly) && isDeclarationFileName(candidate)) {
15021502
const result = tryFile(candidate, onlyRecordFailures, state);
1503-
return result !== undefined ? { path: candidate, ext: forEach([Extension.Dts, Extension.Dcts, Extension.Dmts], e => fileExtensionIs(candidate, e) ? e : undefined)! } : undefined;
1503+
return result !== undefined ? { path: candidate, ext: forEach(supportedDeclarationExtensions, e => fileExtensionIs(candidate, e) ? e : undefined)! } : undefined;
15041504
}
15051505

15061506
return loadModuleFromFileNoImplicitExtensions(extensions, candidate, onlyRecordFailures, state);

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9413,7 +9413,7 @@ namespace ts {
94139413

94149414
/** @internal */
94159415
export function isDeclarationFileName(fileName: string): boolean {
9416-
return fileExtensionIsOneOf(fileName, [Extension.Dts, Extension.Dmts, Extension.Dcts]);
9416+
return fileExtensionIsOneOf(fileName, supportedDeclarationExtensions);
94179417
}
94189418

94199419
/*@internal*/

src/compiler/program.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ namespace ts {
11861186
else if (getEmitModuleKind(parsedRef.commandLine.options) === ModuleKind.None) {
11871187
const getCommonSourceDirectory = memoize(() => getCommonSourceDirectoryOfConfig(parsedRef.commandLine, !host.useCaseSensitiveFileNames()));
11881188
for (const fileName of parsedRef.commandLine.fileNames) {
1189-
if (!fileExtensionIs(fileName, Extension.Dts) && !fileExtensionIs(fileName, Extension.Json)) {
1189+
if (!isDeclarationFileName(fileName) && !fileExtensionIs(fileName, Extension.Json)) {
11901190
processProjectReferenceFile(getOutputDeclarationFileName(fileName, parsedRef.commandLine, !host.useCaseSensitiveFileNames(), getCommonSourceDirectory), { kind: FileIncludeKind.OutputFromProjectReference, index });
11911191
}
11921192
}
@@ -1393,7 +1393,7 @@ namespace ts {
13931393

13941394
function getRedirectReferenceForResolution(file: SourceFile) {
13951395
const redirect = getResolvedProjectReferenceToRedirect(file.originalFileName);
1396-
if (redirect || !fileExtensionIsOneOf(file.originalFileName, [Extension.Dts, Extension.Dcts, Extension.Dmts])) return redirect;
1396+
if (redirect || !isDeclarationFileName(file.originalFileName)) return redirect;
13971397

13981398
// The originalFileName could not be actual source file name if file found was d.ts from referecned project
13991399
// So in this case try to look up if this is output from referenced project, if it is use the redirected project in that case
@@ -2969,7 +2969,7 @@ namespace ts {
29692969

29702970
function getProjectReferenceRedirectProject(fileName: string) {
29712971
// Ignore dts or any json files
2972-
if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts) || fileExtensionIs(fileName, Extension.Json)) {
2972+
if (!resolvedProjectReferences || !resolvedProjectReferences.length || isDeclarationFileName(fileName) || fileExtensionIs(fileName, Extension.Json)) {
29732973
return undefined;
29742974
}
29752975

@@ -3025,7 +3025,7 @@ namespace ts {
30253025
else {
30263026
const getCommonSourceDirectory = memoize(() => getCommonSourceDirectoryOfConfig(resolvedRef.commandLine, !host.useCaseSensitiveFileNames()));
30273027
forEach(resolvedRef.commandLine.fileNames, fileName => {
3028-
if (!fileExtensionIs(fileName, Extension.Dts) && !fileExtensionIs(fileName, Extension.Json)) {
3028+
if (!isDeclarationFileName(fileName) && !fileExtensionIs(fileName, Extension.Json)) {
30293029
const outputDts = getOutputDeclarationFileName(fileName, resolvedRef.commandLine, !host.useCaseSensitiveFileNames(), getCommonSourceDirectory);
30303030
mapFromToProjectReferenceRedirectSource!.set(toPath(outputDts), fileName);
30313031
}
@@ -3947,7 +3947,7 @@ namespace ts {
39473947
return containsPath(options.outDir, filePath, currentDirectory, !host.useCaseSensitiveFileNames());
39483948
}
39493949

3950-
if (fileExtensionIsOneOf(filePath, supportedJSExtensionsFlat) || fileExtensionIs(filePath, Extension.Dts)) {
3950+
if (fileExtensionIsOneOf(filePath, supportedJSExtensionsFlat) || isDeclarationFileName(filePath)) {
39513951
// Otherwise just check if sourceFile with the name exists
39523952
const filePathWithoutExtension = removeFileExtension(filePath);
39533953
return !!getSourceFileByPath((filePathWithoutExtension + Extension.Ts) as Path) ||

src/compiler/tsbuildPublic.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ namespace ts {
7272
return date2 > date1 ? date2 : date1;
7373
}
7474

75-
function isDeclarationFile(fileName: string) {
76-
return fileExtensionIs(fileName, Extension.Dts);
77-
}
78-
7975
export type ReportEmitErrorSummary = (errorCount: number, filesInError: (ReportFileInError | undefined)[]) => void;
8076

8177
export interface ReportFileInError {
@@ -972,7 +968,7 @@ namespace ts {
972968
const emittedOutputs = new Map<Path, string>();
973969
outputFiles.forEach(({ name, text, writeByteOrderMark }) => {
974970
let priorChangeTime: Date | undefined;
975-
if (!anyDtsChanged && isDeclarationFile(name)) {
971+
if (!anyDtsChanged && isDeclarationFileName(name)) {
976972
// Check for unchanged .d.ts files
977973
if (host.fileExists(name) && state.readFileWithCache(name) === text) {
978974
priorChangeTime = host.getModifiedTime(name);
@@ -1421,7 +1417,7 @@ namespace ts {
14211417
// In addition to file timestamps, we also keep track of when a .d.ts file
14221418
// had its file touched but not had its contents changed - this allows us
14231419
// to skip a downstream typecheck
1424-
if (isDeclarationFile(output)) {
1420+
if (isDeclarationFileName(output)) {
14251421
const outputModifiedTime = getModifiedTime(host, output);
14261422
newestDeclarationFileContentChangedTime = newer(newestDeclarationFileContentChangedTime, outputModifiedTime);
14271423
}
@@ -1589,7 +1585,7 @@ namespace ts {
15891585
reportStatus(state, verboseMessage, proj.options.configFilePath!);
15901586
}
15911587

1592-
if (isDeclarationFile(file)) {
1588+
if (isDeclarationFileName(file)) {
15931589
priorNewestUpdateTime = newer(priorNewestUpdateTime, getModifiedTime(host, file));
15941590
}
15951591

src/compiler/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6913,6 +6913,7 @@ namespace ts {
69136913
export const supportedJSExtensionsFlat: readonly Extension[] = flatten(supportedJSExtensions);
69146914
const allSupportedExtensions: readonly Extension[][] = [[Extension.Ts, Extension.Tsx, Extension.Dts, Extension.Js, Extension.Jsx], [Extension.Cts, Extension.Dcts, Extension.Cjs], [Extension.Mts, Extension.Dmts, Extension.Mjs]];
69156915
const allSupportedExtensionsWithJson: readonly Extension[][] = [...allSupportedExtensions, [Extension.Json]];
6916+
export const supportedDeclarationExtensions: readonly Extension[] = [Extension.Dts, Extension.Dcts, Extension.Dmts];
69166917

69176918
export function getSupportedExtensions(options?: CompilerOptions): readonly Extension[][];
69186919
export function getSupportedExtensions(options?: CompilerOptions, extraFileExtensions?: readonly FileExtensionInfo[]): readonly string[][];

src/compiler/watchUtilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ namespace ts {
495495
if (outFile(options) || options.outDir) return false;
496496

497497
// File if emitted next to input needs to be ignored
498-
if (fileExtensionIs(fileOrDirectoryPath, Extension.Dts)) {
498+
if (isDeclarationFileName(fileOrDirectoryPath)) {
499499
// If its declaration directory: its not ignored if not excluded by config
500500
if (options.declarationDir) return false;
501501
}

src/harness/sourceMapRecorder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ namespace Harness.SourceMapRecorder {
282282
const sourceMapData = sourceMapDataList[i];
283283
let prevSourceFile: ts.SourceFile | undefined;
284284
let currentFile: documents.TextDocument;
285-
if (ts.endsWith(sourceMapData.sourceMap.file, ts.Extension.Dts)) {
285+
if (ts.isDeclarationFileName(sourceMapData.sourceMap.file)) {
286286
if (sourceMapDataList.length > jsFiles.length) {
287287
currentFile = declarationFiles[Math.floor(i / 2)]; // When both kinds of source map are present, they alternate js/dts
288288
}

src/harness/vpathUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ namespace vpath {
108108
}
109109

110110
export function isDeclaration(path: string) {
111-
return ts.fileExtensionIsOneOf(path, [ts.Extension.Dmts, ts.Extension.Dcts, ts.Extension.Dts]);
111+
return ts.isDeclarationFileName(path);
112112
}
113113

114114
export function isSourceMap(path: string) {

0 commit comments

Comments
 (0)