Skip to content

Commit

Permalink
Handle declaration file names consistently (#48647)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat authored Apr 12, 2022
1 parent cce61d1 commit 988fa85
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/compiler/builderState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ namespace ts {
);
const firstDts = firstOrUndefined(emitOutput.outputFiles);
if (firstDts) {
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))}`);
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))}`);
latestSignature = (computeHash || generateDjb2Hash)(firstDts.text);
if (exportedModulesMapCache && latestSignature !== prevSignature) {
updateExportedModules(sourceFile, emitOutput.exportedModulesFromDeclarationEmit, exportedModulesMapCache);
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ namespace ts {
}

function getOwnOutputFileNames(configFile: ParsedCommandLine, inputFileName: string, ignoreCase: boolean, addOutput: ReturnType<typeof createAddOutput>["addOutput"], getCommonSourceDirectory?: () => string) {
if (fileExtensionIs(inputFileName, Extension.Dts)) return;
if (isDeclarationFileName(inputFileName)) return;
const js = getOutputJSFileName(inputFileName, configFile, ignoreCase, getCommonSourceDirectory);
addOutput(js);
if (fileExtensionIs(inputFileName, Extension.Json)) return;
Expand Down Expand Up @@ -219,7 +219,7 @@ namespace ts {
export function getCommonSourceDirectoryOfConfig({ options, fileNames }: ParsedCommandLine, ignoreCase: boolean): string {
return getCommonSourceDirectory(
options,
() => filter(fileNames, file => !(options.noEmitForJsFiles && fileExtensionIsOneOf(file, supportedJSExtensionsFlat)) && !fileExtensionIs(file, Extension.Dts)),
() => filter(fileNames, file => !(options.noEmitForJsFiles && fileExtensionIsOneOf(file, supportedJSExtensionsFlat)) && !isDeclarationFileName(file)),
getDirectoryPath(normalizeSlashes(Debug.checkDefined(options.configFilePath))),
createGetCanonicalFileName(!ignoreCase)
);
Expand Down Expand Up @@ -263,7 +263,7 @@ namespace ts {

const getCommonSourceDirectory = memoize(() => getCommonSourceDirectoryOfConfig(configFile, ignoreCase));
for (const inputFileName of configFile.fileNames) {
if (fileExtensionIs(inputFileName, Extension.Dts)) continue;
if (isDeclarationFileName(inputFileName)) continue;
const jsFilePath = getOutputJSFileName(inputFileName, configFile, ignoreCase, getCommonSourceDirectory);
if (jsFilePath) return jsFilePath;
if (fileExtensionIs(inputFileName, Extension.Json)) continue;
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1498,9 +1498,9 @@ namespace ts {
}

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

return loadModuleFromFileNoImplicitExtensions(extensions, candidate, onlyRecordFailures, state);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9413,7 +9413,7 @@ namespace ts {

/** @internal */
export function isDeclarationFileName(fileName: string): boolean {
return fileExtensionIsOneOf(fileName, [Extension.Dts, Extension.Dmts, Extension.Dcts]);
return fileExtensionIsOneOf(fileName, supportedDeclarationExtensions);
}

/*@internal*/
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ namespace ts {
else if (getEmitModuleKind(parsedRef.commandLine.options) === ModuleKind.None) {
const getCommonSourceDirectory = memoize(() => getCommonSourceDirectoryOfConfig(parsedRef.commandLine, !host.useCaseSensitiveFileNames()));
for (const fileName of parsedRef.commandLine.fileNames) {
if (!fileExtensionIs(fileName, Extension.Dts) && !fileExtensionIs(fileName, Extension.Json)) {
if (!isDeclarationFileName(fileName) && !fileExtensionIs(fileName, Extension.Json)) {
processProjectReferenceFile(getOutputDeclarationFileName(fileName, parsedRef.commandLine, !host.useCaseSensitiveFileNames(), getCommonSourceDirectory), { kind: FileIncludeKind.OutputFromProjectReference, index });
}
}
Expand Down Expand Up @@ -1393,7 +1393,7 @@ namespace ts {

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

// The originalFileName could not be actual source file name if file found was d.ts from referecned project
// 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
Expand Down Expand Up @@ -2969,7 +2969,7 @@ namespace ts {

function getProjectReferenceRedirectProject(fileName: string) {
// Ignore dts or any json files
if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts) || fileExtensionIs(fileName, Extension.Json)) {
if (!resolvedProjectReferences || !resolvedProjectReferences.length || isDeclarationFileName(fileName) || fileExtensionIs(fileName, Extension.Json)) {
return undefined;
}

Expand Down Expand Up @@ -3025,7 +3025,7 @@ namespace ts {
else {
const getCommonSourceDirectory = memoize(() => getCommonSourceDirectoryOfConfig(resolvedRef.commandLine, !host.useCaseSensitiveFileNames()));
forEach(resolvedRef.commandLine.fileNames, fileName => {
if (!fileExtensionIs(fileName, Extension.Dts) && !fileExtensionIs(fileName, Extension.Json)) {
if (!isDeclarationFileName(fileName) && !fileExtensionIs(fileName, Extension.Json)) {
const outputDts = getOutputDeclarationFileName(fileName, resolvedRef.commandLine, !host.useCaseSensitiveFileNames(), getCommonSourceDirectory);
mapFromToProjectReferenceRedirectSource!.set(toPath(outputDts), fileName);
}
Expand Down Expand Up @@ -3947,7 +3947,7 @@ namespace ts {
return containsPath(options.outDir, filePath, currentDirectory, !host.useCaseSensitiveFileNames());
}

if (fileExtensionIsOneOf(filePath, supportedJSExtensionsFlat) || fileExtensionIs(filePath, Extension.Dts)) {
if (fileExtensionIsOneOf(filePath, supportedJSExtensionsFlat) || isDeclarationFileName(filePath)) {
// Otherwise just check if sourceFile with the name exists
const filePathWithoutExtension = removeFileExtension(filePath);
return !!getSourceFileByPath((filePathWithoutExtension + Extension.Ts) as Path) ||
Expand Down
10 changes: 3 additions & 7 deletions src/compiler/tsbuildPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ namespace ts {
return date2 > date1 ? date2 : date1;
}

function isDeclarationFile(fileName: string) {
return fileExtensionIs(fileName, Extension.Dts);
}

export type ReportEmitErrorSummary = (errorCount: number, filesInError: (ReportFileInError | undefined)[]) => void;

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

if (isDeclarationFile(file)) {
if (isDeclarationFileName(file)) {
priorNewestUpdateTime = newer(priorNewestUpdateTime, getModifiedTime(host, file));
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6913,6 +6913,7 @@ namespace ts {
export const supportedJSExtensionsFlat: readonly Extension[] = flatten(supportedJSExtensions);
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]];
const allSupportedExtensionsWithJson: readonly Extension[][] = [...allSupportedExtensions, [Extension.Json]];
export const supportedDeclarationExtensions: readonly Extension[] = [Extension.Dts, Extension.Dcts, Extension.Dmts];

export function getSupportedExtensions(options?: CompilerOptions): readonly Extension[][];
export function getSupportedExtensions(options?: CompilerOptions, extraFileExtensions?: readonly FileExtensionInfo[]): readonly string[][];
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/watchUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ namespace ts {
if (outFile(options) || options.outDir) return false;

// File if emitted next to input needs to be ignored
if (fileExtensionIs(fileOrDirectoryPath, Extension.Dts)) {
if (isDeclarationFileName(fileOrDirectoryPath)) {
// If its declaration directory: its not ignored if not excluded by config
if (options.declarationDir) return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/harness/sourceMapRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ namespace Harness.SourceMapRecorder {
const sourceMapData = sourceMapDataList[i];
let prevSourceFile: ts.SourceFile | undefined;
let currentFile: documents.TextDocument;
if (ts.endsWith(sourceMapData.sourceMap.file, ts.Extension.Dts)) {
if (ts.isDeclarationFileName(sourceMapData.sourceMap.file)) {
if (sourceMapDataList.length > jsFiles.length) {
currentFile = declarationFiles[Math.floor(i / 2)]; // When both kinds of source map are present, they alternate js/dts
}
Expand Down
2 changes: 1 addition & 1 deletion src/harness/vpathUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace vpath {
}

export function isDeclaration(path: string) {
return ts.fileExtensionIsOneOf(path, [ts.Extension.Dmts, ts.Extension.Dcts, ts.Extension.Dts]);
return ts.isDeclarationFileName(path);
}

export function isSourceMap(path: string) {
Expand Down
6 changes: 3 additions & 3 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace ts.server {
result.jsxSize! += fileSize;
break;
case ScriptKind.TS:
if (fileExtensionIs(info.fileName, Extension.Dts)) {
if (isDeclarationFileName(info.fileName)) {
result.dts += 1;
result.dtsSize! += fileSize;
}
Expand Down Expand Up @@ -71,7 +71,7 @@ namespace ts.server {

/* @internal */
export function hasNoTypeScriptSource(fileNames: string[]): boolean {
return !fileNames.some(fileName => (fileExtensionIs(fileName, Extension.Ts) && !fileExtensionIs(fileName, Extension.Dts)) || fileExtensionIs(fileName, Extension.Tsx));
return !fileNames.some(fileName => (fileExtensionIs(fileName, Extension.Ts) && !isDeclarationFileName(fileName)) || fileExtensionIs(fileName, Extension.Tsx));
}

/* @internal */
Expand Down Expand Up @@ -686,7 +686,7 @@ namespace ts.server {

// Update the signature
if (this.builderState && getEmitDeclarations(this.compilerOptions)) {
const dtsFiles = outputFiles.filter(f => fileExtensionIs(f.name, Extension.Dts));
const dtsFiles = outputFiles.filter(f => isDeclarationFileName(f.name));
if (dtsFiles.length === 1) {
const sourceFile = this.program!.getSourceFile(scriptInfo.fileName)!;
const signature = this.projectService.host.createHash ?
Expand Down
4 changes: 2 additions & 2 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,7 @@ namespace ts.server {

const compilationSettings = project.getCompilationSettings();

if (!!compilationSettings.noEmit || fileExtensionIs(info.fileName, Extension.Dts) && !dtsChangeCanAffectEmit(compilationSettings)) {
if (!!compilationSettings.noEmit || isDeclarationFileName(info.fileName) && !dtsChangeCanAffectEmit(compilationSettings)) {
// avoid triggering emit when a change is made in a .d.ts when declaration emit and decorator metadata emit are disabled
return undefined;
}
Expand Down Expand Up @@ -2479,7 +2479,7 @@ namespace ts.server {
else {
const info = this.projectService.getScriptInfo(fileNameInProject)!; // TODO: GH#18217
if (!info.isScriptOpen()) {
if (fileExtensionIs(fileNameInProject, Extension.Dts)) {
if (isDeclarationFileName(fileNameInProject)) {
veryLowPriorityFiles.push(fileNameInProject);
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src/services/documentRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ namespace ts {
// It is fairly suspicious to have one path in two buckets - you'd expect dependencies to have similar configurations.
// If this occurs unexpectedly, the fix is likely to synchronize the project settings.
// Skip .d.ts files to reduce noise (should also cover most of node_modules).
const otherBucketKey = !fileExtensionIs(path, Extension.Dts) &&
const otherBucketKey = !isDeclarationFileName(path) &&
forEachEntry(buckets, (bucket, bucketKey) => bucketKey !== key && bucket.has(path) && bucketKey);
if (otherBucketKey) {
tracing.instant(tracing.Phase.Session, "documentRegistryBucketOverlap", { path, key1: otherBucketKey, key2: key });
Expand Down

0 comments on commit 988fa85

Please sign in to comment.