Skip to content

Commit

Permalink
Map ts.NullLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 25, 2024
1 parent 71d1be5 commit 7d3f904
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
21 changes: 19 additions & 2 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
}
Expand Down
40 changes: 26 additions & 14 deletions openrewrite/test/javascript/parser/literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

0 comments on commit 7d3f904

Please sign in to comment.