Skip to content

Fixed accidentally reused comments between files in the emitter #61261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6207,6 +6207,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const context = syntacticContext as NodeBuilderContext;
if (context.bundled || context.enclosingFile !== getSourceFileOfNode(lit)) {
let name = lit.text;
const originalName = name;
const nodeSymbol = getNodeLinks(parent).resolvedSymbol;
const meaning = parent.isTypeOf ? SymbolFlags.Value : SymbolFlags.Type;
const parentSymbol = nodeSymbol
Expand All @@ -6227,7 +6228,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
context.tracker.reportLikelyUnsafeImportRequiredError(name);
}
}
return name;
if (name !== originalName) {
return name;
}
}
},
canReuseTypeNode(context, typeNode) {
Expand Down Expand Up @@ -8831,10 +8834,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return setTextRange(context, setEmitFlags(name, EmitFlags.NoAsciiEscaping), node);
}
const updated = visitEachChildWorker(node, c => attachSymbolToLeftmostIdentifier(c), /*context*/ undefined);
if (updated !== node) {
setTextRange(context, updated, node);
}
return updated;
return setTextRange(context, updated, node);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The containing function was only cloning the leftmost identifier in a qualified name, leaving the other ones intact - but they could also be from a different source file than the target file. So when the leftmost one gets updated, the right ones need to be (potentially) cloned too.

But even the node path containing the leftmost identifier wasn't handled quite right here because that the return value of setTextRange was ignored.

}
}

Expand Down
9 changes: 4 additions & 5 deletions src/compiler/expressionToTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,11 @@ export function createSyntacticTypeNodeBuilder(
if (!resolver.canReuseTypeNode(context, node)) {
return resolver.serializeExistingTypeNode(context, node);
}
const specifier = rewriteModuleSpecifier(node, node.argument.literal);
const literal = specifier === node.argument.literal ? reuseNode(context, node.argument.literal) : specifier;
return factory.updateImportTypeNode(
node,
factory.updateLiteralTypeNode(node.argument, rewriteModuleSpecifier(node, node.argument.literal)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was caused by factory.updateLiteralTypeNode calling its setTextRange. This one is specifically different from setTextRange used by reuseNode (check the comment above setTextRange in the checker.ts).

But it also means that likely all factory.update* methods used in this file here are susceptible for similar bugs. At least some of them are using the internal update function that calls setTextRange. There is a chance though this bug was extra tricky because LiteralType and StringLiteral here have the very same positions and only StringLiteral was correctly stripped from its positions before. So maybe the other update methods are safe-ish.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The factories doing anything with locations sounds very very cursed; but I can very much see update being used all over the place inside of the factory. So, not sure what to think there. I'd personally prefer if we didn't set ranges on nodes that didn't come out of real parsed source.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could setCommentRange instead of setTextRange, but setTextRange also gets the sourcemap locations right for declaration maps, in addition to comments, so is actually what we want. The complexities around when we can and can't actually copy the range are why setTextRange is overridden within the nodebuilder nowadays. (Specifically, we can't copy the location if it's from another file than the one for the tree it's being copied into.) It's definitely unfortunate that some uses in expressionToTypeNode fell through the cracks when it got pulled out, though.

literal === node.argument.literal ? reuseNode(context, node.argument) : factory.createLiteralTypeNode(literal),
visitNode(node.attributes, visitExistingNodeTreeSymbols, isImportAttributes),
visitNode(node.qualifier, visitExistingNodeTreeSymbols, isEntityName),
visitNodes(node.typeArguments, visitExistingNodeTreeSymbols, isTypeNode),
Expand Down Expand Up @@ -612,10 +614,7 @@ export function createSyntacticTypeNodeBuilder(

function rewriteModuleSpecifier(parent: ImportTypeNode, lit: StringLiteral) {
const newName = resolver.getModuleSpecifierOverride(context, parent, lit);
if (newName) {
return setOriginalNode(factory.createStringLiteral(newName), lit);
}
return visitNode(lit, visitExistingNodeTreeSymbols, isStringLiteral)!;
return newName ? setOriginalNode(factory.createStringLiteral(newName), lit) : lit;
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions tests/baselines/reference/declarationEmitNoInvalidCommentReuse1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse1.ts] ////

//// [a.ts]
import { object } from "./obj";

export const _ = object;

///////////
/**
* huh
*/
//// [obj.d.ts]
export declare const object: import("./id").Id<{
foo: import("./id" ).Id<{}>;
}>;

//// [id.d.ts]
export type Id<T> = T;




//// [a.d.ts]
export declare const _: {
foo: import("./id").Id<{}>;
};
/**
* huh
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse1.ts] ////

=== a.ts ===
import { object } from "./obj";
>object : Symbol(object, Decl(a.ts, 0, 8))

export const _ = object;
>_ : Symbol(_, Decl(a.ts, 2, 12))
>object : Symbol(object, Decl(a.ts, 0, 8))

///////////
/**
* huh
*/
=== obj.d.ts ===
export declare const object: import("./id").Id<{
>object : Symbol(object, Decl(obj.d.ts, 0, 20))
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))

foo: import("./id" ).Id<{}>;
>foo : Symbol(foo, Decl(obj.d.ts, 0, 48))
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))

}>;

=== id.d.ts ===
export type Id<T> = T;
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))
>T : Symbol(T, Decl(id.d.ts, 0, 15))
>T : Symbol(T, Decl(id.d.ts, 0, 15))

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse1.ts] ////

=== a.ts ===
import { object } from "./obj";
>object : { foo: import("id").Id<{}>; }
> : ^^^^^^^ ^^^

export const _ = object;
>_ : { foo: import("id").Id<{}>; }
> : ^^^^^^^ ^^^
>object : { foo: import("id").Id<{}>; }
> : ^^^^^^^ ^^^

///////////
/**
* huh
*/
=== obj.d.ts ===
export declare const object: import("./id").Id<{
>object : { foo: import("./id").Id<{}>; }
> : ^^^^^^^ ^^^

foo: import("./id" ).Id<{}>;
>foo : {}
> : ^^

}>;

=== id.d.ts ===
export type Id<T> = T;
>Id : T
> : ^

29 changes: 29 additions & 0 deletions tests/baselines/reference/declarationEmitNoInvalidCommentReuse2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse2.ts] ////

//// [a.ts]
import { object } from "./obj.ts";

export const _ = object;

///////////
/**
* huh
*/
//// [obj.d.ts]
export declare const object: import("./id.ts").Id<{
foo: import("./id.ts" ).Id<{}>;
}>;

//// [id.d.ts]
export type Id<T> = T;




//// [a.d.ts]
export declare const _: {
foo: import("./id").Id<{}>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm slightly surprised this one gets rewritten to "./id" (this test is using rewriteRelativeImportExtensions). IIRC I couldn't make it to be rewritten to anything else with any module-related options but maybe I have not tried hard enough. I guess it might behave differently from what rewriteRelativeImportExtensions promises us normally because it's in a type node.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: #61037

};
/**
* huh
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse2.ts] ////

=== a.ts ===
import { object } from "./obj.ts";
>object : Symbol(object, Decl(a.ts, 0, 8))

export const _ = object;
>_ : Symbol(_, Decl(a.ts, 2, 12))
>object : Symbol(object, Decl(a.ts, 0, 8))

///////////
/**
* huh
*/
=== obj.d.ts ===
export declare const object: import("./id.ts").Id<{
>object : Symbol(object, Decl(obj.d.ts, 0, 20))
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))

foo: import("./id.ts" ).Id<{}>;
>foo : Symbol(foo, Decl(obj.d.ts, 0, 51))
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))

}>;

=== id.d.ts ===
export type Id<T> = T;
>Id : Symbol(Id, Decl(id.d.ts, 0, 0))
>T : Symbol(T, Decl(id.d.ts, 0, 15))
>T : Symbol(T, Decl(id.d.ts, 0, 15))

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse2.ts] ////

=== a.ts ===
import { object } from "./obj.ts";
>object : { foo: import("id").Id<{}>; }
> : ^^^^^^^ ^^^

export const _ = object;
>_ : { foo: import("id").Id<{}>; }
> : ^^^^^^^ ^^^
>object : { foo: import("id").Id<{}>; }
> : ^^^^^^^ ^^^

///////////
/**
* huh
*/
=== obj.d.ts ===
export declare const object: import("./id.ts").Id<{
>object : { foo: import("./id.ts").Id<{}>; }
> : ^^^^^^^ ^^^

foo: import("./id.ts" ).Id<{}>;
>foo : {}
> : ^^

}>;

=== id.d.ts ===
export type Id<T> = T;
>Id : T
> : ^

29 changes: 29 additions & 0 deletions tests/baselines/reference/declarationEmitNoInvalidCommentReuse3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse3.ts] ////

//// [a.ts]
import { object } from "./obj";
import { id } from "./id";
export const _ = object;
/**
*/
//// [obj.d.ts]
import { id } from "./id";
// ----
export declare const object: id.A<{
foo: id.A<1>
}>;

//// [id.d.ts]
export declare namespace id {
type A<T> = T;
}



//// [a.d.ts]
import { id } from "./id";
export declare const _: {
foo: id.A<1>;
};
/**
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse3.ts] ////

=== a.ts ===
import { object } from "./obj";
>object : Symbol(object, Decl(a.ts, 0, 8))

import { id } from "./id";
>id : Symbol(id, Decl(a.ts, 1, 8))

export const _ = object;
>_ : Symbol(_, Decl(a.ts, 2, 12))
>object : Symbol(object, Decl(a.ts, 0, 8))

/**
*/
=== obj.d.ts ===
import { id } from "./id";
>id : Symbol(id, Decl(obj.d.ts, 0, 8))

// ----
export declare const object: id.A<{
>object : Symbol(object, Decl(obj.d.ts, 2, 20))
>id : Symbol(id, Decl(obj.d.ts, 0, 8))
>A : Symbol(id.A, Decl(id.d.ts, 0, 29))

foo: id.A<1>
>foo : Symbol(foo, Decl(obj.d.ts, 2, 35))
>id : Symbol(id, Decl(obj.d.ts, 0, 8))
>A : Symbol(id.A, Decl(id.d.ts, 0, 29))

}>;

=== id.d.ts ===
export declare namespace id {
>id : Symbol(id, Decl(id.d.ts, 0, 0))

type A<T> = T;
>A : Symbol(A, Decl(id.d.ts, 0, 29))
>T : Symbol(T, Decl(id.d.ts, 1, 11))
>T : Symbol(T, Decl(id.d.ts, 1, 11))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//// [tests/cases/compiler/declarationEmitNoInvalidCommentReuse3.ts] ////

=== a.ts ===
import { object } from "./obj";
>object : { foo: id.A<1>; }
> : ^^^^^^^ ^^^

import { id } from "./id";
>id : any
> : ^^^

export const _ = object;
>_ : { foo: id.A<1>; }
> : ^^^^^^^ ^^^
>object : { foo: id.A<1>; }
> : ^^^^^^^ ^^^

/**
*/
=== obj.d.ts ===
import { id } from "./id";
>id : any
> : ^^^

// ----
export declare const object: id.A<{
>object : { foo: id.A<1>; }
> : ^^^^^^^ ^^^
>id : any
> : ^^^

foo: id.A<1>
>foo : 1
> : ^
>id : any
> : ^^^

}>;

=== id.d.ts ===
export declare namespace id {
type A<T> = T;
>A : T
> : ^
}
22 changes: 22 additions & 0 deletions tests/cases/compiler/declarationEmitNoInvalidCommentReuse1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @strict: true
// @declaration: true
// @emitDeclarationOnly: true

// https://github.com/microsoft/TypeScript/issues/61239

// @filename: a.ts
import { object } from "./obj";

export const _ = object;

///////////
/**
* huh
*/
// @filename: obj.d.ts
export declare const object: import("./id").Id<{
foo: import("./id" ).Id<{}>;
}>;

// @filename: id.d.ts
export type Id<T> = T;
Loading