swc-project/swc · error

illegal conversion: Cannot convert {:?} to key, value

Error message

illegal conversion: Cannot convert {:?} to key, value

What it means

convert_import_attrs expects every property of a with/assert object to be a plain KeyValue pair, because Babel's ImportAttribute is exactly one key plus one string value. Shorthand (`with { type }`), methods (`with { f() {} }`), getters/setters, or any other Prop shape reaches this match and panics with 'illegal conversion: Cannot convert ... to key, value'. The panic identifies a property form that has no import-attribute meaning.

Source

Thrown at crates/swc_estree_compat/src/babelify/module_decl.rs:143

                                &keyval.key
                            ),
                        };
                        let val = match *keyval.value {
                            Expr::Lit(lit) => match lit {
                                Lit::Str(s) => s.babelify(ctx),
                                _ => panic!(
                                    "illegal conversion: Cannot convert {:?} to Expr::Lit",
                                    &lit
                                ),
                            },
                            _ => panic!(
                                "illegal conversion: Cannot convert {:?} to Expr::Lit",
                                &keyval.value
                            ),
                        };
                        (key, val)
                    }
                    _ => panic!(
                        "illegal conversion: Cannot convert {:?} to key, value",
                        &prop
                    ),
                };
                ImportAttribute {
                    base: ctx.base(obj_span),
                    key,
                    value: val,
                }
            })
            .collect()
    })
}

impl Babelify for ImportDecl {
    type Output = ImportDeclaration;

    fn babelify(self, ctx: &Context) -> Self::Output {

View on GitHub (pinned to 5176682b65)

Solutions

  1. Fix the source: write attribute entries as explicit `key: "value"` pairs
  2. Pre-validate the with-clause object: only Prop::KeyValue entries are babelifiable
  3. Reject such files with a clear error before conversion
  4. Guard batch conversion with catch_unwind

Example fix

// before
import x from './m' with { type };

// after
import x from './m' with { type: 'json' };
Defensive patterns

Strategy: validation

Validate before calling

fn import_attr_props_ok(module: &Module) -> Result<(), String> {
    for item in &module.body {
        if let ModuleItem::ModuleDecl(ModuleDecl::Import(i)) = item {
            if let Some(with) = &i.with {
                for p in &with.props {
                    if let PropOrSpread::Prop(prop) = p {
                        if !matches!(&**prop, Prop::KeyValue(_)) {
                            return Err("import attribute entry must be key: value".into());
                        }
                    }
                }
            }
        }
    }
    Ok(())
}

Type guard

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

Try / catch

let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| program.babelify(&ctx)))
    .map_err(|_| anyhow::anyhow!("invalid import attribute entry (must be KeyValue)"))?;

Prevention

When it happens

Trigger: Parsing and babelifying `import x from './m' with { type }` (shorthand), `with { type() {} }` (method), or `with { get type() {} }` — any Prop variant other than KeyValue inside the attribute object.

Common situations: Unquoted/malformed with-clauses written by hand; codegen that copies object-literal shorthand into import statements; fuzzing inputs that explore the parser's acceptance of object syntax in with-clauses.

Related errors


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