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
> : ^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)