From bcd78514e23ca0c32b3858bd435c2de99089beba Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Wed, 25 Sep 2024 21:14:17 +0200 Subject: [PATCH] Implement a few more primary expressions --- openrewrite/src/javascript/parser.ts | 20 +++++++------- .../test/javascript/parser/literal.test.ts | 26 +++++++++++++++---- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 49619e5..c68e037 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -202,35 +202,35 @@ export class JavaScriptParserVisitor { } visitBigIntLiteral(node: ts.BigIntLiteral) { - return this.mapLiteral(node, node.text) // FIXME value not in AST + return this.mapLiteral(node, node.text); // FIXME value not in AST } visitStringLiteral(node: ts.StringLiteral) { - return this.mapLiteral(node, node.text) // FIXME value not in AST + return this.mapLiteral(node, node.text); // FIXME value not in AST } visitJsxText(node: ts.JsxText) { - return this.visitUnknown(node) + return this.visitUnknown(node); } visitRegularExpressionLiteral(node: ts.RegularExpressionLiteral) { - return this.visitUnknown(node) + return this.mapLiteral(node, node.text); // FIXME value not in AST } visitNoSubstitutionTemplateLiteral(node: ts.NoSubstitutionTemplateLiteral) { - return this.visitUnknown(node) + return this.mapLiteral(node, node.text); // FIXME value not in AST } visitTemplateHead(node: ts.TemplateHead) { - return this.visitUnknown(node) + return this.visitUnknown(node); } visitTemplateMiddle(node: ts.TemplateMiddle) { - return this.visitUnknown(node) + return this.visitUnknown(node); } visitTemplateTail(node: ts.TemplateTail) { - return this.visitUnknown(node) + return this.visitUnknown(node); } visitIdentifier(node: ts.Identifier) { @@ -310,7 +310,7 @@ export class JavaScriptParserVisitor { } visitTypeReference(node: ts.TypeReferenceNode) { - return this.visitUnknown(node) + return this.visitUnknown(node); } visitFunctionType(node: ts.FunctionTypeNode) { @@ -1035,7 +1035,7 @@ export class JavaScriptParserVisitor { if (ts.isLiteralExpression(node)) { if (ts.isNumericLiteral(node)) { return JavaType.Primitive.of(JavaType.PrimitiveKind.Int); - } else if (ts.isStringLiteral(node)) { + } else if (ts.isStringLiteral(node) || ts.isRegularExpressionLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) { return JavaType.Primitive.of(JavaType.PrimitiveKind.String); } return JavaType.Primitive.of(JavaType.PrimitiveKind.Void); diff --git a/openrewrite/test/javascript/parser/literal.test.ts b/openrewrite/test/javascript/parser/literal.test.ts index 0e64f98..fa8094c 100644 --- a/openrewrite/test/javascript/parser/literal.test.ts +++ b/openrewrite/test/javascript/parser/literal.test.ts @@ -1,6 +1,7 @@ import * as J from "../../../dist/java/tree"; import * as JS from "../../../dist/javascript/tree"; import {connect, disconnect, javaScript, rewriteRunWithOptions} from '../testHarness'; +import {JavaType} from "../../../dist/java/tree"; describe('literal mapping', () => { beforeAll(() => connect()); @@ -10,32 +11,46 @@ describe('literal mapping', () => { rewriteRunWithOptions( {normalizeIndent: false}, javaScript(' 1', sourceFile => { - assertLiteralLst(sourceFile, '1'); + assertLiteralLst(sourceFile, '1', JavaType.PrimitiveKind.Int); })); }); test('string', () => { rewriteRunWithOptions( {normalizeIndent: false}, javaScript('"1"', sourceFile => { - assertLiteralLst(sourceFile, '"1"'); + assertLiteralLst(sourceFile, '"1"', JavaType.PrimitiveKind.String); })); }); test('boolean', () => { rewriteRunWithOptions( {normalizeIndent: false}, javaScript('true', sourceFile => { - assertLiteralLst(sourceFile, 'true'); + assertLiteralLst(sourceFile, 'true', JavaType.PrimitiveKind.Boolean); })); }); test('null', () => { rewriteRunWithOptions( {normalizeIndent: false}, javaScript('null', sourceFile => { - assertLiteralLst(sourceFile, 'null'); + assertLiteralLst(sourceFile, 'null', JavaType.PrimitiveKind.Null); + })); + }); + test('regex', () => { + rewriteRunWithOptions( + {normalizeIndent: false}, + javaScript('/hello/gi', sourceFile => { + assertLiteralLst(sourceFile, '/hello/gi', JavaType.PrimitiveKind.String); + })); + }); + test('template without substitutions', () => { + rewriteRunWithOptions( + {normalizeIndent: false}, + javaScript('`hello!`', sourceFile => { + assertLiteralLst(sourceFile, '`hello!`', JavaType.PrimitiveKind.String); })); }); - function assertLiteralLst(sourceFile: JS.CompilationUnit, expectedValueSource: string) { + function assertLiteralLst(sourceFile: JS.CompilationUnit, expectedValueSource: string, expectedType: JavaType.PrimitiveKind) { expect(sourceFile).toBeDefined(); expect(sourceFile.statements).toHaveLength(1); let statement = sourceFile.statements[0]; @@ -43,5 +58,6 @@ describe('literal mapping', () => { let expression = (statement as JS.ExpressionStatement).expression; expect(expression).toBeInstanceOf(J.Literal); expect((expression as J.Literal).valueSource).toBe(expectedValueSource); + expect((expression as J.Literal).type.kind).toBe(expectedType); } });