{"record":{"id":"a14d524aa6c7c594","repo":"swc-project/swc","slug":"illegal-conversion-cannot-convert-to-prop","errorCode":null,"errorMessage":"illegal conversion: Cannot convert {:?} to Prop","messagePattern":"illegal conversion: Cannot convert (.+?) to Prop","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_estree_compat/src/babelify/module_decl.rs","lineNumber":113,"sourceCode":"            with: Default::default(),\n            export_kind: Default::default(),\n        }\n    }\n}\n\nfn convert_import_attrs(\n    asserts: Option<Box<ObjectLit>>,\n    ctx: &Context,\n) -> Option<Vec<ImportAttribute>> {\n    asserts.map(|obj| {\n        let obj_span = obj.span;\n\n        obj.props\n            .into_iter()\n            .map(|prop_or_spread| {\n                let prop = match prop_or_spread {\n                    PropOrSpread::Prop(p) => p,\n                    _ => panic!(\n                        \"illegal conversion: Cannot convert {:?} to Prop\",\n                        &prop_or_spread\n                    ),\n                };\n                let (key, val) = match *prop {\n                    Prop::KeyValue(keyval) => {\n                        let key = match keyval.key {\n                            PropName::Ident(i) => IdOrString::Id(i.babelify(ctx)),\n                            PropName::Str(s) => IdOrString::String(s.babelify(ctx)),\n                            _ => panic!(\n                                \"illegal conversion: Cannot convert {:?} to Prop::KeyValue\",\n                                &keyval.key\n                            ),\n                        };\n                        let val = match *keyval.value {\n                            Expr::Lit(lit) => match lit {\n                                Lit::Str(s) => s.babelify(ctx),\n                                _ => panic!(","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_estree_compat/src/babelify/module_decl.rs#L95-L131","documentation":"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.","triggerScenarios":"Parsing `import x from './m' with { ...opts }` (or `assert { ...opts }`) and babelifying the module — the spread prop fails the PropOrSpread::Prop match.","commonSituations":"Test corpora with deliberately malformed import attributes; code generators that splice objects into the with-clause; accepting untrusted JS into an AST conversion pipeline.","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"],"exampleFix":"// before\nimport data from './data.json' with { ...opts };\n\n// after\nimport data from './data.json' with { type: 'json' };","handlingStrategy":"validation","validationCode":"fn import_attrs_ok(module: &Module) -> Result<(), String> {\n    for item in &module.body {\n        if let ModuleItem::ModuleDecl(ModuleDecl::Import(i)) = item {\n            if let Some(with) = &i.with {\n                for p in &with.props {\n                    if !matches!(p, PropOrSpread::Prop(_)) {\n                        return Err(format!(\"spread in import attribute clause at {:?}\", with.span));\n                    }\n                }\n            }\n        }\n    }\n    Ok(())\n}","typeGuard":"fn prop_or_spread_is_prop(p: &PropOrSpread) -> bool {\n    matches!(p, PropOrSpread::Prop(_))\n}","tryCatchPattern":"let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| program.babelify(&ctx)))\n    .map_err(|_| anyhow::anyhow!(\"invalid import attribute clause (spread element)\"))?;","preventionTips":["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"],"tags":["swc","babelify","panic","import-attributes","spread-element","illegal-conversion"],"backgroundTag":"invalid-import-attributes","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}