From ffa7bfad5624bc10452e4d4468980ccb7785f331 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 26 Sep 2024 09:48:58 +0200 Subject: [PATCH] Map `ts.ParenthesizedExpression` --- openrewrite/src/javascript/parser.ts | 25 ++++++++++++------- .../javascript/parser/parenthesis.test.ts | 18 +++++++++++++ 2 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 openrewrite/test/javascript/parser/parenthesis.test.ts diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 637c6b12..0c45e8bd 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -151,14 +151,16 @@ export class JavaScriptParserVisitor { return []; } - private rightPaddedList(nodes: ts.NodeArray, trailing?: (node: N) => Space, markers?: (node: N) => Markers) { - return nodes.map(n => { - return new JRightPadded( - this.visit(n) as T, - trailing ? trailing(n) : Space.EMPTY, - markers ? markers(n) : Markers.EMPTY - ); - }); + private rightPadded(node: N, trailing?: Space, markers?: Markers) { + return new JRightPadded( + this.visit(node) as T, + trailing ?? Space.EMPTY, + markers ?? Markers.EMPTY + ); + } + + private rightPaddedList(nodes: ts.NodeArray, trailing?: (node: N) => Space, markers?: (node: N) => Markers): JRightPadded[] { + return nodes.map(n => this.rightPadded(n, trailing?.(n), markers?.(n))); } private semicolonPrefix = (n: ts.Node) => { @@ -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) { diff --git a/openrewrite/test/javascript/parser/parenthesis.test.ts b/openrewrite/test/javascript/parser/parenthesis.test.ts new file mode 100644 index 00000000..564e05f8 --- /dev/null +++ b/openrewrite/test/javascript/parser/parenthesis.test.ts @@ -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 )') + ); + }); +});