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

Fix for immediately invoked anonymous function #150

Merged
merged 1 commit into from
Nov 18, 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
83 changes: 48 additions & 35 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1347,8 +1347,43 @@ export class JavaScriptParserVisitor {

visitCallExpression(node: ts.CallExpression) {
const prefix = this.prefix(node);
const typeArguments = node.typeArguments ? this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments) : null;

if (ts.isParenthesizedExpression(node.expression)) {
return new JS.JSMethodInvocation(
randomId(),
prefix,
Markers.EMPTY,
null,
typeArguments,
this.convert(node.expression),
this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(-3)),
this.mapMethodType(node)
);
}

if (node.questionDotToken) {
return new JS.JSMethodInvocation(
randomId(),
prefix,
Markers.EMPTY,
null,
typeArguments,
new JS.Unary(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.leftPadded(this.suffix(node.expression), JS.Unary.Type.QuestionDotWithDot),
this.visit(node.expression),
this.mapType(node)
),
this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(-3)),
this.mapMethodType(node)
);
}

let select: JRightPadded<J.Expression> | null;
let name: ts.Expression;
let name: J.Identifier;
if (ts.isPropertyAccessExpression(node.expression)) {
select = this.rightPadded(
node.expression.questionDotToken ?
Expand All @@ -1363,44 +1398,22 @@ export class JavaScriptParserVisitor {
this.convert<J.Expression>(node.expression.expression),
this.prefix(node.expression.getChildAt(1, this.sourceFile))
);
name = node.expression.name;
name = this.convert(node.expression.name);
} else {
select = null;
name = node.expression;
name = this.convert(node.expression);
}

if (node.questionDotToken) {
const unary = new JS.Unary(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.leftPadded(this.suffix(name), JS.Unary.Type.QuestionDotWithDot),
this.visit(name),
this.mapType(node)
);

return new JS.JSMethodInvocation(
randomId(),
prefix,
Markers.EMPTY,
select,
node.typeArguments ? this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments) : null,
unary,
this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(-3)),
this.mapMethodType(node)
);
} else {
return new J.MethodInvocation(
randomId(),
prefix,
Markers.EMPTY,
select,
node.typeArguments ? this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments) : null,
this.convert(name),
this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(-3)),
this.mapMethodType(node)
);
}
return new J.MethodInvocation(
randomId(),
prefix,
Markers.EMPTY,
select,
typeArguments,
name,
this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(-3)),
this.mapMethodType(node)
)
}

visitNewExpression(node: ts.NewExpression) {
Expand Down
11 changes: 11 additions & 0 deletions openrewrite/test/javascript/parser/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,15 @@ describe('function mapping', () => {
`)
);
});

test('immediately invoked anonymous function', () => {
rewriteRun(
//language=typescript
typeScript(`
(function() {
console.log('IIFE');
})();
`)
);
});
});