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

No Semicolon for J.Unknown and no unnecessary JS.ExpressionStatement #124

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
28 changes: 21 additions & 7 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ts from 'typescript';
import * as J from '../java';
import {JavaType, JContainer, JLeftPadded, JRightPadded, Semicolon, Space, Statement, TrailingComma} from '../java';
import {JavaType, JContainer, JLeftPadded, JRightPadded, Semicolon, Space, TrailingComma} from '../java';
import * as JS from '.';
import {
ExecutionContext,
Expand Down Expand Up @@ -214,9 +214,16 @@ export class JavaScriptParserVisitor {
}

private semicolonPaddedStatementList(statements: ts.NodeArray<ts.Statement>) {
return this.rightPaddedList([...statements], this.semicolonPrefix, n => {
const last = n.getChildAt(n.getChildCount(this.sourceFile) - 1, this.sourceFile);
return last?.kind == ts.SyntaxKind.SemicolonToken ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY;
return [...statements].map(n => {
const j: J.Statement = this.convert(n);
if (j instanceof J.Unknown) {
// in case of `J.Unknown` its source will already contain any `;`
return this.rightPadded(j, Space.EMPTY, Markers.EMPTY);
}
return this.rightPadded(j, this.semicolonPrefix(n), (n => {
const last = n.getChildAt(n.getChildCount(this.sourceFile) - 1, this.sourceFile);
return last?.kind == ts.SyntaxKind.SemicolonToken ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY;
})?.(n));
});
}

Expand Down Expand Up @@ -1259,10 +1266,17 @@ export class JavaScriptParserVisitor {
return this.visitVariableDeclarationList(node.declarationList);
}

visitExpressionStatement(node: ts.ExpressionStatement): JS.ExpressionStatement {
visitExpressionStatement(node: ts.ExpressionStatement): J.Statement {
const expression = this.visit(node.expression) as J.Expression;
if (expression instanceof J.MethodInvocation || expression instanceof J.NewClass || expression instanceof J.Unknown ||
expression instanceof J.AssignmentOperation || expression instanceof J.Ternary || expression instanceof J.Empty ||
expression instanceof JS.ExpressionStatement || expression instanceof J.Assignment || expression instanceof J.FieldAccess) {
// FIXME this is a hack we currently require because our `Expression` and `Statement` interfaces don't have any type guards
return expression as J.Statement;
}
return new JS.ExpressionStatement(
randomId(),
this.visit(node.expression) as J.Expression
expression
)
}

Expand Down Expand Up @@ -1413,7 +1427,7 @@ export class JavaScriptParserVisitor {
[this.rightPadded(this.visit(declaration), Space.EMPTY)]
), this.suffix(declaration));
})
)
);
}

visitFunctionDeclaration(node: ts.FunctionDeclaration) {
Expand Down