Skip to content

Commit

Permalink
Implement a few more primary expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 25, 2024
1 parent 6fc7319 commit bcd7851
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
20 changes: 10 additions & 10 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -310,7 +310,7 @@ export class JavaScriptParserVisitor {
}

visitTypeReference(node: ts.TypeReferenceNode) {
return this.visitUnknown(node)
return this.visitUnknown(node);
}

visitFunctionType(node: ts.FunctionTypeNode) {
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 21 additions & 5 deletions openrewrite/test/javascript/parser/literal.test.ts
Original file line number Diff line number Diff line change
@@ -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());
Expand All @@ -10,38 +11,53 @@ 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];
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);
expect((expression as J.Literal).type.kind).toBe(expectedType);
}
});

0 comments on commit bcd7851

Please sign in to comment.