Skip to content

Commit b97eafb

Browse files
Fixed accidentally reused comments between files in the emitter (#61261)
Co-authored-by: michaelm <[email protected]>
1 parent afc4639 commit b97eafb

14 files changed

+374
-10
lines changed

src/compiler/checker.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6207,6 +6207,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
62076207
const context = syntacticContext as NodeBuilderContext;
62086208
if (context.bundled || context.enclosingFile !== getSourceFileOfNode(lit)) {
62096209
let name = lit.text;
6210+
const originalName = name;
62106211
const nodeSymbol = getNodeLinks(parent).resolvedSymbol;
62116212
const meaning = parent.isTypeOf ? SymbolFlags.Value : SymbolFlags.Type;
62126213
const parentSymbol = nodeSymbol
@@ -6227,7 +6228,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
62276228
context.tracker.reportLikelyUnsafeImportRequiredError(name);
62286229
}
62296230
}
6230-
return name;
6231+
if (name !== originalName) {
6232+
return name;
6233+
}
62316234
}
62326235
},
62336236
canReuseTypeNode(context, typeNode) {
@@ -8831,10 +8834,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
88318834
return setTextRange(context, setEmitFlags(name, EmitFlags.NoAsciiEscaping), node);
88328835
}
88338836
const updated = visitEachChildWorker(node, c => attachSymbolToLeftmostIdentifier(c), /*context*/ undefined);
8834-
if (updated !== node) {
8835-
setTextRange(context, updated, node);
8836-
}
8837-
return updated;
8837+
return setTextRange(context, updated, node);
88388838
}
88398839
}
88408840

src/compiler/expressionToTypeNode.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,11 @@ export function createSyntacticTypeNodeBuilder(
435435
if (!resolver.canReuseTypeNode(context, node)) {
436436
return resolver.serializeExistingTypeNode(context, node);
437437
}
438+
const specifier = rewriteModuleSpecifier(node, node.argument.literal);
439+
const literal = specifier === node.argument.literal ? reuseNode(context, node.argument.literal) : specifier;
438440
return factory.updateImportTypeNode(
439441
node,
440-
factory.updateLiteralTypeNode(node.argument, rewriteModuleSpecifier(node, node.argument.literal)),
442+
literal === node.argument.literal ? reuseNode(context, node.argument) : factory.createLiteralTypeNode(literal),
441443
visitNode(node.attributes, visitExistingNodeTreeSymbols, isImportAttributes),
442444
visitNode(node.qualifier, visitExistingNodeTreeSymbols, isEntityName),
443445
visitNodes(node.typeArguments, visitExistingNodeTreeSymbols, isTypeNode),
@@ -612,10 +614,7 @@ export function createSyntacticTypeNodeBuilder(
612614

613615
function rewriteModuleSpecifier(parent: ImportTypeNode, lit: StringLiteral) {
614616
const newName = resolver.getModuleSpecifierOverride(context, parent, lit);
615-
if (newName) {
616-
return setOriginalNode(factory.createStringLiteral(newName), lit);
617-
}
618-
return visitNode(lit, visitExistingNodeTreeSymbols, isStringLiteral)!;
617+
return newName ? setOriginalNode(factory.createStringLiteral(newName), lit) : lit;
619618
}
620619
}
621620
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse1.ts] ////
2+
3+
//// [a.ts]
4+
import { object } from "./obj";
5+
6+
export const _ = object;
7+
8+
///////////
9+
/**
10+
* huh
11+
*/
12+
//// [obj.d.ts]
13+
export declare const object: import("./id").Id<{
14+
foo: import("./id" ).Id<{}>;
15+
}>;
16+
17+
//// [id.d.ts]
18+
export type Id<T> = T;
19+
20+
21+
22+
23+
//// [a.d.ts]
24+
export declare const _: {
25+
foo: import("./id").Id<{}>;
26+
};
27+
/**
28+
* huh
29+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse1.ts] ////
2+
3+
=== a.ts ===
4+
import { object } from "./obj";
5+
>object : Symbol(object, Decl(a.ts, 0, 8))
6+
7+
export const _ = object;
8+
>_ : Symbol(_, Decl(a.ts, 2, 12))
9+
>object : Symbol(object, Decl(a.ts, 0, 8))
10+
11+
///////////
12+
/**
13+
* huh
14+
*/
15+
=== obj.d.ts ===
16+
export declare const object: import("./id").Id<{
17+
>object : Symbol(object, Decl(obj.d.ts, 0, 20))
18+
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))
19+
20+
foo: import("./id" ).Id<{}>;
21+
>foo : Symbol(foo, Decl(obj.d.ts, 0, 48))
22+
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))
23+
24+
}>;
25+
26+
=== id.d.ts ===
27+
export type Id<T> = T;
28+
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))
29+
>T : Symbol(T, Decl(id.d.ts, 0, 15))
30+
>T : Symbol(T, Decl(id.d.ts, 0, 15))
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse1.ts] ////
2+
3+
=== a.ts ===
4+
import { object } from "./obj";
5+
>object : { foo: import("id").Id<{}>; }
6+
> : ^^^^^^^ ^^^
7+
8+
export const _ = object;
9+
>_ : { foo: import("id").Id<{}>; }
10+
> : ^^^^^^^ ^^^
11+
>object : { foo: import("id").Id<{}>; }
12+
> : ^^^^^^^ ^^^
13+
14+
///////////
15+
/**
16+
* huh
17+
*/
18+
=== obj.d.ts ===
19+
export declare const object: import("./id").Id<{
20+
>object : { foo: import("./id").Id<{}>; }
21+
> : ^^^^^^^ ^^^
22+
23+
foo: import("./id" ).Id<{}>;
24+
>foo : {}
25+
> : ^^
26+
27+
}>;
28+
29+
=== id.d.ts ===
30+
export type Id<T> = T;
31+
>Id : T
32+
> : ^
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse2.ts] ////
2+
3+
//// [a.ts]
4+
import { object } from "./obj.ts";
5+
6+
export const _ = object;
7+
8+
///////////
9+
/**
10+
* huh
11+
*/
12+
//// [obj.d.ts]
13+
export declare const object: import("./id.ts").Id<{
14+
foo: import("./id.ts" ).Id<{}>;
15+
}>;
16+
17+
//// [id.d.ts]
18+
export type Id<T> = T;
19+
20+
21+
22+
23+
//// [a.d.ts]
24+
export declare const _: {
25+
foo: import("./id").Id<{}>;
26+
};
27+
/**
28+
* huh
29+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse2.ts] ////
2+
3+
=== a.ts ===
4+
import { object } from "./obj.ts";
5+
>object : Symbol(object, Decl(a.ts, 0, 8))
6+
7+
export const _ = object;
8+
>_ : Symbol(_, Decl(a.ts, 2, 12))
9+
>object : Symbol(object, Decl(a.ts, 0, 8))
10+
11+
///////////
12+
/**
13+
* huh
14+
*/
15+
=== obj.d.ts ===
16+
export declare const object: import("./id.ts").Id<{
17+
>object : Symbol(object, Decl(obj.d.ts, 0, 20))
18+
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))
19+
20+
foo: import("./id.ts" ).Id<{}>;
21+
>foo : Symbol(foo, Decl(obj.d.ts, 0, 51))
22+
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))
23+
24+
}>;
25+
26+
=== id.d.ts ===
27+
export type Id<T> = T;
28+
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))
29+
>T : Symbol(T, Decl(id.d.ts, 0, 15))
30+
>T : Symbol(T, Decl(id.d.ts, 0, 15))
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse2.ts] ////
2+
3+
=== a.ts ===
4+
import { object } from "./obj.ts";
5+
>object : { foo: import("id").Id<{}>; }
6+
> : ^^^^^^^ ^^^
7+
8+
export const _ = object;
9+
>_ : { foo: import("id").Id<{}>; }
10+
> : ^^^^^^^ ^^^
11+
>object : { foo: import("id").Id<{}>; }
12+
> : ^^^^^^^ ^^^
13+
14+
///////////
15+
/**
16+
* huh
17+
*/
18+
=== obj.d.ts ===
19+
export declare const object: import("./id.ts").Id<{
20+
>object : { foo: import("./id.ts").Id<{}>; }
21+
> : ^^^^^^^ ^^^
22+
23+
foo: import("./id.ts" ).Id<{}>;
24+
>foo : {}
25+
> : ^^
26+
27+
}>;
28+
29+
=== id.d.ts ===
30+
export type Id<T> = T;
31+
>Id : T
32+
> : ^
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse3.ts] ////
2+
3+
//// [a.ts]
4+
import { object } from "./obj";
5+
import { id } from "./id";
6+
export const _ = object;
7+
/**
8+
*/
9+
//// [obj.d.ts]
10+
import { id } from "./id";
11+
// ----
12+
export declare const object: id.A<{
13+
foo: id.A<1>
14+
}>;
15+
16+
//// [id.d.ts]
17+
export declare namespace id {
18+
type A<T> = T;
19+
}
20+
21+
22+
23+
//// [a.d.ts]
24+
import { id } from "./id";
25+
export declare const _: {
26+
foo: id.A<1>;
27+
};
28+
/**
29+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse3.ts] ////
2+
3+
=== a.ts ===
4+
import { object } from "./obj";
5+
>object : Symbol(object, Decl(a.ts, 0, 8))
6+
7+
import { id } from "./id";
8+
>id : Symbol(id, Decl(a.ts, 1, 8))
9+
10+
export const _ = object;
11+
>_ : Symbol(_, Decl(a.ts, 2, 12))
12+
>object : Symbol(object, Decl(a.ts, 0, 8))
13+
14+
/**
15+
*/
16+
=== obj.d.ts ===
17+
import { id } from "./id";
18+
>id : Symbol(id, Decl(obj.d.ts, 0, 8))
19+
20+
// ----
21+
export declare const object: id.A<{
22+
>object : Symbol(object, Decl(obj.d.ts, 2, 20))
23+
>id : Symbol(id, Decl(obj.d.ts, 0, 8))
24+
>A : Symbol(id.A, Decl(id.d.ts, 0, 29))
25+
26+
foo: id.A<1>
27+
>foo : Symbol(foo, Decl(obj.d.ts, 2, 35))
28+
>id : Symbol(id, Decl(obj.d.ts, 0, 8))
29+
>A : Symbol(id.A, Decl(id.d.ts, 0, 29))
30+
31+
}>;
32+
33+
=== id.d.ts ===
34+
export declare namespace id {
35+
>id : Symbol(id, Decl(id.d.ts, 0, 0))
36+
37+
type A<T> = T;
38+
>A : Symbol(A, Decl(id.d.ts, 0, 29))
39+
>T : Symbol(T, Decl(id.d.ts, 1, 11))
40+
>T : Symbol(T, Decl(id.d.ts, 1, 11))
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse3.ts] ////
2+
3+
=== a.ts ===
4+
import { object } from "./obj";
5+
>object : { foo: id.A<1>; }
6+
> : ^^^^^^^ ^^^
7+
8+
import { id } from "./id";
9+
>id : any
10+
> : ^^^
11+
12+
export const _ = object;
13+
>_ : { foo: id.A<1>; }
14+
> : ^^^^^^^ ^^^
15+
>object : { foo: id.A<1>; }
16+
> : ^^^^^^^ ^^^
17+
18+
/**
19+
*/
20+
=== obj.d.ts ===
21+
import { id } from "./id";
22+
>id : any
23+
> : ^^^
24+
25+
// ----
26+
export declare const object: id.A<{
27+
>object : { foo: id.A<1>; }
28+
> : ^^^^^^^ ^^^
29+
>id : any
30+
> : ^^^
31+
32+
foo: id.A<1>
33+
>foo : 1
34+
> : ^
35+
>id : any
36+
> : ^^^
37+
38+
}>;
39+
40+
=== id.d.ts ===
41+
export declare namespace id {
42+
type A<T> = T;
43+
>A : T
44+
> : ^
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @strict: true
2+
// @declaration: true
3+
// @emitDeclarationOnly: true
4+
5+
// https://github.com/microsoft/TypeScript/issues/61239
6+
7+
// @filename: a.ts
8+
import { object } from "./obj";
9+
10+
export const _ = object;
11+
12+
///////////
13+
/**
14+
* huh
15+
*/
16+
// @filename: obj.d.ts
17+
export declare const object: import("./id").Id<{
18+
foo: import("./id" ).Id<{}>;
19+
}>;
20+
21+
// @filename: id.d.ts
22+
export type Id<T> = T;

0 commit comments

Comments
 (0)