Skip to content

Remove uses of visitNodes and visitNode in visitEachChild #49992

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 3 commits into from
Jul 23, 2022
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
12 changes: 6 additions & 6 deletions src/compiler/visitorPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ namespace ts {
nodeVisitor(node.argument, visitor, isTypeNode),
nodeVisitor(node.assertions, visitor, isNode),
nodeVisitor(node.qualifier, visitor, isEntityName),
visitNodes(node.typeArguments, visitor, isTypeNode),
nodesVisitor(node.typeArguments, visitor, isTypeNode),
Copy link
Member Author

Choose a reason for hiding this comment

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

This one in particular was the bug.

I tried to come up with a way that would prevent this sort of from happening (like Debug.type<unknown>(visitNodes), or redeclaring it), but wasn't successful in stopping TS from allowing this.

Copy link
Member Author

Choose a reason for hiding this comment

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

I really, really wonder how many other bugs there are that were caused by this in the wild...

Copy link
Member Author

Choose a reason for hiding this comment

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

wasn't successful in stopping TS from allowing this.

The fix would be to start targeting ES2015 aka ES6, then do const visitNodes = undefined in the body; the only reason that doesn't work now is that we downlevel default parameters to assignments and that interferes with the scoping.

node.isTypeOf
);

Expand All @@ -630,10 +630,10 @@ namespace ts {
case SyntaxKind.NamedTupleMember:
Debug.type<NamedTupleMember>(node);
return factory.updateNamedTupleMember(node,
visitNode(node.dotDotDotToken, visitor, isDotDotDotToken),
visitNode(node.name, visitor, isIdentifier),
visitNode(node.questionToken, visitor, isQuestionToken),
visitNode(node.type, visitor, isTypeNode),
nodeVisitor(node.dotDotDotToken, visitor, isDotDotDotToken),
nodeVisitor(node.name, visitor, isIdentifier),
nodeVisitor(node.questionToken, visitor, isQuestionToken),
nodeVisitor(node.type, visitor, isTypeNode),
);

case SyntaxKind.ParenthesizedType:
Expand Down Expand Up @@ -761,7 +761,7 @@ namespace ts {
Debug.type<TaggedTemplateExpression>(node);
return factory.updateTaggedTemplateExpression(node,
nodeVisitor(node.tag, visitor, isExpression),
visitNodes(node.typeArguments, visitor, isTypeNode),
nodesVisitor(node.typeArguments, visitor, isTypeNode),
nodeVisitor(node.template, visitor, isTemplateLiteral));

case SyntaxKind.TypeAssertionExpression:
Expand Down
2 changes: 2 additions & 0 deletions src/services/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ namespace ts.formatting {
undecoratedParentStartLine: number,
isListItem: boolean,
isFirstListItem?: boolean): number {
Debug.assert(!nodeIsSynthesized(child));

if (nodeIsMissing(child)) {
return inheritedIndentation;
Expand Down Expand Up @@ -778,6 +779,7 @@ namespace ts.formatting {
parentStartLine: number,
parentDynamicIndentation: DynamicIndentation): void {
Debug.assert(isNodeArray(nodes));
Debug.assert(!nodeIsSynthesized(nodes));

const listStartToken = getOpenTokenForList(parent, nodes);

Expand Down
62 changes: 62 additions & 0 deletions tests/cases/fourslash/extractFunctionWithSyntheticNodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/// <reference path="fourslash.ts" />

// @filename: /project/tsconfig.json
//// {}

// @filename: /project/index.esm.d.ts
//// export declare class Chart {
//// constructor(config: ChartConfiguration);
//// }
////
//// export interface ChartConfiguration {
//// options?: Partial<TickOptions>;
//// }
////
//// export interface TickOptions {
//// callback: (this: Scale, tickValue: number | string) => string | string[] | number | number[] | null | undefined;
//// }
////
//// export interface CoreScaleOptions {
//// opt: boolean;
//// }
////
//// export interface Scale<O extends CoreScaleOptions = CoreScaleOptions> {
//// opts: O;
//// getLabelForValue(value: number): string;
//// }

// @filename: /project/options.ts
//// import { Chart } from './index.esm';
////
//// const chart = new Chart({
//// options: {
//// callback(tickValue) {
//// /*a*/const value = this.getLabelForValue(tickValue as number);/*b*/
//// return '$' + value;
//// }
//// }
//// });

goTo.file("/project/options.ts");
verify.noErrors();
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Extract Symbol",
actionName: "function_scope_0",
actionDescription: "Extract to inner function in method 'callback'",
newContent:
`import { Chart } from './index.esm';

const chart = new Chart({
options: {
callback(tickValue) {
const value = /*RENAME*/newFunction.call(this);
return '$' + value;

function newFunction(this: import("/project/index.esm").Scale<import("/project/index.esm").CoreScaleOptions>) {
return this.getLabelForValue(tickValue as number);
}
}
}
});`
});