Skip to content
Closed
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/oxc_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ assert-unchecked = { workspace = true }
bitflags = { workspace = true }
cow-utils = { workspace = true }
nonmax = { workspace = true }
num-bigint = { workspace = true }
num-traits = { workspace = true }
rustc-hash = { workspace = true }

[dev-dependencies]
Expand Down
48 changes: 46 additions & 2 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::ops::Not;

use cow_utils::CowUtils;
use num_bigint::BigInt;
use num_traits::Num;
use oxc_ast::ast::*;
use oxc_span::GetSpan;
use oxc_syntax::{
Expand Down Expand Up @@ -1162,13 +1164,55 @@ impl<'a> GenExpr for NumericLiteral<'a> {
}

impl<'a> Gen for BigIntLiteral<'a> {
fn gen(&self, p: &mut Codegen, _ctx: Context) {
fn gen(&self, p: &mut Codegen, ctx: Context) {
if self.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());

if ctx.contains(Context::TYPESCRIPT) {
p.print_str(self.raw.as_str());
} else {
let str = self
.raw
.as_str()
.strip_suffix("n")
.map(|s| {
let prefix_size = match self.base {
BigintBase::Decimal => 0,
BigintBase::Binary | BigintBase::Hex | BigintBase::Octal => 2,
};

&s[prefix_size..]
})
.and_then(|s| BigInt::from_str_radix(s, self.base.base()).ok());

if let Some(big_int) = str {
if p.options.minify {
if big_int >= 10_000_000_000_000_000_i64.into() {
p.print_str("0x");
p.print_str(&big_int.to_str_radix(16));
} else if big_int <= (-10_000_000_000_000_000_i64).into() {
p.print_str("-0x");
p.print_str(&(-big_int).to_str_radix(16));
} else {
p.print_str(&big_int.to_string());
}
} else {
p.print_str(&big_int.to_string());
}
} else {
#[cfg(debug_assertions)]
{
// This should never happen, we should've validated that the BigInt is valid in the parser.
unreachable!("Invalid BigIntLiteral: {:?}", self);
}
#[cfg(not(debug_assertions))]
p.print_str(self.raw.as_str());
}
p.print_ascii_byte(b'n');
}
}
}

Expand Down
64 changes: 64 additions & 0 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,67 @@ fn in_expr_in_sequence_in_for_loop_init() {
"for ((\"hidden\" in a) && (m = a.hidden), r = 0; s > r; r++) {}\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]
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
Expand Up @@ -10,7 +10,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
9 changes: 9 additions & 0 deletions crates/oxc_syntax/src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ impl BigintBase {
pub fn is_base_10(&self) -> bool {
self == &Self::Decimal
}

pub fn base(&self) -> u32 {
match self {
BigintBase::Decimal => 10,
BigintBase::Binary => 2,
BigintBase::Octal => 8,
BigintBase::Hex => 16,
}
}
}

/// <https://tc39.es/ecma262/#sec-numeric-types-number-tostring>
Expand Down