swc-project/swc · error
illegal conversion: Cannot convert {:?} to Prop::KeyValue
Error message
illegal conversion: Cannot convert {:?} to Prop::KeyValue What it means
When converting an import attribute clause, each property key must become a Babel IdOrString, and only Ident and Str keys are supported. SWC's parser accepts any PropName in the with-clause object, so a computed key (`{ [k]: 'v' }`), numeric key (`{ 1: 'json' }`), or BigInt key reaches this match and panics. The panic flags an import attribute key that cannot be represented in the Babel ImportAttribute shape.
Source
Thrown at crates/swc_estree_compat/src/babelify/module_decl.rs:123
asserts.map(|obj| {
let obj_span = obj.span;
obj.props
.into_iter()
.map(|prop_or_spread| {
let prop = match prop_or_spread {
PropOrSpread::Prop(p) => p,
_ => panic!(
"illegal conversion: Cannot convert {:?} to Prop",
&prop_or_spread
),
};
let (key, val) = match *prop {
Prop::KeyValue(keyval) => {
let key = match keyval.key {
PropName::Ident(i) => IdOrString::Id(i.babelify(ctx)),
PropName::Str(s) => IdOrString::String(s.babelify(ctx)),
_ => panic!(
"illegal conversion: Cannot convert {:?} to Prop::KeyValue",
&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)View on GitHub (pinned to 5176682b65)
Solutions
- Fix the source: attribute keys must be plain identifiers or string literals
- Validate the with/assert object before babelify: every key must be PropName::Ident or PropName::Str
- Reject files with non-plain attribute keys early in the pipeline with a proper diagnostic
- Guard the batch job with catch_unwind so one bad file doesn't kill the run
Example fix
// before
import x from './m' with { [type]: 'json' };
// after
import x from './m' with { type: 'json' }; Defensive patterns
Strategy: validation
Validate before calling
fn import_attr_keys_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 let Prop::KeyValue(kv) = &**prop {
if !matches!(kv.key, PropName::Ident(_) | PropName::Str(_)) {
return Err("non-Ident/Str key in import attributes".into());
}
}
}
}
}
}
}
Ok(())
} Type guard
fn prop_name_is_attr_key(k: &PropName) -> bool {
matches!(k, PropName::Ident(_) | PropName::Str(_))
} Try / catch
let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| program.babelify(&ctx)))
.map_err(|_| anyhow::anyhow!("invalid import attribute key (must be identifier or string)"))?; Prevention
- Use only identifier or quoted-string keys in with-clauses
- Validate attribute keys before babelify when accepting generated code
- Watch for templated code interpolating dynamic keys into import statements
When it happens
Trigger: Parsing and babelifying `import x from './m' with { [type]: 'json' }` or `with { 123: 'json' }` — any PropName variant other than Ident/Str in the attribute object.
Common situations: Fuzzed or adversarial JS inputs, templated code that interpolates dynamic keys into with-clauses, hand-written fixtures exercising unusual syntax.
Related errors
- illegal conversion: Cannot convert {:?} to Prop
- illegal conversion: Cannot convert {:?} to Expr::Lit
- illegal conversion: Cannot convert {:?} to key, value
- 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/05ee540016ac43ea.
Report an issue: GitHub.