Skip to content

Commit

Permalink
Fix semicolon handling
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 27, 2024
1 parent b5d3a59 commit 9a6ae6a
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
} from "../core";
import {Semicolon, TrailingComma} from "../java";
import {binarySearch, compareTextSpans, getNextSibling, TextSpan} from "./parserUtils";
import {PunctuationSyntaxKind} from "typescript";

export class JavaScriptParser extends Parser {

Expand Down Expand Up @@ -207,7 +206,7 @@ export class JavaScriptParserVisitor {
return nodes.map(n => this.rightPadded(this.convert(n), trailing(n), markers?.(n)));
}

private rightPaddedSeparatedList<N extends ts.Node, T extends J.J>(nodes: N[], separator: ts.PunctuationSyntaxKind, markers?: (node: N) => Markers): JRightPadded<T>[] {
private rightPaddedSeparatedList<N extends ts.Node, T extends J.J>(nodes: N[], separator: ts.PunctuationSyntaxKind, markers: (nodes: N[], i: number) => Markers): JRightPadded<T>[] {
if (nodes.length === 0) {
return [];
}
Expand All @@ -219,11 +218,11 @@ export class JavaScriptParserVisitor {
ts.push(this.rightPadded(
this.convert(nodes[i]),
this.prefix(nodes[i + 1]),
last ? Markers.build([new TrailingComma(randomId(), this.prefix(nodes[nodes.length - 1]))]) : Markers.EMPTY
markers(nodes, i)
));
}
if ((nodes.length & 1) === 1) {
ts.push(this.rightPadded(this.convert(nodes[nodes.length - 1]), Space.EMPTY));
ts.push(this.rightPadded(this.convert(nodes[nodes.length - 1]), Space.EMPTY, markers(nodes, nodes.length - 1)));
}

return ts;
Expand Down Expand Up @@ -574,11 +573,31 @@ export class JavaScriptParserVisitor {
Space.EMPTY,
null,
JContainer.empty(),
this.convertBlock(node.getChildren().slice(-3)),
this.convertObjectAssignments(node.getChildren().slice(-3)),
this.mapMethodType(node)
);
}

private convertObjectAssignments(nodes: ts.Node[]): J.Block {
const prefix = this.prefix(nodes[0]);
let statementList = nodes[1] as ts.SyntaxList;

const statements: JRightPadded<Statement>[] = this.rightPaddedSeparatedList(
[...statementList.getChildren()],
ts.SyntaxKind.CommaToken,
(nodes, i) => i == nodes.length -2 && nodes[i + 1].kind == ts.SyntaxKind.CommaToken ? Markers.build([new TrailingComma(randomId(), this.prefix(nodes[i + 1]))]) : Markers.EMPTY
);

return new J.Block(
randomId(),
prefix,
Markers.EMPTY,
this.rightPadded(false, Space.EMPTY),
statements,
this.prefix(nodes[nodes.length - 1])
);
}

visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
return new J.FieldAccess(
randomId(),
Expand Down Expand Up @@ -1573,21 +1592,14 @@ export class JavaScriptParserVisitor {
}

private convertBlock(nodes: ts.Node[]): J.Block {
if (nodes.length === 0) {
return new J.Block(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.rightPadded(false, Space.EMPTY),
[],
Space.EMPTY
);
}

const prefix = this.prefix(nodes[0]);
let statementList = nodes[1] as ts.SyntaxList;

const statements: JRightPadded<Statement>[] = this.rightPaddedSeparatedList([...statementList.getChildren()], ts.SyntaxKind.CommaToken);
const statements: JRightPadded<Statement>[] = this.rightPaddedSeparatedList(
[...statementList.getChildren()],
ts.SyntaxKind.SemicolonToken,
(nodes, i) => nodes[i].getLastToken()?.kind == ts.SyntaxKind.SemicolonToken ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY
);

return new J.Block(
randomId(),
Expand Down

0 comments on commit 9a6ae6a

Please sign in to comment.