From 343010c60cdf741521d50ef987aef1db2dfe24e0 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Sat, 21 Sep 2024 13:27:34 +0200 Subject: [PATCH] Add `visitExpressionStatement()` and `visitStatementExpression()` --- .../javascript/JavaScriptVisitor.java | 33 +++++++++++++++++++ .../org/openrewrite/javascript/tree/JS.java | 16 ++------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java b/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java index 0e9ba682..43dbce6f 100644 --- a/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -183,6 +183,23 @@ public J visitExport(JS.Export export, P p) { return e; } + public J visitExpressionStatement(JS.ExpressionStatement statement, P p) { + JS.ExpressionStatement es = statement; + es = es.withMarkers(visitMarkers(es.getMarkers(), p)); + Statement temp = (Statement) visitStatement(es, p); + if (!(temp instanceof JS.ExpressionStatement)) { + return temp; + } else { + es = (JS.ExpressionStatement) temp; + } + J expression = visit(es.getExpression(), p); + if (expression instanceof Statement) { + return expression; + } + es = es.withExpression((Expression) expression); + return es; + } + public J visitFunctionType(JS.FunctionType functionType, P p) { JS.FunctionType f = functionType; f = f.withPrefix(visitSpace(f.getPrefix(), JsSpace.Location.FUNCTION_TYPE_PREFIX, p)); @@ -285,6 +302,22 @@ public J visitObjectBindingDeclarations(JS.ObjectBindingDeclarations objectBindi return o; } + public J visitStatementExpression(JS.StatementExpression expression, P p) { + JS.StatementExpression se = expression; + Expression temp = (Expression) visitExpression(se, p); + if (!(temp instanceof JS.StatementExpression)) { + return temp; + } else { + se = (JS.StatementExpression) temp; + } + J statement = visit(se.getStatement(), p); + if (statement instanceof Expression) { + return statement; + } + se = se.withStatement((Statement) statement); + return se; + } + public J visitTemplateExpression(JS.TemplateExpression templateExpression, P p) { JS.TemplateExpression s = templateExpression; s = s.withPrefix(visitSpace(s.getPrefix(), JsSpace.Location.TEMPLATE_EXPRESSION_PREFIX, p)); diff --git a/src/main/java/org/openrewrite/javascript/tree/JS.java b/src/main/java/org/openrewrite/javascript/tree/JS.java index 8d70e90e..0d5a9f13 100644 --- a/src/main/java/org/openrewrite/javascript/tree/JS.java +++ b/src/main/java/org/openrewrite/javascript/tree/JS.java @@ -585,13 +585,7 @@ final class ExpressionStatement implements JS, Expression, Statement { @Override public

J acceptJavaScript(JavaScriptVisitor

v, P p) { - J j = v.visit(getExpression(), p); - if (j instanceof ExpressionStatement) { - return j; - } else if (j instanceof Expression) { - return withExpression((Expression) j); - } - return j; + return v.visitExpressionStatement(this, p); } @Override @@ -1285,13 +1279,7 @@ final class StatementExpression implements JS, Expression, Statement { @Override public

J acceptJavaScript(JavaScriptVisitor

v, P p) { - J j = v.visit(getStatement(), p); - if (j instanceof StatementExpression) { - return j; - } else if (j instanceof Statement) { - return withStatement((Statement) j); - } - return j; + return v.visitStatementExpression(this, p); } @Override