Skip to content

Commit

Permalink
Fix for immediately invoked anonymous function (#150)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrii Rodionov <[email protected]>
  • Loading branch information
arodionov and Andrii Rodionov authored Nov 18, 2024
1 parent aa44c2c commit 42b9fd6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
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');
})();
`)
);
});
});

0 comments on commit 42b9fd6

Please sign in to comment.