Skip to content

Commit

Permalink
feat(codegen): remove underscore from bigint
Browse files Browse the repository at this point in the history
closes #7285
closes #7286
  • Loading branch information
Boshen committed Nov 20, 2024
1 parent b6d5c0f commit f3d8b5d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
6 changes: 4 additions & 2 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,12 +1163,14 @@ impl<'a> GenExpr for NumericLiteral<'a> {

impl<'a> Gen for BigIntLiteral<'a> {
fn gen(&self, p: &mut Codegen, _ctx: Context) {
if self.raw.starts_with('-') {
let raw = self.raw.as_str().cow_replace('_', "");
if raw.starts_with('-') {
p.print_space_before_operator(Operator::Unary(UnaryOperator::UnaryNegation));
}

p.print_space_before_identifier();
p.add_source_mapping(self.span.start);
p.print_str(self.raw.as_str());
p.print_str(&raw);
}
}

Expand Down
65 changes: 65 additions & 0 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,68 @@ fn in_expr_in_arrow_function_expression() {
test("() => 'foo' in bar", "() => \"foo\" in bar;\n");
test("() => { ('foo' in bar) }", "() => {\n\t\"foo\" in bar;\n};\n");
}

#[test]
fn big_int() {
test("9007199254740991n;", "9007199254740991n;\n");
test("-9007199254740991n;", "-9007199254740991n;\n");
test("-90_0719_92547_40991n;", "-9007199254740991n;\n");
test("+9007199254740991n;", "+9007199254740991n;\n");
test("1000n", "1000n;\n");
test("-15n", "-15n;\n");

test("100_000_000n;", "100000000n;\n");
test("10000000000000000n;", "10000000000000000n;\n");
test("0n;", "0n;\n");
test("+0n;", "+0n;\n");
test("-0n;", "-0n;\n");

test("0x1_0n;", "0x10n;\n");
test("0x10n;", "0x10n;\n");

test("0b1_01n;", "0b101n;\n");
test("0b101n;", "0b101n;\n");
test("0b101_101n;", "0b101101n;\n");
test("0b10_1n", "0b101n;\n");

test("0o13n;", "0o13n;\n");
test("0o7n", "0o7n;\n");

test("0x2_0n", "0x20n;\n");
test("0xfabn", "0xfabn;\n");
test("0xaef_en;", "0xaefen;\n");
test("0xaefen;", "0xaefen;\n");
}

#[test]
#[ignore = "Minify bigint is not implemented."]
fn big_int_minify() {
test_minify("9007199254740991n", "9007199254740991n;");
test_minify("-9007199254740991n;", "-9007199254740991n;");
test_minify("-90_0719_92547_40991n;", "-9007199254740991n;");
test_minify("+9007199254740991n;", "+9007199254740991n;");
test_minify("1000n", "1000n;");
test_minify("-15n", "-15n;");

test_minify("100_000_000n;", "100000000n;");
test_minify("10000000000000000n;", "0x2386f26fc10000n;");
test_minify("0n;", "0n;");
test_minify("+0n;", "+0n;");
test_minify("-0n;", "-0n;");

test_minify("0x1_0n;", "16n;");
test_minify("0x10n;", "16n;");

test_minify("0b1_01n;", "5n;");
test_minify("0b101n;", "5n;");
test_minify("0b101_101n;", "45n;");
test_minify("0b10_1n", "5n;");

test_minify("0o13n;", "11n;");
test_minify("0o7n", "7n;");

test_minify("0x2_0n", "32n;");
test_minify("0xfabn", "4011n;");
test_minify("0xaef_en;", "44798n;");
test_minify("0xaefen;", "44798n;");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/oxc_isolated_declarations/tests/mod.rs
input_file: crates/oxc_isolated_declarations/tests/fixtures/as-const.ts
snapshot_kind: text
---
```
==================== .D.TS ====================
Expand All @@ -9,7 +10,7 @@ declare const F: {
readonly string: "string";
readonly templateLiteral: "templateLiteral";
readonly number: 1.23;
readonly bigint: -1_2_3n;
readonly bigint: -123n;
readonly boolean: true;
readonly null: null;
readonly undefined: undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/oxc_isolated_declarations/tests/mod.rs
input_file: crates/oxc_isolated_declarations/tests/fixtures/infer-expression.ts
snapshot_kind: text
---
```
==================== .D.TS ====================
Expand All @@ -10,7 +11,7 @@ declare const s: string;
declare const t: string;
declare const b: boolean;
declare let unaryA: number;
declare const unaryB = -1_2n;
declare const unaryB = -12n;
declare const unaryC: unknown;
declare const unaryD: unknown;
declare const unaryE: {};
Expand Down

0 comments on commit f3d8b5d

Please sign in to comment.