Skip to content

Commit

Permalink
Map ts.ParenthesizedExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 26, 2024
1 parent dbeabd7 commit ffa7bfa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
25 changes: 16 additions & 9 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,16 @@ export class JavaScriptParserVisitor {
return [];
}

private rightPaddedList<N extends ts.Node, T extends J.J>(nodes: ts.NodeArray<N>, trailing?: (node: N) => Space, markers?: (node: N) => Markers) {
return nodes.map(n => {
return new JRightPadded<T>(
this.visit(n) as T,
trailing ? trailing(n) : Space.EMPTY,
markers ? markers(n) : Markers.EMPTY
);
});
private rightPadded<N extends ts.Node, T extends J.J>(node: N, trailing?: Space, markers?: Markers) {
return new JRightPadded<T>(
this.visit(node) as T,
trailing ?? Space.EMPTY,
markers ?? Markers.EMPTY
);
}

private rightPaddedList<N extends ts.Node, T extends J.J>(nodes: ts.NodeArray<N>, trailing?: (node: N) => Space, markers?: (node: N) => Markers): JRightPadded<T>[] {
return nodes.map(n => this.rightPadded(n, trailing?.(n), markers?.(n)));
}

private semicolonPrefix = (n: ts.Node) => {
Expand Down Expand Up @@ -461,7 +463,12 @@ export class JavaScriptParserVisitor {
}

visitParenthesizedExpression(node: ts.ParenthesizedExpression) {
return this.visitUnknown(node);
return new J.Parentheses(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.rightPadded(node.expression, this.prefix(node.getLastToken()!))
)
}

visitFunctionExpression(node: ts.FunctionExpression) {
Expand Down
18 changes: 18 additions & 0 deletions openrewrite/test/javascript/parser/parenthesis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {connect, disconnect, javaScript, rewriteRun} from '../testHarness';

describe('parenthesis mapping', () => {
beforeAll(() => connect());
afterAll(() => disconnect());

test('simple', () => {
rewriteRun(
javaScript('(1)')
);
});

test('space', () => {
rewriteRun(
javaScript('( 1 )')
);
});
});

0 comments on commit ffa7bfa

Please sign in to comment.