swc-project/swc · error
illegal conversion: Cannot convert {:?} to Prop
Error message
illegal conversion: Cannot convert {:?} to Prop What it means
convert_import_attrs turns the object literal of an import attribute clause (`with { ... }`, formerly `assert { ... }`) into Babel ImportAttribute entries. The spec only allows `key: "string"` pairs, but SWC's parser stores any ObjectLit there, so a spread element (`...x`) inside the clause reaches this match and panics with 'illegal conversion: Cannot convert ... to Prop'. The panic means the input's attribute object is not representable as import attributes.
Source
Thrown at crates/swc_estree_compat/src/babelify/module_decl.rs:113
with: Default::default(),
export_kind: Default::default(),
}
}
}
fn convert_import_attrs(
asserts: Option<Box<ObjectLit>>,
ctx: &Context,
) -> Option<Vec<ImportAttribute>> {
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!(View on GitHub (pinned to 5176682b65)
Solutions
- Fix the source: import attribute clauses may only contain `key: "value"` pairs, no spreads
- Add a pre-babelify validation that walks each ImportDecl's with/assert ObjectLit and rejects non-plain entries with a real error
- If you must process such files, sanitize the with-clause (drop or rewrite spread entries) before babelify
- Wrap babelify in catch_unwind to report the file path instead of aborting the batch
Example fix
// before
import data from './data.json' with { ...opts };
// after
import data from './data.json' with { type: 'json' }; Defensive patterns
Strategy: validation
Validate before calling
fn import_attrs_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 !matches!(p, PropOrSpread::Prop(_)) {
return Err(format!("spread in import attribute clause at {:?}", with.span));
}
}
}
}
}
Ok(())
} Type guard
fn prop_or_spread_is_prop(p: &PropOrSpread) -> bool {
matches!(p, PropOrSpread::Prop(_))
} Try / catch
let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| program.babelify(&ctx)))
.map_err(|_| anyhow::anyhow!("invalid import attribute clause (spread element)"))?; Prevention
- Keep with/assert clauses to spec form: only `key: "value"` pairs
- Validate import attribute objects before estree conversion
- Reject untrusted inputs with unusual with-clauses early
- Lint import attributes in your pipeline if you accept external JS
When it happens
Trigger: Parsing `import x from './m' with { ...opts }` (or `assert { ...opts }`) and babelifying the module — the spread prop fails the PropOrSpread::Prop match.
Common situations: Test corpora with deliberately malformed import attributes; code generators that splice objects into the with-clause; accepting untrusted JS into an AST conversion pipeline.
Related errors
- illegal conversion: Cannot convert {:?} to Prop::KeyValue
- 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/a14d524aa6c7c594.
Report an issue: GitHub.