Skip to content

Commit 6092c2d

Browse files
authored
Add missing recursive vistor on copied import type nodes (microsoft#58165)
1 parent 4e29496 commit 6092c2d

15 files changed

+161
-36
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ import {
112112
createEvaluator,
113113
createFileDiagnostic,
114114
createFlowNode,
115-
createGetCanonicalFileName,
116115
createGetSymbolWalker,
117116
createModeAwareCacheKey,
118117
createModuleNotFoundChain,
@@ -347,7 +346,6 @@ import {
347346
getPropertyNameFromType,
348347
getResolutionDiagnostic,
349348
getResolutionModeOverride,
350-
getResolvedExternalModuleName,
351349
getResolveJsonModule,
352350
getRestParameterElementType,
353351
getRootDeclaration,
@@ -7668,14 +7666,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
76687666
return (symbol.escapedName as string).substring(1, (symbol.escapedName as string).length - 1);
76697667
}
76707668
}
7671-
if (!context.enclosingDeclaration || !context.tracker.moduleResolverHost) {
7669+
if (!context.enclosingFile || !context.tracker.moduleResolverHost) {
76727670
// If there's no context declaration, we can't lookup a non-ambient specifier, so we just use the symbol name
76737671
if (ambientModuleSymbolRegex.test(symbol.escapedName as string)) {
76747672
return (symbol.escapedName as string).substring(1, (symbol.escapedName as string).length - 1);
76757673
}
76767674
return getSourceFileOfNode(getNonAugmentationDeclaration(symbol)!).fileName; // A resolver may not be provided for baselines and errors - in those cases we use the fileName in full
76777675
}
7678-
const contextFile = getSourceFileOfNode(getOriginalNode(context.enclosingDeclaration));
7676+
const contextFile = context.enclosingFile;
76797677
const resolutionMode = overrideImportMode || contextFile?.impliedNodeFormat;
76807678
const cacheKey = createModeAwareCacheKey(contextFile.path, resolutionMode);
76817679
const links = getSymbolLinks(symbol);
@@ -8479,22 +8477,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
84798477
}
84808478

84818479
function rewriteModuleSpecifier(parent: ImportTypeNode, lit: StringLiteral) {
8482-
if (context.bundled) {
8483-
if (context.tracker && context.tracker.moduleResolverHost) {
8484-
const targetFile = getExternalModuleFileFromDeclaration(parent);
8485-
if (targetFile) {
8486-
const getCanonicalFileName = createGetCanonicalFileName(!!host.useCaseSensitiveFileNames);
8487-
const resolverHost = {
8488-
getCanonicalFileName,
8489-
getCurrentDirectory: () => context.tracker.moduleResolverHost!.getCurrentDirectory(),
8490-
getCommonSourceDirectory: () => context.tracker.moduleResolverHost!.getCommonSourceDirectory(),
8491-
};
8492-
const newName = getResolvedExternalModuleName(resolverHost, targetFile);
8493-
return factory.createStringLiteral(newName);
8480+
if (context.bundled || context.enclosingFile !== getSourceFileOfNode(lit)) {
8481+
const targetFile = getExternalModuleFileFromDeclaration(parent);
8482+
if (targetFile) {
8483+
const newName = getSpecifierForModuleSymbol(targetFile.symbol, context);
8484+
if (newName !== lit.text) {
8485+
return setOriginalNode(factory.createStringLiteral(newName), lit);
84948486
}
84958487
}
84968488
}
8497-
return lit;
8489+
return visitNode(lit, visitExistingNodeTreeSymbols, isStringLiteral)!;
84988490
}
84998491
}
85008492
}

tests/baselines/reference/callbackCrossModule.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function C() {
3636
=== use.js ===
3737
/** @param {import('./mod1').Con} k */
3838
function f(k) {
39-
>f : (k: import('./mod1').Con) => any
39+
>f : (k: import("./mod1").Con) => any
4040
> : ^ ^^ ^^^^^^^^
4141
>k : import("mod1").Con
4242
> : ^^^^^^^^^^^^^^^^^^
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//// [tests/cases/compiler/declarationEmitCrossFileCopiedGeneratedImportType.ts] ////
2+
3+
//// [index.d.ts]
4+
export declare class Foo extends Number {
5+
private _;
6+
}
7+
//// [index.d.ts]
8+
import { Foo } from "../projA";
9+
export declare const f: (foo: Foo) => boolean;
10+
//// [index.d.ts]
11+
export declare const e: {
12+
f: (foo: import("../projA").Foo) => boolean;
13+
};
14+
//// [index.ts]
15+
import {e} from "../projC";
16+
17+
export const d = {e};
18+
19+
//// [index.js]
20+
"use strict";
21+
Object.defineProperty(exports, "__esModule", { value: true });
22+
exports.d = void 0;
23+
var projC_1 = require("../projC");
24+
exports.d = { e: projC_1.e };
25+
26+
27+
//// [index.d.ts]
28+
export declare const d: {
29+
e: {
30+
f: (foo: import("../projA").Foo) => boolean;
31+
};
32+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// [tests/cases/compiler/declarationEmitCrossFileCopiedGeneratedImportType.ts] ////
2+
3+
=== projA/index.d.ts ===
4+
export declare class Foo extends Number {
5+
>Foo : Symbol(Foo, Decl(index.d.ts, 0, 0))
6+
>Number : Symbol(Number, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
7+
8+
private _;
9+
>_ : Symbol(Foo._, Decl(index.d.ts, 0, 41))
10+
}
11+
=== projB/index.d.ts ===
12+
import { Foo } from "../projA";
13+
>Foo : Symbol(Foo, Decl(index.d.ts, 0, 8))
14+
15+
export declare const f: (foo: Foo) => boolean;
16+
>f : Symbol(f, Decl(index.d.ts, 1, 20))
17+
>foo : Symbol(foo, Decl(index.d.ts, 1, 25))
18+
>Foo : Symbol(Foo, Decl(index.d.ts, 0, 8))
19+
20+
=== projC/index.d.ts ===
21+
export declare const e: {
22+
>e : Symbol(e, Decl(index.d.ts, 0, 20))
23+
24+
f: (foo: import("../projA").Foo) => boolean;
25+
>f : Symbol(f, Decl(index.d.ts, 0, 25))
26+
>foo : Symbol(foo, Decl(index.d.ts, 1, 8))
27+
>Foo : Symbol(Foo, Decl(index.d.ts, 0, 0))
28+
29+
};
30+
=== projD/index.ts ===
31+
import {e} from "../projC";
32+
>e : Symbol(e, Decl(index.ts, 0, 8))
33+
34+
export const d = {e};
35+
>d : Symbol(d, Decl(index.ts, 2, 12))
36+
>e : Symbol(e, Decl(index.ts, 2, 18))
37+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//// [tests/cases/compiler/declarationEmitCrossFileCopiedGeneratedImportType.ts] ////
2+
3+
=== projA/index.d.ts ===
4+
export declare class Foo extends Number {
5+
>Foo : Foo
6+
> : ^^^
7+
>Number : Number
8+
> : ^^^^^^
9+
10+
private _;
11+
>_ : any
12+
}
13+
=== projB/index.d.ts ===
14+
import { Foo } from "../projA";
15+
>Foo : typeof Foo
16+
> : ^^^^^^^^^^
17+
18+
export declare const f: (foo: Foo) => boolean;
19+
>f : (foo: Foo) => boolean
20+
> : ^ ^^ ^^^^^
21+
>foo : Foo
22+
> : ^^^
23+
24+
=== projC/index.d.ts ===
25+
export declare const e: {
26+
>e : { f: (foo: import("../projA").Foo) => boolean; }
27+
> : ^^^^^ ^^^
28+
29+
f: (foo: import("../projA").Foo) => boolean;
30+
>f : (foo: import("../projA").Foo) => boolean
31+
> : ^ ^^ ^^^^^
32+
>foo : import("projA/index").Foo
33+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
34+
35+
};
36+
=== projD/index.ts ===
37+
import {e} from "../projC";
38+
>e : { f: (foo: import("projA/index").Foo) => boolean; }
39+
> : ^^^^^^ ^^ ^^^^^^^^^^^^^^^
40+
41+
export const d = {e};
42+
>d : { e: { f: (foo: import("projA/index").Foo) => boolean; }; }
43+
> : ^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^
44+
>{e} : { e: { f: (foo: import("projA/index").Foo) => boolean; }; }
45+
> : ^^^^^ ^^^
46+
>e : { f: (foo: import("projA/index").Foo) => boolean; }
47+
> : ^^^^^^ ^^ ^^^^^^^^^^^^^^^
48+

tests/baselines/reference/jsDeclarationsClassImplementsGenericsSerialization.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ export class Encoder<T> implements IEncoder<T> {
6868
*/
6969
encode(value: T): Uint8Array;
7070
}
71-
export type IEncoder<T> = import('./interface').Encoder<T>;
71+
export type IEncoder<T> = import("./interface").Encoder<T>;

tests/baselines/reference/jsDeclarationsParameterTagReusesInputNodeInEmit2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ declare class Base {
6363
* @returns {InstanceType<BaseFactory["Base"]>}
6464
*/
6565
declare function test(base: InstanceType<BaseFactory["Base"]>): InstanceType<BaseFactory["Base"]>;
66-
type BaseFactory = typeof import('./base');
66+
type BaseFactory = typeof import("./base");

tests/baselines/reference/jsDeclarationsTypedefPropertyAndExportAssignment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ declare class MainThreadTasks {
148148
declare namespace MainThreadTasks {
149149
export { TaskGroup, TaskNode, PriorTaskData };
150150
}
151-
type TaskGroup = import('./module.js').TaskGroup;
151+
type TaskGroup = import("./module.js").TaskGroup;
152152
type TaskNode = {
153153
children: TaskNode[];
154154
parent: TaskNode | undefined;

tests/baselines/reference/jsDeclarationsUniqueSymbolUsage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ export type WithSymbol = {
2828
* @returns {import('./a').WithSymbol}
2929
* @param {import('./a').WithSymbol} value
3030
*/
31-
export function b(value: import('./a').WithSymbol): import('./a').WithSymbol;
31+
export function b(value: import("./a").WithSymbol): import("./a").WithSymbol;

tests/baselines/reference/jsDeclarationsUniqueSymbolUsage.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const kSymbol = Symbol("my-symbol");
2020
* @param {import('./a').WithSymbol} value
2121
*/
2222
export function b(value) {
23-
>b : (value: import('./a').WithSymbol) => import('./a').WithSymbol
23+
>b : (value: import("./a").WithSymbol) => import("./a").WithSymbol
2424
> : ^ ^^ ^^^^^
2525
>value : import("a").WithSymbol
2626
> : ^^^^^^^^^^^^^^^^^^^^^^

tests/baselines/reference/jsdocThisType.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ export function f2() {
5050

5151
/** @type {(this: import('./types').Foo) => void} */
5252
export const f3 = function() {
53-
>f3 : (this: import('./types').Foo) => void
53+
>f3 : (this: import("./types").Foo) => void
5454
> : ^ ^^ ^^^^^^^^^
55-
>function() { this.test();} : (this: import('./types').Foo) => void
55+
>function() { this.test();} : (this: import("./types").Foo) => void
5656
> : ^ ^^ ^^^^^^^^^
5757

5858
this.test();
@@ -68,7 +68,7 @@ export const f3 = function() {
6868

6969
/** @type {(this: import('./types').Foo) => void} */
7070
export function f4() {
71-
>f4 : (this: import('./types').Foo) => void
71+
>f4 : (this: import("./types").Foo) => void
7272
> : ^ ^^ ^^^^^
7373

7474
this.test();
@@ -84,9 +84,9 @@ export function f4() {
8484

8585
/** @type {function(this: import('./types').Foo): void} */
8686
export const f5 = function() {
87-
>f5 : (this: import('./types').Foo) => void
87+
>f5 : (this: import("./types").Foo) => void
8888
> : ^ ^^ ^^^^^^^^^
89-
>function() { this.test();} : (this: import('./types').Foo) => void
89+
>function() { this.test();} : (this: import("./types").Foo) => void
9090
> : ^ ^^ ^^^^^^^^^
9191

9292
this.test();
@@ -102,7 +102,7 @@ export const f5 = function() {
102102

103103
/** @type {function(this: import('./types').Foo): void} */
104104
export function f6() {
105-
>f6 : (this: import('./types').Foo) => void
105+
>f6 : (this: import("./types").Foo) => void
106106
> : ^ ^^ ^^^^^
107107

108108
this.test();

tests/baselines/reference/moduleExportAssignment7.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function jsvalues(a, b, c, d, e, f, g) {
227227

228228
=== index.ts ===
229229
function types(
230-
>types : (a: any, b: any, c: any, d: any, e: any, f: import('./mod').buz, g: any) => any
230+
>types : (a: any, b: any, c: any, d: any, e: any, f: import("./mod").buz, g: any) => any
231231
> : ^ ^^^^^^^ ^^^^^^^ ^^^^^^^ ^^^^^^^ ^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^
232232

233233
a: import('./mod').Thing,
@@ -309,7 +309,7 @@ function types(
309309
}
310310

311311
function values(
312-
>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
312+
>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
313313
> : ^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^ ^^ ^^^^^^^^
314314

315315
a: typeof import('./mod').Thing,

tests/baselines/reference/privateNamesUnique-2.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ const b = new B();
6565
a.copy(b); // error
6666
>a.copy(b) : void
6767
> : ^^^^
68-
>a.copy : (other: import(} fro).Foo) => void
69-
> : ^ ^^ ^^^^^^^^^
68+
>a.copy : (other: import("b").Foo) => void
69+
> : ^ ^^ ^^^^^^^^^
7070
>a : A
7171
> : ^
72-
>copy : (other: import(} fro).Foo) => void
73-
> : ^ ^^ ^^^^^^^^^
72+
>copy : (other: import("b").Foo) => void
73+
> : ^ ^^ ^^^^^^^^^
7474
>b : B
7575
> : ^
7676

tests/baselines/reference/spuriousCircularityOnTypeImport.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type SelectorMap<T extends Record<string, (...params: unknown[]) => unkno
2121
};
2222

2323
export declare const value2: {
24-
>value2 : { sliceSelectors: <FuncMap extends import('./types').SelectorMap<FuncMap>>(selectorsBySlice: FuncMap) => { [P in keyof FuncMap]: Parameters<FuncMap[P]>; }; }
24+
>value2 : { sliceSelectors: <FuncMap extends import("./types").SelectorMap<FuncMap>>(selectorsBySlice: FuncMap) => { [P in keyof FuncMap]: Parameters<FuncMap[P]>; }; }
2525
> : ^^^^^^^^^^^^^^^^^^ ^^^
2626

2727
sliceSelectors: <FuncMap extends import('./types').SelectorMap<FuncMap>>(selectorsBySlice: FuncMap) => { [P in keyof FuncMap]: Parameters<FuncMap[P]> };
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @declaration: true
2+
// @filename: projA/index.d.ts
3+
export declare class Foo extends Number {
4+
private _;
5+
}
6+
// @filename: projB/index.d.ts
7+
import { Foo } from "../projA";
8+
export declare const f: (foo: Foo) => boolean;
9+
// @filename: projC/index.d.ts
10+
export declare const e: {
11+
f: (foo: import("../projA").Foo) => boolean;
12+
};
13+
// @filename: projD/index.ts
14+
import {e} from "../projC";
15+
16+
export const d = {e};

0 commit comments

Comments
 (0)