From 42b9fd6dcfa527400d45b1fd536e11070d9957b9 Mon Sep 17 00:00:00 2001 From: Andrii Rodionov Date: Mon, 18 Nov 2024 23:31:54 +0100 Subject: [PATCH] Fix for immediately invoked anonymous function (#150) Co-authored-by: Andrii Rodionov --- openrewrite/src/javascript/parser.ts | 83 +++++++++++-------- .../test/javascript/parser/function.test.ts | 11 +++ 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 95f2a5af..fda9650d 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -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 | null; - let name: ts.Expression; + let name: J.Identifier; if (ts.isPropertyAccessExpression(node.expression)) { select = this.rightPadded( node.expression.questionDotToken ? @@ -1363,44 +1398,22 @@ export class JavaScriptParserVisitor { this.convert(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) { diff --git a/openrewrite/test/javascript/parser/function.test.ts b/openrewrite/test/javascript/parser/function.test.ts index bdd85825..4651e5b1 100644 --- a/openrewrite/test/javascript/parser/function.test.ts +++ b/openrewrite/test/javascript/parser/function.test.ts @@ -162,4 +162,15 @@ describe('function mapping', () => { `) ); }); + + test('immediately invoked anonymous function', () => { + rewriteRun( + //language=typescript + typeScript(` + (function() { + console.log('IIFE'); + })(); + `) + ); + }); });