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

Nested ExpressionStatements Issue #144 #181

Merged
merged 4 commits into from
Aug 11, 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: 10 additions & 2 deletions packages/ts-migrate-plugins/src/plugins/add-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const addConversionsTransformerFactory =
: factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);

let nodesToConvert: Set<ts.Node>;

const ancestorReplaceMap = new Map<ts.Node, boolean>();
return (file: ts.SourceFile) => {
nodesToConvert = new Set(
diags
Expand Down Expand Up @@ -71,6 +71,14 @@ const addConversionsTransformerFactory =
};

function visit(origNode: ts.Node): ts.Node | undefined {
const ancestorShouldBeReplaced = ancestorReplaceMap.get(origNode.parent);
ancestorReplaceMap.set(
origNode,
ancestorShouldBeReplaced === undefined
? origNode.kind === ts.SyntaxKind.ExpressionStatement
: origNode.kind === ts.SyntaxKind.ExpressionStatement || ancestorShouldBeReplaced,
);

const needsConversion = nodesToConvert.has(origNode);
let node = ts.visitEachChild(origNode, visit, context);
if (node === origNode && !needsConversion) {
Expand All @@ -81,7 +89,7 @@ const addConversionsTransformerFactory =
node = factory.createAsExpression(node as ts.Expression, anyType);
}

if (shouldReplace(node)) {
if (shouldReplace(node) && !ancestorShouldBeReplaced) {
replaceNode(origNode, node);
return origNode;
}
Expand Down
18 changes: 18 additions & 0 deletions packages/ts-migrate-plugins/tests/src/add-conversions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,22 @@ class PublishEvent {
}
`);
});

it('Nested Expression Statements (issue #144) Part 1: Expression Statement -> Expression Statement', async () => {
const text = `var window = { onResetData: function () { this.clearNextPush = function () { this.setState({ history: [] }); }; } };`;
const result = addConversionsPlugin.run(await realPluginParams({ text }));

expect(result).toBe(
`var window = { onResetData: function () { (this as any).clearNextPush = function () { (this as any).setState({ history: [] }); }; } };`,
);
});

it('Nested Expression Statements (issue #144) Part 2: Expression Statement -> If Statement -> Expression Statement', async () => {
const text = `const window = { onResetData() { this.clearNextPush = function () {\n if (this.setState) {\n this.setState({ history: [] });\n} }; } };`;
const result = addConversionsPlugin.run(await realPluginParams({ text }));

expect(result).toBe(
`const window = { onResetData() { (this as any).clearNextPush = function () {\n if ((this as any).setState) {\n (this as any).setState({ history: [] });\n }\n}; } };`,
);
});
});