diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 48e837ec..49619e5d 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -4,6 +4,7 @@ import {Comment, JavaType, JRightPadded, Space, TextComment} from '../java/tree' import * as JS from './tree'; import {ExecutionContext, Markers, ParseError, Parser, ParserInput, randomId, SourceFile} from "../core"; import {Semicolon} from "../java"; +import {FalseLiteral, NullLiteral, TrueLiteral} from "typescript"; export class JavaScriptParser extends Parser { @@ -173,10 +174,22 @@ export class JavaScriptParserVisitor { } visitNumericLiteral(node: ts.NumericLiteral) { - return this.mapLiteral(node, node.text) // FIXME value not in AST + return this.mapLiteral(node, node.text); // FIXME value not in AST + } + + visitTrueKeyword(node: ts.TrueLiteral) { + return this.mapLiteral(node, true); + } + + visitFalseKeyword(node: ts.FalseLiteral) { + return this.mapLiteral(node, false); + } + + visitNullKeyword(node: ts.NullLiteral) { + return this.mapLiteral(node, null); } - private mapLiteral(node: ts.LiteralExpression, value: any) { + private mapLiteral(node: ts.LiteralExpression | TrueLiteral | FalseLiteral | NullLiteral, value: any): J.Literal { return new J.Literal( randomId(), this.prefix(node), @@ -1026,6 +1039,10 @@ export class JavaScriptParserVisitor { return JavaType.Primitive.of(JavaType.PrimitiveKind.String); } return JavaType.Primitive.of(JavaType.PrimitiveKind.Void); + } else if (node.kind == ts.SyntaxKind.TrueKeyword || node.kind == ts.SyntaxKind.FalseKeyword) { + return JavaType.Primitive.of(JavaType.PrimitiveKind.Boolean); + } else if (node.kind == ts.SyntaxKind.NullKeyword) { + return JavaType.Primitive.of(JavaType.PrimitiveKind.Null); } return JavaType.Unknown.INSTANCE; } diff --git a/openrewrite/test/javascript/parser/literal.test.ts b/openrewrite/test/javascript/parser/literal.test.ts index 1ef95f93..0e64f98c 100644 --- a/openrewrite/test/javascript/parser/literal.test.ts +++ b/openrewrite/test/javascript/parser/literal.test.ts @@ -10,26 +10,38 @@ describe('literal mapping', () => { rewriteRunWithOptions( {normalizeIndent: false}, javaScript(' 1', sourceFile => { - expect(sourceFile).toBeDefined(); - expect(sourceFile.statements).toHaveLength(1); - let statement = sourceFile.statements[0]; - expect(statement).toBeInstanceOf(JS.ExpressionStatement); - let expression = (statement as JS.ExpressionStatement).expression; - expect(expression).toBeInstanceOf(J.Literal); - expect((expression as J.Literal).valueSource).toBe('1'); + assertLiteralLst(sourceFile, '1'); })); }); test('string', () => { rewriteRunWithOptions( {normalizeIndent: false}, javaScript('"1"', sourceFile => { - expect(sourceFile).toBeDefined(); - expect(sourceFile.statements).toHaveLength(1); - let statement = sourceFile.statements[0]; - expect(statement).toBeInstanceOf(JS.ExpressionStatement); - let expression = (statement as JS.ExpressionStatement).expression; - expect(expression).toBeInstanceOf(J.Literal); - expect((expression as J.Literal).valueSource).toBe('"1"'); + assertLiteralLst(sourceFile, '"1"'); })); }); + test('boolean', () => { + rewriteRunWithOptions( + {normalizeIndent: false}, + javaScript('true', sourceFile => { + assertLiteralLst(sourceFile, 'true'); + })); + }); + test('null', () => { + rewriteRunWithOptions( + {normalizeIndent: false}, + javaScript('null', sourceFile => { + assertLiteralLst(sourceFile, 'null'); + })); + }); + + function assertLiteralLst(sourceFile: JS.CompilationUnit, expectedValueSource: string) { + expect(sourceFile).toBeDefined(); + expect(sourceFile.statements).toHaveLength(1); + let statement = sourceFile.statements[0]; + expect(statement).toBeInstanceOf(JS.ExpressionStatement); + let expression = (statement as JS.ExpressionStatement).expression; + expect(expression).toBeInstanceOf(J.Literal); + expect((expression as J.Literal).valueSource).toBe(expectedValueSource); + } });