swc-project/swc · error

failed to parse the value of BigIntLiteral

Error message

failed to parse the value of BigIntLiteral

What it means

Thrown while converting an ESTree BigIntLiteral into swc's BigInt node (swc_estree_compat `swcify`). The `value` string is passed straight to `num_bigint::BigInt::from_str`, which only accepts an optional sign followed by plain decimal digits; anything else returns Err and the `.expect(...)` panics, aborting the process. ESTree producers (acorn) put the raw literal text in `value`, e.g. "123n", and that trailing 'n' alone makes the parse fail. Radix prefixes (0x/0b/0o), underscores, whitespace, and empty strings fail the same way.

Source

Thrown at crates/swc_estree_compat/src/swcify/lit.rs:145

            span: ctx.span(&self.base),
            tail: self.tail,
            cooked: self.value.cooked,
            raw: self.value.raw,
        }
    }
}

impl Swcify for BigIntLiteral {
    type Output = BigInt;

    fn swcify(self, ctx: &Context) -> Self::Output {
        BigInt {
            span: ctx.span(&self.base),
            value: self
                .value
                .parse()
                .map(Box::new)
                .expect("failed to parse the value of BigIntLiteral"),
            // TODO improve me
            raw: None,
        }
    }
}

impl Swcify for DecimalLiteral {
    type Output = Number;

    fn swcify(self, ctx: &Context) -> Self::Output {
        Number {
            span: ctx.span(&self.base),
            value: self
                .value
                .parse()
                .expect("failed to parse the value of DecimalLiteral"),
            // TODO improve me
            raw: None,

View on GitHub (pinned to 5176682b65)

Solutions

  1. Normalize the value before converting: strip a trailing 'n' (and any radix prefix / '_' separators) so only an optional sign plus decimal digits remain
  2. If the AST came from acorn, copy the `bigint` property (digits only) into `value` before swcify
  3. If you build the ESTree AST yourself, emit value as the digits-only decimal string
  4. Patch the crate to use a fallible conversion (map the Err into a diagnostic instead of expect) and report the upstream issue

Example fix

// before
// node = acorn BigIntLiteral: { type: 'BigIntLiteral', value: '9007199254740993n', bigint: '9007199254740993' }
swcify(node); // panics: failed to parse the value of BigIntLiteral

// after
if (node.type === 'BigIntLiteral' && node.bigint != null) {
  node.value = node.bigint; // digits only, no 'n'
}
swcify(node);
Defensive patterns

Strategy: validation

Validate before calling

// before swcify, check every BigIntLiteral value parses as a bigint
function bigintValueOk(node) {
  if (node.type !== 'BigIntLiteral') return true;
  const digits = String(node.value).replace(/n$/, '');
  return /^[+-]?\d+$/.test(digits);
}
const bad = nodes.filter(n => !bigintValueOk(n));
if (bad.length) throw new Error(`unparseable BigIntLiteral value: ${bad[0].value}`);

Type guard

function isParseableBigIntLiteral(node: unknown): boolean {
  if (typeof node !== 'object' || node === null) return false;
  const n = node as { type?: string; value?: unknown; bigint?: unknown };
  if (n.type !== 'BigIntLiteral') return false;
  if (typeof n.bigint === 'string') return /^[+-]?\d+$/.test(n.bigint);
  return typeof n.value === 'string' && /^[+-]?\d+$/.test(n.value.replace(/n$/, ''));
}

Try / catch

// Rust callers can keep the panic from aborting the process:
let converted = std::panic::catch_unwind(|| estree_ast.swcify(&ctx))
    .map_err(|p| panic_message(&p)) // convert to a normal error
    .and_then(|ast| Ok(ast));
// prefer pre-validating values instead; catch_unwind is a last resort

Prevention

When it happens

Trigger: Calling `.swcify(ctx)` (directly or via a full estree->swc AST conversion) on a tree containing a BigInt literal whose `value` is not bare decimal digits: acorn-style value="123n", value="0xffn", value="1_000n", value="", or value with surrounding whitespace.

Common situations: Feeding acorn / estree-toolkit / serialized ESTree JSON into swc_estree_compat; AST producers that keep the suffix in `value` instead of using acorn's dedicated `bigint` field; hand-written AST fixtures with placeholder values; partial/malformed ASTs where value was never populated.

Understand the failure class

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/4df02b6a6420ad0d. Report an issue: GitHub.