mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Handle declaration file names consistently (#48647)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
@@ -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)
|
||||
);
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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*/
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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) ||
|
||||
|
||||
@@ -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 {
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
}
|
||||
@@ -1589,7 +1585,7 @@ namespace ts {
|
||||
reportStatus(state, verboseMessage, proj.options.configFilePath!);
|
||||
}
|
||||
|
||||
if (isDeclarationFile(file)) {
|
||||
if (isDeclarationFileName(file)) {
|
||||
priorNewestUpdateTime = newer(priorNewestUpdateTime, getModifiedTime(host, file));
|
||||
}
|
||||
|
||||
|
||||
@@ -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[][];
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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 */
|
||||
@@ -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 ?
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 });
|
||||
|
||||
Reference in New Issue
Block a user