Skip to content

Commit 0339f7e

Browse files
committed
If import is used in the file, prefer that import specifier over calculating new one
Fixes #38111
1 parent e69d317 commit 0339f7e

File tree

7 files changed

+19
-5
lines changed

7 files changed

+19
-5
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4378,7 +4378,8 @@ namespace ts {
43784378
getProjectReferenceRedirect: fileName => host.getProjectReferenceRedirect(fileName),
43794379
isSourceOfProjectReferenceRedirect: fileName => host.isSourceOfProjectReferenceRedirect(fileName),
43804380
fileExists: fileName => host.fileExists(fileName),
4381-
getCompilerOptions: () => host.getCompilerOptions()
4381+
getCompilerOptions: () => host.getCompilerOptions(),
4382+
getFileIncludeReasons: () => host.getFileIncludeReasons(),
43824383
} : undefined },
43834384
encounteredError: false,
43844385
visitedTypes: undefined,

src/compiler/emitter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,8 @@ namespace ts {
849849
useCaseSensitiveFileNames: () => host.useCaseSensitiveFileNames(),
850850
getProgramBuildInfo: returnUndefined,
851851
getSourceFileFromReference: returnUndefined,
852-
redirectTargetsMap: createMultiMap()
852+
redirectTargetsMap: createMultiMap(),
853+
getFileIncludeReasons: notImplemented,
853854
};
854855
emitFiles(
855856
notImplementedResolver,

src/compiler/moduleSpecifiers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ namespace ts.moduleSpecifiers {
105105
const moduleSourceFile = getSourceFileOfNode(moduleSymbol.valueDeclaration || getNonAugmentationDeclaration(moduleSymbol));
106106
const modulePaths = getAllModulePaths(importingSourceFile.path, moduleSourceFile.originalFileName, host);
107107

108+
const existingSpecifier = forEach(modulePaths, modulePath => forEach(
109+
host.getFileIncludeReasons().get(toPath(modulePath.path, host.getCurrentDirectory(), info.getCanonicalFileName)),
110+
reason => reason.kind === FileIncludeKind.Import && reason.file === importingSourceFile.path ?
111+
getModuleNameStringLiteralAt(importingSourceFile, reason.index).text :
112+
undefined
113+
));
114+
if (existingSpecifier) return [existingSpecifier];
115+
108116
const preferences = getPreferences(userPreferences, compilerOptions, importingSourceFile);
109117
const importedFileIsInNodeModules = some(modulePaths, p => p.isInNodeModules);
110118

src/compiler/program.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,7 @@ namespace ts {
16491649
getProgramBuildInfo: () => program.getProgramBuildInfo && program.getProgramBuildInfo(),
16501650
getSourceFileFromReference: (file, ref) => program.getSourceFileFromReference(file, ref),
16511651
redirectTargetsMap,
1652+
getFileIncludeReasons: program.getFileIncludeReasons,
16521653
};
16531654
}
16541655

@@ -3973,7 +3974,8 @@ namespace ts {
39733974
return res;
39743975
}
39753976

3976-
function getModuleNameStringLiteralAt({ imports, moduleAugmentations }: SourceFile, index: number): StringLiteralLike {
3977+
/* @internal */
3978+
export function getModuleNameStringLiteralAt({ imports, moduleAugmentations }: SourceFile, index: number): StringLiteralLike {
39773979
if (index < imports.length) return imports[index];
39783980
let augIndex = imports.length;
39793981
for (const aug of moduleAugmentations) {

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7947,6 +7947,7 @@ namespace ts {
79477947
getProjectReferenceRedirect(fileName: string): string | undefined;
79487948
isSourceOfProjectReferenceRedirect(fileName: string): boolean;
79497949
getCompilerOptions(): CompilerOptions;
7950+
getFileIncludeReasons(): MultiMap<Path, FileIncludeReason>;
79507951
}
79517952

79527953
// Note: this used to be deprecated in our public API, but is still used internally

src/services/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,7 @@ namespace ts {
18361836
isSourceOfProjectReferenceRedirect: fileName => program.isSourceOfProjectReferenceRedirect(fileName),
18371837
getCompilerOptions: () => program.getCompilerOptions(),
18381838
getNearestAncestorDirectoryWithPackageJson: maybeBind(host, host.getNearestAncestorDirectoryWithPackageJson),
1839+
getFileIncludeReasons: () => program.getFileIncludeReasons(),
18391840
};
18401841
}
18411842

tests/baselines/reference/tsbuild/declarationEmit/initial-build/when-declaration-file-used-inferred-type-from-referenced-project.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ exports.__esModule = true;
109109
}
110110

111111
//// [/src/packages/pkg2/lib/src/index.d.ts]
112-
export declare function fn4(): import("../../pkg1/src").IThing;
112+
export declare function fn4(): import("@fluentui/pkg1").IThing;
113113

114114

115115
//// [/src/packages/pkg2/lib/src/index.js]
@@ -139,7 +139,7 @@ exports.fn4 = fn4;
139139
},
140140
"../src/index.ts": {
141141
"version": "8515046367-import { IThings } from '@fluentui/pkg1';\nexport function fn4() {\n const a: IThings = { thing1: { a: 'b' } };\n return a.thing1;\n}",
142-
"signature": "-10018297277-export declare function fn4(): import(\"../../pkg1/src\").IThing;\r\n",
142+
"signature": "-9447422063-export declare function fn4(): import(\"@fluentui/pkg1\").IThing;\r\n",
143143
"affectsGlobalScope": false
144144
}
145145
},

0 commit comments

Comments
 (0)