swc-project/swc · error

illegal conversion: Cannot convert {:?} to ObjectKey

Error message

illegal conversion: Cannot convert {:?} to ObjectKey

What it means

babelify maps PropName to Babel ObjectKey for Ident, Str, Num and Computed — but `PropName::BigInt` has no arm, so an object literal with a BigInt property key panics. This is reachable with plain, valid JavaScript (`({ 100n: 1 })`); Babel itself represents such keys as string keys. The `_` also catches unknown variants under `swc_ast_unknown` builds.

Source

Thrown at crates/swc_estree_compat/src/babelify/prop.rs:129

impl Babelify for MethodProp {
    type Output = ObjectMethod;

    fn babelify(self, ctx: &Context) -> Self::Output {
        babelify_object_method(self.key, self.function, ObjectMethodKind::Method, ctx)
    }
}

impl Babelify for PropName {
    type Output = ObjectKey;

    fn babelify(self, ctx: &Context) -> Self::Output {
        match self {
            PropName::Ident(i) => ObjectKey::Id(i.babelify(ctx)),
            PropName::Str(s) => ObjectKey::String(s.babelify(ctx)),
            PropName::Num(n) => ObjectKey::Numeric(n.babelify(ctx)),
            PropName::Computed(e) => ObjectKey::Expr(Box::alloc().init(e.babelify(ctx))),
            _ => panic!(
                "illegal conversion: Cannot convert {:?} to ObjectKey",
                &self
            ),
        }
    }
}

impl Babelify for ComputedPropName {
    type Output = Expression;

    fn babelify(self, ctx: &Context) -> Self::Output {
        self.expr.babelify(ctx).into()
    }
}

View on GitHub (pinned to 5176682b65)

Solutions

  1. Upgrade swc_estree_compat; check the changelog for a BigInt PropName -> ObjectKey mapping and pick a release that includes it
  2. Preprocess: rewrite `PropName::BigInt` into a string or computed key with a VisitMut pass before babelify
  3. Reject inputs containing BigInt property keys until your swc version covers them
  4. Patch prop.rs to map PropName::BigInt to a StringLiteral key the way Babel does, and upstream it

Example fix

// before (crates/swc_estree_compat/src/babelify/prop.rs)
PropName::Computed(e) => ObjectKey::Expr(Box::alloc().init(e.babelify(ctx))),
_ => panic!("illegal conversion: Cannot convert {:?} to ObjectKey", &self),

// after - map BigInt keys like Babel (string key)
PropName::BigInt(b) => ObjectKey::String(Str {
    span: b.span,
    value: b.value.to_string().into(),
    raw: None,
}.babelify(ctx)),
Defensive patterns

Strategy: validation

Validate before calling

use swc_ecma_ast::{Module, PropName};
use swc_ecma_visit::{Visit, VisitWith};

#[derive(Default)]
struct BigIntKeys(Vec<swc_common::Span>);

impl Visit for BigIntKeys {
    fn visit_prop_name(&mut self, n: &PropName) {
        if let PropName::BigInt(b) = n { self.0.push(b.span); }
        n.visit_children_with(self);
    }
}

fn has_bigint_keys(m: &Module) -> bool {
    let mut v = BigIntKeys::default();
    m.visit_with(&mut v);
    !v.0.is_empty()
}

Type guard

fn prop_name_convertible(n: &PropName) -> bool {
    !matches!(n, PropName::BigInt(_))
}

Try / catch

use std::panic::{catch_unwind, AssertUnwindSafe};

catch_unwind(AssertUnwindSafe(|| program.babelify()))
    .map_err(|_| anyhow!("BigInt property key is not mapped by this swc_estree_compat version"))?;

Prevention

When it happens

Trigger: Babelify source containing a BigInt literal in property-key position: `({ 100n: 1 })`, `{ 0b1101n: v }`, `{ 0x1fn: v }` — no special feature flags or error recovery needed.

Common situations: Codebases using BigInt keys for bitmask/hash tables; snapshot or AST-viewer tools that babelify arbitrary fixture corpora; upgrading swc without checking estree-compat coverage for newer syntax.

Related errors


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