diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b7281687ad7..fe102a25912 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -112,7 +112,6 @@ import { createEvaluator, createFileDiagnostic, createFlowNode, - createGetCanonicalFileName, createGetSymbolWalker, createModeAwareCacheKey, createModuleNotFoundChain, @@ -347,7 +346,6 @@ import { getPropertyNameFromType, getResolutionDiagnostic, getResolutionModeOverride, - getResolvedExternalModuleName, getResolveJsonModule, getRestParameterElementType, getRootDeclaration, @@ -7668,14 +7666,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return (symbol.escapedName as string).substring(1, (symbol.escapedName as string).length - 1); } } - if (!context.enclosingDeclaration || !context.tracker.moduleResolverHost) { + if (!context.enclosingFile || !context.tracker.moduleResolverHost) { // If there's no context declaration, we can't lookup a non-ambient specifier, so we just use the symbol name if (ambientModuleSymbolRegex.test(symbol.escapedName as string)) { return (symbol.escapedName as string).substring(1, (symbol.escapedName as string).length - 1); } return getSourceFileOfNode(getNonAugmentationDeclaration(symbol)!).fileName; // A resolver may not be provided for baselines and errors - in those cases we use the fileName in full } - const contextFile = getSourceFileOfNode(getOriginalNode(context.enclosingDeclaration)); + const contextFile = context.enclosingFile; const resolutionMode = overrideImportMode || contextFile?.impliedNodeFormat; const cacheKey = createModeAwareCacheKey(contextFile.path, resolutionMode); const links = getSymbolLinks(symbol); @@ -8479,22 +8477,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function rewriteModuleSpecifier(parent: ImportTypeNode, lit: StringLiteral) { - if (context.bundled) { - if (context.tracker && context.tracker.moduleResolverHost) { - const targetFile = getExternalModuleFileFromDeclaration(parent); - if (targetFile) { - const getCanonicalFileName = createGetCanonicalFileName(!!host.useCaseSensitiveFileNames); - const resolverHost = { - getCanonicalFileName, - getCurrentDirectory: () => context.tracker.moduleResolverHost!.getCurrentDirectory(), - getCommonSourceDirectory: () => context.tracker.moduleResolverHost!.getCommonSourceDirectory(), - }; - const newName = getResolvedExternalModuleName(resolverHost, targetFile); - return factory.createStringLiteral(newName); + if (context.bundled || context.enclosingFile !== getSourceFileOfNode(lit)) { + const targetFile = getExternalModuleFileFromDeclaration(parent); + if (targetFile) { + const newName = getSpecifierForModuleSymbol(targetFile.symbol, context); + if (newName !== lit.text) { + return setOriginalNode(factory.createStringLiteral(newName), lit); } } } - return lit; + return visitNode(lit, visitExistingNodeTreeSymbols, isStringLiteral)!; } } } diff --git a/tests/baselines/reference/callbackCrossModule.types b/tests/baselines/reference/callbackCrossModule.types index 2e5388420d6..062a8a09257 100644 --- a/tests/baselines/reference/callbackCrossModule.types +++ b/tests/baselines/reference/callbackCrossModule.types @@ -36,7 +36,7 @@ function C() { === use.js === /** @param {import('./mod1').Con} k */ function f(k) { ->f : (k: import('./mod1').Con) => any +>f : (k: import("./mod1").Con) => any > : ^ ^^ ^^^^^^^^ >k : import("mod1").Con > : ^^^^^^^^^^^^^^^^^^ diff --git a/tests/baselines/reference/declarationEmitCrossFileCopiedGeneratedImportType.js b/tests/baselines/reference/declarationEmitCrossFileCopiedGeneratedImportType.js new file mode 100644 index 00000000000..f0fcefa965c --- /dev/null +++ b/tests/baselines/reference/declarationEmitCrossFileCopiedGeneratedImportType.js @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/declarationEmitCrossFileCopiedGeneratedImportType.ts] //// + +//// [index.d.ts] +export declare class Foo extends Number { + private _; +} +//// [index.d.ts] +import { Foo } from "../projA"; +export declare const f: (foo: Foo) => boolean; +//// [index.d.ts] +export declare const e: { + f: (foo: import("../projA").Foo) => boolean; +}; +//// [index.ts] +import {e} from "../projC"; + +export const d = {e}; + +//// [index.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.d = void 0; +var projC_1 = require("../projC"); +exports.d = { e: projC_1.e }; + + +//// [index.d.ts] +export declare const d: { + e: { + f: (foo: import("../projA").Foo) => boolean; + }; +}; diff --git a/tests/baselines/reference/declarationEmitCrossFileCopiedGeneratedImportType.symbols b/tests/baselines/reference/declarationEmitCrossFileCopiedGeneratedImportType.symbols new file mode 100644 index 00000000000..f4ead3344c3 --- /dev/null +++ b/tests/baselines/reference/declarationEmitCrossFileCopiedGeneratedImportType.symbols @@ -0,0 +1,37 @@ +//// [tests/cases/compiler/declarationEmitCrossFileCopiedGeneratedImportType.ts] //// + +=== projA/index.d.ts === +export declare class Foo extends Number { +>Foo : Symbol(Foo, Decl(index.d.ts, 0, 0)) +>Number : Symbol(Number, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + private _; +>_ : Symbol(Foo._, Decl(index.d.ts, 0, 41)) +} +=== projB/index.d.ts === +import { Foo } from "../projA"; +>Foo : Symbol(Foo, Decl(index.d.ts, 0, 8)) + +export declare const f: (foo: Foo) => boolean; +>f : Symbol(f, Decl(index.d.ts, 1, 20)) +>foo : Symbol(foo, Decl(index.d.ts, 1, 25)) +>Foo : Symbol(Foo, Decl(index.d.ts, 0, 8)) + +=== projC/index.d.ts === +export declare const e: { +>e : Symbol(e, Decl(index.d.ts, 0, 20)) + + f: (foo: import("../projA").Foo) => boolean; +>f : Symbol(f, Decl(index.d.ts, 0, 25)) +>foo : Symbol(foo, Decl(index.d.ts, 1, 8)) +>Foo : Symbol(Foo, Decl(index.d.ts, 0, 0)) + +}; +=== projD/index.ts === +import {e} from "../projC"; +>e : Symbol(e, Decl(index.ts, 0, 8)) + +export const d = {e}; +>d : Symbol(d, Decl(index.ts, 2, 12)) +>e : Symbol(e, Decl(index.ts, 2, 18)) + diff --git a/tests/baselines/reference/declarationEmitCrossFileCopiedGeneratedImportType.types b/tests/baselines/reference/declarationEmitCrossFileCopiedGeneratedImportType.types new file mode 100644 index 00000000000..1dfaa90f55f --- /dev/null +++ b/tests/baselines/reference/declarationEmitCrossFileCopiedGeneratedImportType.types @@ -0,0 +1,48 @@ +//// [tests/cases/compiler/declarationEmitCrossFileCopiedGeneratedImportType.ts] //// + +=== projA/index.d.ts === +export declare class Foo extends Number { +>Foo : Foo +> : ^^^ +>Number : Number +> : ^^^^^^ + + private _; +>_ : any +} +=== projB/index.d.ts === +import { Foo } from "../projA"; +>Foo : typeof Foo +> : ^^^^^^^^^^ + +export declare const f: (foo: Foo) => boolean; +>f : (foo: Foo) => boolean +> : ^ ^^ ^^^^^ +>foo : Foo +> : ^^^ + +=== projC/index.d.ts === +export declare const e: { +>e : { f: (foo: import("../projA").Foo) => boolean; } +> : ^^^^^ ^^^ + + f: (foo: import("../projA").Foo) => boolean; +>f : (foo: import("../projA").Foo) => boolean +> : ^ ^^ ^^^^^ +>foo : import("projA/index").Foo +> : ^^^^^^^^^^^^^^^^^^^^^^^^^ + +}; +=== projD/index.ts === +import {e} from "../projC"; +>e : { f: (foo: import("projA/index").Foo) => boolean; } +> : ^^^^^^ ^^ ^^^^^^^^^^^^^^^ + +export const d = {e}; +>d : { e: { f: (foo: import("projA/index").Foo) => boolean; }; } +> : ^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^ +>{e} : { e: { f: (foo: import("projA/index").Foo) => boolean; }; } +> : ^^^^^ ^^^ +>e : { f: (foo: import("projA/index").Foo) => boolean; } +> : ^^^^^^ ^^ ^^^^^^^^^^^^^^^ + diff --git a/tests/baselines/reference/jsDeclarationsClassImplementsGenericsSerialization.js b/tests/baselines/reference/jsDeclarationsClassImplementsGenericsSerialization.js index 285cdae7b42..04fbe98c546 100644 --- a/tests/baselines/reference/jsDeclarationsClassImplementsGenericsSerialization.js +++ b/tests/baselines/reference/jsDeclarationsClassImplementsGenericsSerialization.js @@ -68,4 +68,4 @@ export class Encoder implements IEncoder { */ encode(value: T): Uint8Array; } -export type IEncoder = import('./interface').Encoder; +export type IEncoder = import("./interface").Encoder; diff --git a/tests/baselines/reference/jsDeclarationsParameterTagReusesInputNodeInEmit2.js b/tests/baselines/reference/jsDeclarationsParameterTagReusesInputNodeInEmit2.js index 15357997c00..3f566b06685 100644 --- a/tests/baselines/reference/jsDeclarationsParameterTagReusesInputNodeInEmit2.js +++ b/tests/baselines/reference/jsDeclarationsParameterTagReusesInputNodeInEmit2.js @@ -63,4 +63,4 @@ declare class Base { * @returns {InstanceType} */ declare function test(base: InstanceType): InstanceType; -type BaseFactory = typeof import('./base'); +type BaseFactory = typeof import("./base"); diff --git a/tests/baselines/reference/jsDeclarationsTypedefPropertyAndExportAssignment.js b/tests/baselines/reference/jsDeclarationsTypedefPropertyAndExportAssignment.js index 3be4e5494ee..6e90297a1e4 100644 --- a/tests/baselines/reference/jsDeclarationsTypedefPropertyAndExportAssignment.js +++ b/tests/baselines/reference/jsDeclarationsTypedefPropertyAndExportAssignment.js @@ -148,7 +148,7 @@ declare class MainThreadTasks { declare namespace MainThreadTasks { export { TaskGroup, TaskNode, PriorTaskData }; } -type TaskGroup = import('./module.js').TaskGroup; +type TaskGroup = import("./module.js").TaskGroup; type TaskNode = { children: TaskNode[]; parent: TaskNode | undefined; diff --git a/tests/baselines/reference/jsDeclarationsUniqueSymbolUsage.js b/tests/baselines/reference/jsDeclarationsUniqueSymbolUsage.js index 5b931e62911..9c929017db4 100644 --- a/tests/baselines/reference/jsDeclarationsUniqueSymbolUsage.js +++ b/tests/baselines/reference/jsDeclarationsUniqueSymbolUsage.js @@ -28,4 +28,4 @@ export type WithSymbol = { * @returns {import('./a').WithSymbol} * @param {import('./a').WithSymbol} value */ -export function b(value: import('./a').WithSymbol): import('./a').WithSymbol; +export function b(value: import("./a").WithSymbol): import("./a").WithSymbol; diff --git a/tests/baselines/reference/jsDeclarationsUniqueSymbolUsage.types b/tests/baselines/reference/jsDeclarationsUniqueSymbolUsage.types index efd0cf4b2f0..23fd77525e7 100644 --- a/tests/baselines/reference/jsDeclarationsUniqueSymbolUsage.types +++ b/tests/baselines/reference/jsDeclarationsUniqueSymbolUsage.types @@ -20,7 +20,7 @@ export const kSymbol = Symbol("my-symbol"); * @param {import('./a').WithSymbol} value */ export function b(value) { ->b : (value: import('./a').WithSymbol) => import('./a').WithSymbol +>b : (value: import("./a").WithSymbol) => import("./a").WithSymbol > : ^ ^^ ^^^^^ >value : import("a").WithSymbol > : ^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/baselines/reference/jsdocThisType.types b/tests/baselines/reference/jsdocThisType.types index b51f56b65cf..6df5e8a4a33 100644 --- a/tests/baselines/reference/jsdocThisType.types +++ b/tests/baselines/reference/jsdocThisType.types @@ -50,9 +50,9 @@ export function f2() { /** @type {(this: import('./types').Foo) => void} */ export const f3 = function() { ->f3 : (this: import('./types').Foo) => void +>f3 : (this: import("./types").Foo) => void > : ^ ^^ ^^^^^^^^^ ->function() { this.test();} : (this: import('./types').Foo) => void +>function() { this.test();} : (this: import("./types").Foo) => void > : ^ ^^ ^^^^^^^^^ this.test(); @@ -68,7 +68,7 @@ export const f3 = function() { /** @type {(this: import('./types').Foo) => void} */ export function f4() { ->f4 : (this: import('./types').Foo) => void +>f4 : (this: import("./types").Foo) => void > : ^ ^^ ^^^^^ this.test(); @@ -84,9 +84,9 @@ export function f4() { /** @type {function(this: import('./types').Foo): void} */ export const f5 = function() { ->f5 : (this: import('./types').Foo) => void +>f5 : (this: import("./types").Foo) => void > : ^ ^^ ^^^^^^^^^ ->function() { this.test();} : (this: import('./types').Foo) => void +>function() { this.test();} : (this: import("./types").Foo) => void > : ^ ^^ ^^^^^^^^^ this.test(); @@ -102,7 +102,7 @@ export const f5 = function() { /** @type {function(this: import('./types').Foo): void} */ export function f6() { ->f6 : (this: import('./types').Foo) => void +>f6 : (this: import("./types").Foo) => void > : ^ ^^ ^^^^^ this.test(); diff --git a/tests/baselines/reference/moduleExportAssignment7.types b/tests/baselines/reference/moduleExportAssignment7.types index d33dc4b3751..8461b55622f 100644 --- a/tests/baselines/reference/moduleExportAssignment7.types +++ b/tests/baselines/reference/moduleExportAssignment7.types @@ -227,7 +227,7 @@ function jsvalues(a, b, c, d, e, f, g) { === index.ts === function types( ->types : (a: any, b: any, c: any, d: any, e: any, f: import('./mod').buz, g: any) => any +>types : (a: any, b: any, c: any, d: any, e: any, f: import("./mod").buz, g: any) => any > : ^ ^^^^^^^ ^^^^^^^ ^^^^^^^ ^^^^^^^ ^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^ a: import('./mod').Thing, @@ -309,7 +309,7 @@ function types( } function values( ->values : (a: typeof import('./mod').Thing, b: typeof import('./mod').AnotherThing, c: typeof import('./mod').foo, d: typeof import('./mod').qux, e: typeof import('./mod').baz, f: any, g: typeof import('./mod').literal) => any +>values : (a: typeof import("./mod").Thing, b: typeof import("./mod").AnotherThing, c: typeof import("./mod").foo, d: typeof import("./mod").qux, e: typeof import("./mod").baz, f: any, g: typeof import("./mod").literal) => any > : ^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^ ^^ ^^^^^^^^ a: typeof import('./mod').Thing, diff --git a/tests/baselines/reference/privateNamesUnique-2.types b/tests/baselines/reference/privateNamesUnique-2.types index 18d57c78237..6dabf47b307 100644 --- a/tests/baselines/reference/privateNamesUnique-2.types +++ b/tests/baselines/reference/privateNamesUnique-2.types @@ -65,12 +65,12 @@ const b = new B(); a.copy(b); // error >a.copy(b) : void > : ^^^^ ->a.copy : (other: import(} fro).Foo) => void -> : ^ ^^ ^^^^^^^^^ +>a.copy : (other: import("b").Foo) => void +> : ^ ^^ ^^^^^^^^^ >a : A > : ^ ->copy : (other: import(} fro).Foo) => void -> : ^ ^^ ^^^^^^^^^ +>copy : (other: import("b").Foo) => void +> : ^ ^^ ^^^^^^^^^ >b : B > : ^ diff --git a/tests/baselines/reference/spuriousCircularityOnTypeImport.types b/tests/baselines/reference/spuriousCircularityOnTypeImport.types index b32037b3306..675ed82b6c5 100644 --- a/tests/baselines/reference/spuriousCircularityOnTypeImport.types +++ b/tests/baselines/reference/spuriousCircularityOnTypeImport.types @@ -21,7 +21,7 @@ export type SelectorMap unkno }; export declare const value2: { ->value2 : { sliceSelectors: >(selectorsBySlice: FuncMap) => { [P in keyof FuncMap]: Parameters; }; } +>value2 : { sliceSelectors: >(selectorsBySlice: FuncMap) => { [P in keyof FuncMap]: Parameters; }; } > : ^^^^^^^^^^^^^^^^^^ ^^^ sliceSelectors: >(selectorsBySlice: FuncMap) => { [P in keyof FuncMap]: Parameters }; diff --git a/tests/cases/compiler/declarationEmitCrossFileCopiedGeneratedImportType.ts b/tests/cases/compiler/declarationEmitCrossFileCopiedGeneratedImportType.ts new file mode 100644 index 00000000000..7a2e1f2ed48 --- /dev/null +++ b/tests/cases/compiler/declarationEmitCrossFileCopiedGeneratedImportType.ts @@ -0,0 +1,16 @@ +// @declaration: true +// @filename: projA/index.d.ts +export declare class Foo extends Number { + private _; +} +// @filename: projB/index.d.ts +import { Foo } from "../projA"; +export declare const f: (foo: Foo) => boolean; +// @filename: projC/index.d.ts +export declare const e: { + f: (foo: import("../projA").Foo) => boolean; +}; +// @filename: projD/index.ts +import {e} from "../projC"; + +export const d = {e}; \ No newline at end of file