Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions rewrite-javascript/rewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,12 @@ export class JavaScriptParserVisitor {
visitNumericLiteral(node: ts.NumericLiteral): J.Literal {
// Parse the numeric value from the text
const text = node.text;
let value: number | bigint;
let value: number | bigint | string;

// Check if it's a BigInt literal (ends with 'n')
if (text.endsWith('n')) {
value = BigInt(text.slice(0, -1));
// TODO consider adding `JS.Literal`
value = text.slice(0, -1);
} else if (text.includes('.') || text.toLowerCase().includes('e')) {
// Floating point number
value = parseFloat(text);
Expand Down Expand Up @@ -708,7 +709,8 @@ export class JavaScriptParserVisitor {
visitBigIntLiteral(node: ts.BigIntLiteral): J.Literal {
// Parse BigInt value, removing the 'n' suffix
const text = node.text;
const value = BigInt(text.slice(0, -1));
// TODO consider adding `JS.Literal`
const value = text.slice(0, -1);
return this.mapLiteral(node, value);
}

Expand All @@ -717,6 +719,7 @@ export class JavaScriptParserVisitor {
}

visitRegularExpressionLiteral(node: ts.RegularExpressionLiteral): J.Literal {
// TODO consider adding `JS.Literal`
return this.mapLiteral(node, node.text); // FIXME value not in AST
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const spec = new RecipeSpec();
describe.each([
['1', Type.Primitive.Double],
['1.0', Type.Primitive.Double],
['123n', Type.Primitive.Long],
['"1"', Type.Primitive.String],
['true', Type.Primitive.Boolean],
['null', Type.Primitive.Null],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('JavaScript type mapping', () => {
test('should map bigint literal', async () => {
const spec = new RecipeSpec();
spec.recipe = markTypes((node, type) => {
if (node?.kind === J.Kind.Literal && typeof (node as J.Literal).value === 'bigint') {
if (node?.kind === J.Kind.Literal && typeof (node as J.Literal).value === 'string') {
return formatPrimitiveType(type);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class JavaScriptParserTest {

@BeforeEach
void before() {
// JavaScriptRewriteRpc.setFactory(JavaScriptRewriteRpc.builder().trace(true));
// JavaScriptRewriteRpc.setFactory(JavaScriptRewriteRpc.builder().inspectBrk(Paths.get("rewrite")));
this.parser = JavaScriptParser.builder().build();
this.ctx = new InMemoryExecutionContext();
}
Expand All @@ -56,7 +56,7 @@ void after() {
void helloJavaScript() {
@Language("js")
String helloWorld = """
console.info("Hello world!")
console.info("Hello " + 123n)
""";
Parser.Input input = Parser.Input.fromString(Path.of("helloworld.js"), helloWorld);
Optional<SourceFile> javascript = parser.parseInputs(List.of(input), null, ctx).findFirst();
Expand Down