Skip to content
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

Fixed declaration emit issue related to a qualifier being reused cross-file #58810

Merged
merged 3 commits into from
Jun 14, 2024
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
16 changes: 8 additions & 8 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8726,7 +8726,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
if (isJSDocTypeLiteral(node)) {
return factory.createTypeLiteralNode(map(node.jsDocPropertyTags, t => {
const name = isIdentifier(t.name) ? t.name : t.name.right;
const name = visitNode(isIdentifier(t.name) ? t.name : t.name.right, visitExistingNodeTreeSymbols, isIdentifier)!;
const typeViaParent = getTypeOfPropertyOfType(getTypeFromTypeNode(context, node), name.escapedText);
const overrideTypeNode = typeViaParent && t.typeExpression && getTypeFromTypeNode(context, t.typeExpression.type) !== typeViaParent ? typeToTypeNodeHelper(typeViaParent, context) : undefined;

Expand Down Expand Up @@ -8765,7 +8765,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
/*modifiers*/ undefined,
getEffectiveDotDotDotForParameter(p),
setTextRange(context, factory.createIdentifier(getNameForJSDocFunctionParameter(p, i)), p),
p.questionToken,
factory.cloneNode(p.questionToken),
Andarist marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some other code paths in this function do a similar "cloning" of the .questionToken so I assume that it's the right thing to do here and in other places that I touched

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those 2 call sites related to .questionToken are also related to JSDoc. I wonder if this cloning even matters in this context.

visitNode(p.type, visitExistingNodeTreeSymbols, isTypeNode),
/*initializer*/ undefined,
)),
Expand All @@ -8780,7 +8780,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
/*modifiers*/ undefined,
getEffectiveDotDotDotForParameter(p),
setTextRange(context, factory.createIdentifier(getNameForJSDocFunctionParameter(p, i)), p),
p.questionToken,
factory.cloneNode(p.questionToken),
visitNode(p.type, visitExistingNodeTreeSymbols, isTypeNode),
/*initializer*/ undefined,
)),
Expand All @@ -8798,7 +8798,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (isTypeParameterDeclaration(node)) {
return factory.updateTypeParameterDeclaration(
node,
node.modifiers,
visitNodes(node.modifiers, visitExistingNodeTreeSymbols, isModifier),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

those modifiers are just tokens so i guess i could just map over them and clone them?

setTextRange(context, typeParameterToName(getDeclaredTypeOfSymbol(getSymbolOfDeclaration(node)), context), node),
visitNode(node.constraint, visitExistingNodeTreeSymbols, isTypeNode),
visitNode(node.default, visitExistingNodeTreeSymbols, isTypeNode),
Expand Down Expand Up @@ -8839,8 +8839,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return factory.updateImportTypeNode(
node,
factory.updateLiteralTypeNode(node.argument, rewriteModuleSpecifier(node, node.argument.literal)),
node.attributes,
node.qualifier,
visitNode(node.attributes, visitExistingNodeTreeSymbols, isImportAttributes),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

those are not jus flat tokens so I wasn't quite sure what to do about those, it seemed like maybe i should deep clone them?

visitNode(node.qualifier, visitExistingNodeTreeSymbols, isEntityName),
Copy link

@MichaelMitchell-at MichaelMitchell-at Jun 10, 2024

Choose a reason for hiding this comment

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

I definitely don't have enough context on the workings of the checker to give any informed feedback, but I can at least ask at a high level do we have some confidence whether anything more could be done to the structure of the code to avoid this general class of problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tbh, I don't know this part of TS too well either so we need to wait for @weswigham or @dragomirtitian to give some feedback on this. I'd expect that other things in this function (like node.attributes here) should be wrapped like this too (if only this is the right fix).

Copy link
Member

Choose a reason for hiding this comment

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

I would definitely suspect a lot of this needs this same fix.

Copy link
Member

Choose a reason for hiding this comment

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

100% - any child node we copy should have its' children visited - any missing visitor calls on manually remapped nodes are an oversight, presumably.

visitNodes(node.typeArguments, visitExistingNodeTreeSymbols, isTypeNode),
node.isTypeOf,
);
Expand Down Expand Up @@ -8904,9 +8904,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
parameterName = result;
}
else {
parameterName = node.parameterName;
parameterName = factory.cloneNode(node.parameterName);
}
return factory.updateTypePredicateNode(node, node.assertsModifier, parameterName, visitNode(node.type, visitExistingNodeTreeSymbols, isTypeNode));
return factory.updateTypePredicateNode(node, factory.cloneNode(node.assertsModifier), parameterName, visitNode(node.type, visitExistingNodeTreeSymbols, isTypeNode));
}

if (isTupleTypeNode(node) || isTypeLiteralNode(node) || isMappedTypeNode(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.

there are also places in this function that do smth like:

if (visited === node) {
  visited = factory.cloneNode(node)
}

this pattern is not applied consistently across the whole function, should it be? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

I suspect yes.... Sure seems like though that we should make some sort of wrapper for the visitor func that would check that. Though I feel like I've seen that exact code in some visitor transformer code already (maybe we're not going through that)

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 think this might be a bigger change so I'd leave it out of this PR. I just want to flag this as a potential problem.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//// [tests/cases/compiler/declarationEmitTopLevelNodeFromCrossFile2.ts] ////

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

export const _ = boxedBox;

// At index 83
/**
* wat
*/

//// [boxedBox.d.ts]
export declare const boxedBox: import("./box").Box<{
boxed: import("./box").Box<number>;
}>; // ^This is index 83 in this file

//// [box.d.ts]
export declare class Box<T> {
value: T;
constructor(value: T);
}
export declare function box<T>(value: T): Box<T>;

//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports._ = void 0;
var boxedBox_1 = require("./boxedBox");
exports._ = boxedBox_1.boxedBox;
// At index 83
/**
* wat
*/


//// [a.d.ts]
export declare const _: import("./box").Box<{
boxed: import("./box").Box<number>;
}>;
/**
* wat
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//// [tests/cases/compiler/declarationEmitTopLevelNodeFromCrossFile2.ts] ////

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

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

// At index 83
/**
* wat
*/

=== boxedBox.d.ts ===
export declare const boxedBox: import("./box").Box<{
>boxedBox : Symbol(boxedBox, Decl(boxedBox.d.ts, 0, 20))
>Box : Symbol(Box, Decl(box.d.ts, 0, 0))

boxed: import("./box").Box<number>;
>boxed : Symbol(boxed, Decl(boxedBox.d.ts, 0, 52))
>Box : Symbol(Box, Decl(box.d.ts, 0, 0))

}>; // ^This is index 83 in this file

=== box.d.ts ===
export declare class Box<T> {
>Box : Symbol(Box, Decl(box.d.ts, 0, 0))
>T : Symbol(T, Decl(box.d.ts, 0, 25))

value: T;
>value : Symbol(Box.value, Decl(box.d.ts, 0, 29))
>T : Symbol(T, Decl(box.d.ts, 0, 25))

constructor(value: T);
>value : Symbol(value, Decl(box.d.ts, 2, 16))
>T : Symbol(T, Decl(box.d.ts, 0, 25))
}
export declare function box<T>(value: T): Box<T>;
>box : Symbol(box, Decl(box.d.ts, 3, 1))
>T : Symbol(T, Decl(box.d.ts, 4, 28))
>value : Symbol(value, Decl(box.d.ts, 4, 31))
>T : Symbol(T, Decl(box.d.ts, 4, 28))
>Box : Symbol(Box, Decl(box.d.ts, 0, 0))
>T : Symbol(T, Decl(box.d.ts, 4, 28))

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

=== a.ts ===
import { boxedBox } from "./boxedBox";
>boxedBox : import("box").Box<{ boxed: import("box").Box<number>; }>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^

export const _ = boxedBox;
>_ : import("box").Box<{ boxed: import("box").Box<number>; }>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^
>boxedBox : import("box").Box<{ boxed: import("box").Box<number>; }>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^

// At index 83
/**
* wat
*/

=== boxedBox.d.ts ===
export declare const boxedBox: import("./box").Box<{
>boxedBox : import("box").Box<{ boxed: import("./box").Box<number>; }>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^

boxed: import("./box").Box<number>;
>boxed : import("box").Box<number>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

}>; // ^This is index 83 in this file

=== box.d.ts ===
export declare class Box<T> {
>Box : Box<T>
> : ^^^^^^

value: T;
>value : T
> : ^

constructor(value: T);
>value : T
> : ^
}
export declare function box<T>(value: T): Box<T>;
>box : <T>(value: T) => Box<T>
> : ^ ^^ ^^ ^^^^^
>value : T
> : ^

24 changes: 24 additions & 0 deletions tests/cases/compiler/declarationEmitTopLevelNodeFromCrossFile2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// @strict: true
// @declaration: true

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

export const _ = boxedBox;

// At index 83
/**
* wat
*/

// @filename: boxedBox.d.ts
export declare const boxedBox: import("./box").Box<{
boxed: import("./box").Box<number>;
}>; // ^This is index 83 in this file

// @filename: box.d.ts
export declare class Box<T> {
value: T;
constructor(value: T);
}
export declare function box<T>(value: T): Box<T>;