Skip to content

Commit 721616d

Browse files
Map JS BigInt literals to string values (#6148)
* Map JS `BigInt` literals to string values `BigInt` can't be serialized as JSON. * Adjust test expectation * Rename `Long` constant to `BigInt`
1 parent 39c864b commit 721616d

File tree

6 files changed

+15
-11
lines changed

6 files changed

+15
-11
lines changed

rewrite-javascript/rewrite/src/java/type.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export namespace Type {
154154
static readonly Double = new Primitive('double');
155155
static readonly Float = new Primitive('float');
156156
static readonly Int = new Primitive('int');
157-
static readonly Long = new Primitive('long');
157+
static readonly BigInt = new Primitive('long');
158158
static readonly Short = new Primitive('short');
159159
static readonly String = new Primitive('String');
160160
static readonly Void = new Primitive('void');
@@ -168,7 +168,7 @@ export namespace Type {
168168
Primitive.Double,
169169
Primitive.Float,
170170
Primitive.Int,
171-
Primitive.Long,
171+
Primitive.BigInt,
172172
Primitive.Short,
173173
Primitive.String,
174174
Primitive.Void,
@@ -195,7 +195,7 @@ export namespace Type {
195195
case 'int':
196196
return Primitive.Int;
197197
case 'long':
198-
return Primitive.Long;
198+
return Primitive.BigInt;
199199
case 'short':
200200
return Primitive.Short;
201201
case 'String':

rewrite-javascript/rewrite/src/javascript/parser.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,12 @@ export class JavaScriptParserVisitor {
609609
visitNumericLiteral(node: ts.NumericLiteral): J.Literal {
610610
// Parse the numeric value from the text
611611
const text = node.text;
612-
let value: number | bigint;
612+
let value: number | bigint | string;
613613

614614
// Check if it's a BigInt literal (ends with 'n')
615615
if (text.endsWith('n')) {
616-
value = BigInt(text.slice(0, -1));
616+
// TODO consider adding `JS.Literal`
617+
value = text.slice(0, -1);
617618
} else if (text.includes('.') || text.toLowerCase().includes('e')) {
618619
// Floating point number
619620
value = parseFloat(text);
@@ -708,7 +709,8 @@ export class JavaScriptParserVisitor {
708709
visitBigIntLiteral(node: ts.BigIntLiteral): J.Literal {
709710
// Parse BigInt value, removing the 'n' suffix
710711
const text = node.text;
711-
const value = BigInt(text.slice(0, -1));
712+
// TODO consider adding `JS.Literal`
713+
const value = text.slice(0, -1);
712714
return this.mapLiteral(node, value);
713715
}
714716

@@ -717,6 +719,7 @@ export class JavaScriptParserVisitor {
717719
}
718720

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

rewrite-javascript/rewrite/src/javascript/type-mapping.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export class JavaScriptTypeMapping {
196196
private wrapperType(declaringType: (Type.FullyQualified & Type.Primitive) | Type.FullyQualified) {
197197
if (declaringType == Type.Primitive.String && this.stringWrapperType) {
198198
return this.getType(this.stringWrapperType) as Type.FullyQualified;
199-
} else if ((declaringType == Type.Primitive.Double || declaringType == Type.Primitive.Long) && this.numberWrapperType) {
199+
} else if ((declaringType == Type.Primitive.Double || declaringType == Type.Primitive.BigInt) && this.numberWrapperType) {
200200
return this.getType(this.numberWrapperType) as Type.FullyQualified;
201201
} else if (declaringType == Type.Primitive.Boolean && this.booleanWrapperType) {
202202
return this.getType(this.booleanWrapperType) as Type.FullyQualified;
@@ -808,7 +808,7 @@ export class JavaScriptTypeMapping {
808808
type.flags === ts.TypeFlags.BigIntLiteral ||
809809
type.flags === ts.TypeFlags.BigIntLike
810810
) {
811-
return Type.Primitive.Long;
811+
return Type.Primitive.BigInt;
812812
} else if (
813813
(type.symbol !== undefined && type.symbol === this.regExpSymbol) ||
814814
this.checker.typeToString(type) === "RegExp"

rewrite-javascript/rewrite/test/javascript/parser/literal.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const spec = new RecipeSpec();
2323
describe.each([
2424
['1', Type.Primitive.Double],
2525
['1.0', Type.Primitive.Double],
26+
['123n', Type.Primitive.BigInt],
2627
['"1"', Type.Primitive.String],
2728
['true', Type.Primitive.Boolean],
2829
['null', Type.Primitive.Null],

rewrite-javascript/rewrite/test/javascript/type-mapping.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('JavaScript type mapping', () => {
129129
test('should map bigint literal', async () => {
130130
const spec = new RecipeSpec();
131131
spec.recipe = markTypes((node, type) => {
132-
if (node?.kind === J.Kind.Literal && typeof (node as J.Literal).value === 'bigint') {
132+
if (node?.kind === J.Kind.Literal && typeof (node as J.Literal).value === 'string') {
133133
return formatPrimitiveType(type);
134134
}
135135
return null;

rewrite-javascript/src/integTest/java/org/openrewrite/javascript/JavaScriptParserTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class JavaScriptParserTest {
4242

4343
@BeforeEach
4444
void before() {
45-
// JavaScriptRewriteRpc.setFactory(JavaScriptRewriteRpc.builder().trace(true));
45+
// JavaScriptRewriteRpc.setFactory(JavaScriptRewriteRpc.builder().inspectBrk(Paths.get("rewrite")));
4646
this.parser = JavaScriptParser.builder().build();
4747
this.ctx = new InMemoryExecutionContext();
4848
}
@@ -56,7 +56,7 @@ void after() {
5656
void helloJavaScript() {
5757
@Language("js")
5858
String helloWorld = """
59-
console.info("Hello world!")
59+
console.info("Hello " + 123n)
6060
""";
6161
Parser.Input input = Parser.Input.fromString(Path.of("helloworld.js"), helloWorld);
6262
Optional<SourceFile> javascript = parser.parseInputs(List.of(input), null, ctx).findFirst();

0 commit comments

Comments
 (0)