Skip to content

Commit

Permalink
Also support if-else
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 30, 2024
1 parent 7046854 commit 6809e29
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
17 changes: 15 additions & 2 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
);
}

Expand Down
13 changes: 13 additions & 0 deletions openrewrite/test/javascript/parser/empty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {connect, disconnect, rewriteRun, typeScript} from '../testHarness';

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

test('simple', () => {
rewriteRun(
//language=typescript
typeScript(';')
);
});
});
12 changes: 12 additions & 0 deletions openrewrite/test/javascript/parser/if.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");')
);
});
});

0 comments on commit 6809e29

Please sign in to comment.