swc-project/swc · error

illegal conversion: Cannot convert {:?} to ObjectMember

Error message

illegal conversion: Cannot convert {:?} to ObjectMember

What it means

babelify maps object-literal members to Babel ObjectMember for Shorthand, KeyValue, Getter, Setter and Method props. The catch-all panics for `Prop::Assign` — an assignment inside an object literal, which swc_ecma_ast itself documents as invalid for object literals — and for any unknown Prop variant, since ObjectMember has no shape for them. Unlike the cfg-gated arms, this `_` is always compiled.

Source

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

    fn babelify(self, ctx: &Context) -> Self::Output {
        match self {
            Prop::Shorthand(i) => {
                let id = i.babelify(ctx);
                ObjectMember::Prop(ObjectProperty {
                    base: id.base.clone(),
                    key: ObjectKey::Id(id.clone()),
                    value: ObjectPropVal::Expr(Box::alloc().init(Expression::Id(id))),
                    computed: Default::default(),
                    shorthand: true,
                    decorators: Default::default(),
                })
            }
            Prop::KeyValue(k) => ObjectMember::Prop(k.babelify(ctx)),
            Prop::Getter(g) => ObjectMember::Method(g.babelify(ctx)),
            Prop::Setter(s) => ObjectMember::Method(s.babelify(ctx)),
            Prop::Method(m) => ObjectMember::Method(m.babelify(ctx)),
            _ => panic!(
                "illegal conversion: Cannot convert {:?} to ObjectMember",
                &self
            ),
        }
    }
}

impl Babelify for KeyValueProp {
    type Output = ObjectProperty;

    fn babelify(self, ctx: &Context) -> Self::Output {
        ObjectProperty {
            base: ctx.base(self.span()),
            key: self.key.babelify(ctx),
            value: ObjectPropVal::Expr(Box::alloc().init(self.value.babelify(ctx).into())),
            computed: Default::default(),
            shorthand: Default::default(),
            decorators: Default::default(),

View on GitHub (pinned to 5176682b65)

Solutions

  1. Validate the AST for `Prop::Assign` nodes before babelify and reject or repair the input
  2. Fix the source that produced an assignment inside an object literal
  3. In transforms, emit KeyValue props or object patterns instead of AssignProp
  4. For plugin builds, drop `--cfg=swc_ast_unknown` on babelify paths

Example fix

// before
let estree = module.babelify(); // panics on `{ x = 1 }` recovered into Prop::Assign

// after - detect and reject first
if module.has_assign_prop() {
    return Err(anyhow!("Prop::Assign is not valid in an object literal"));
}
let estree = module.babelify();
Defensive patterns

Strategy: validation

Validate before calling

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

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

impl Visit for AssignProps {
    fn visit_prop(&mut self, p: &Prop) {
        if let Prop::Assign(a) = p { self.0.push(a.span); }
        p.visit_children_with(self);
    }
}

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

Type guard

fn object_lit_member_ok(p: &Prop) -> bool {
    !matches!(p, Prop::Assign(_))
}

Try / catch

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

catch_unwind(AssertUnwindSafe(|| program.babelify()))
    .map_err(|_| anyhow!("Prop::Assign cannot appear in an object literal"))?;

Prevention

When it happens

Trigger: Babelify an ObjectLit containing `Prop::Assign`, produced by error recovery from `{ x = 1 }`-style broken input or by a transform/codemod that builds AssignProp nodes; the same arm also fires for unknown variants under `swc_ast_unknown` builds.

Common situations: Codemods that normalize object patterns into literals; lenient parsing of experimental `{ x = 1 }` pattern syntax; version skew under the swc_ast_unknown cfg; fuzzed AST corpora.

Related errors


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