From 6809e2938be35940dbecdd0ca5d230d8435b5536 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Mon, 30 Sep 2024 14:18:24 +0200 Subject: [PATCH] Also support `if-else` --- openrewrite/src/javascript/parser.ts | 17 +++++++++++++++-- .../test/javascript/parser/empty.test.ts | 13 +++++++++++++ openrewrite/test/javascript/parser/if.test.ts | 12 ++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 openrewrite/test/javascript/parser/empty.test.ts diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index ba9f3a86..6d483a41 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -1162,7 +1162,11 @@ export class JavaScriptParserVisitor { } visitEmptyStatement(node: ts.EmptyStatement) { - return this.visitUnknown(node); + return new J.Empty( + randomId(), + this.prefix(node), + Markers.EMPTY + ); } visitVariableStatement(node: ts.VariableStatement): J.VariableDeclarations | J.Unknown { @@ -1209,7 +1213,16 @@ export class JavaScriptParserVisitor { semicolonAfterThen ? this.prefix(node.thenStatement.getLastToken()!) : Space.EMPTY, semicolonAfterThen ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY ), - node.elseStatement ? this.visit(node.elseStatement) : null + node.elseStatement ? new J.If.Else( + randomId(), + this.prefix(this.findChildNode(node, ts.SyntaxKind.ElseKeyword)!), + Markers.EMPTY, + this.rightPadded( + this.convert(node.elseStatement), + semicolonAfterThen ? this.prefix(node.elseStatement.getLastToken()!) : Space.EMPTY, + semicolonAfterThen ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY + ) + ) : null ); } diff --git a/openrewrite/test/javascript/parser/empty.test.ts b/openrewrite/test/javascript/parser/empty.test.ts new file mode 100644 index 00000000..8ffd2ce5 --- /dev/null +++ b/openrewrite/test/javascript/parser/empty.test.ts @@ -0,0 +1,13 @@ +import {connect, disconnect, rewriteRun, typeScript} from '../testHarness'; + +describe('empty mapping', () => { + beforeAll(() => connect()); + afterAll(() => disconnect()); + + test('simple', () => { + rewriteRun( + //language=typescript + typeScript(';') + ); + }); +}); diff --git a/openrewrite/test/javascript/parser/if.test.ts b/openrewrite/test/javascript/parser/if.test.ts index 1444d2a6..db2e6fad 100644 --- a/openrewrite/test/javascript/parser/if.test.ts +++ b/openrewrite/test/javascript/parser/if.test.ts @@ -10,4 +10,16 @@ describe('if mapping', () => { typeScript('if (true) console.log("foo");') ); }); + test('braces', () => { + rewriteRun( + //language=typescript + typeScript('if (true) { console.log("foo"); }') + ); + }); + test('else', () => { + rewriteRun( + //language=typescript + typeScript('if (true) console.log("foo"); else console.log("bar");') + ); + }); });