Skip to content

Commit

Permalink
Add visitExpressionStatement() and visitStatementExpression()
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 21, 2024
1 parent 8252d3c commit 343010c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
33 changes: 33 additions & 0 deletions src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/org/openrewrite/javascript/tree/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,7 @@ final class ExpressionStatement implements JS, Expression, Statement {

@Override
public <P> J acceptJavaScript(JavaScriptVisitor<P> 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
Expand Down Expand Up @@ -1285,13 +1279,7 @@ final class StatementExpression implements JS, Expression, Statement {

@Override
public <P> J acceptJavaScript(JavaScriptVisitor<P> 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
Expand Down

0 comments on commit 343010c

Please sign in to comment.