From 82773cb455faadea3376756fa247df4c05fc9ba0 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Wed, 20 Nov 2024 09:08:51 +0000 Subject: [PATCH] feat(codegen): remove underscore from bigint (#7367) closes #7285 closes #7286 --- crates/oxc_codegen/src/gen.rs | 6 +- crates/oxc_codegen/tests/integration/unit.rs | 65 +++++++++++++++++++ .../tests/snapshots/as-const.snap | 3 +- .../tests/snapshots/infer-expression.snap | 3 +- 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index cbb5409f6988e..e2f7cc6dd2105 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -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); } } diff --git a/crates/oxc_codegen/tests/integration/unit.rs b/crates/oxc_codegen/tests/integration/unit.rs index 17cf30bbea8aa..dae518dd5bcab 100644 --- a/crates/oxc_codegen/tests/integration/unit.rs +++ b/crates/oxc_codegen/tests/integration/unit.rs @@ -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;"); +} diff --git a/crates/oxc_isolated_declarations/tests/snapshots/as-const.snap b/crates/oxc_isolated_declarations/tests/snapshots/as-const.snap index edb2066d8a11e..db05afc0f607d 100644 --- a/crates/oxc_isolated_declarations/tests/snapshots/as-const.snap +++ b/crates/oxc_isolated_declarations/tests/snapshots/as-const.snap @@ -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 ==================== @@ -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; diff --git a/crates/oxc_isolated_declarations/tests/snapshots/infer-expression.snap b/crates/oxc_isolated_declarations/tests/snapshots/infer-expression.snap index 6a49c344a90bf..3ce34e4905d03 100644 --- a/crates/oxc_isolated_declarations/tests/snapshots/infer-expression.snap +++ b/crates/oxc_isolated_declarations/tests/snapshots/infer-expression.snap @@ -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 ==================== @@ -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: {};