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
- Fix the source: write attribute entries as explicit `key: "value"` pairs
- Pre-validate the with-clause object: only Prop::KeyValue entries are babelifiable
- Reject such files with a clear error before conversion
- 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
- Expand shorthand properties in with-clauses to explicit key: value
- Validate the attribute object's prop kinds before babelify
- Prefer spec-compliant import attributes in all generated code
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
- illegal conversion: Cannot convert {:?} to Prop
- illegal conversion: Cannot convert {:?} to Prop::KeyValue
- illegal conversion: Cannot convert {:?} to Expr::Lit
- illegal conversion: Cannot convert {:?} to BinaryExprOp
- illegal conversion: Cannot convert {:?} to LogicalExprOp
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/7160a22f6d6e6c59.
Report an issue: GitHub.