Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions rewrite-javascript/rewrite/src/java/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export namespace Type {
static readonly Double = new Primitive('double');
static readonly Float = new Primitive('float');
static readonly Int = new Primitive('int');
static readonly Long = new Primitive('long');
static readonly BigInt = new Primitive('long');
static readonly Short = new Primitive('short');
static readonly String = new Primitive('String');
static readonly Void = new Primitive('void');
Expand All @@ -168,7 +168,7 @@ export namespace Type {
Primitive.Double,
Primitive.Float,
Primitive.Int,
Primitive.Long,
Primitive.BigInt,
Primitive.Short,
Primitive.String,
Primitive.Void,
Expand All @@ -195,7 +195,7 @@ export namespace Type {
case 'int':
return Primitive.Int;
case 'long':
return Primitive.Long;
return Primitive.BigInt;
case 'short':
return Primitive.Short;
case 'String':
Expand Down
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
4 changes: 2 additions & 2 deletions rewrite-javascript/rewrite/src/javascript/type-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export class JavaScriptTypeMapping {
private wrapperType(declaringType: (Type.FullyQualified & Type.Primitive) | Type.FullyQualified) {
if (declaringType == Type.Primitive.String && this.stringWrapperType) {
return this.getType(this.stringWrapperType) as Type.FullyQualified;
} else if ((declaringType == Type.Primitive.Double || declaringType == Type.Primitive.Long) && this.numberWrapperType) {
} else if ((declaringType == Type.Primitive.Double || declaringType == Type.Primitive.BigInt) && this.numberWrapperType) {
return this.getType(this.numberWrapperType) as Type.FullyQualified;
} else if (declaringType == Type.Primitive.Boolean && this.booleanWrapperType) {
return this.getType(this.booleanWrapperType) as Type.FullyQualified;
Expand Down Expand Up @@ -808,7 +808,7 @@ export class JavaScriptTypeMapping {
type.flags === ts.TypeFlags.BigIntLiteral ||
type.flags === ts.TypeFlags.BigIntLike
) {
return Type.Primitive.Long;
return Type.Primitive.BigInt;
} else if (
(type.symbol !== undefined && type.symbol === this.regExpSymbol) ||
this.checker.typeToString(type) === "RegExp"
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.BigInt],
['"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