{"record":{"id":"7160a22f6d6e6c59","repo":"swc-project/swc","slug":"illegal-conversion-cannot-convert-to-key-va","errorCode":null,"errorMessage":"illegal conversion: Cannot convert {:?} to key, value","messagePattern":"illegal conversion: Cannot convert (.+?) to key, value","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_estree_compat/src/babelify/module_decl.rs","lineNumber":143,"sourceCode":"                                &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!(\n                                    \"illegal conversion: Cannot convert {:?} to Expr::Lit\",\n                                    &lit\n                                ),\n                            },\n                            _ => panic!(\n                                \"illegal conversion: Cannot convert {:?} to Expr::Lit\",\n                                &keyval.value\n                            ),\n                        };\n                        (key, val)\n                    }\n                    _ => panic!(\n                        \"illegal conversion: Cannot convert {:?} to key, value\",\n                        &prop\n                    ),\n                };\n                ImportAttribute {\n                    base: ctx.base(obj_span),\n                    key,\n                    value: val,\n                }\n            })\n            .collect()\n    })\n}\n\nimpl Babelify for ImportDecl {\n    type Output = ImportDeclaration;\n\n    fn babelify(self, ctx: &Context) -> Self::Output {","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_estree_compat/src/babelify/module_decl.rs#L125-L161","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nimport x from './m' with { type };\n\n// after\nimport x from './m' with { type: 'json' };","handlingStrategy":"validation","validationCode":"fn import_attr_props_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 let PropOrSpread::Prop(prop) = p {\n                        if !matches!(&**prop, Prop::KeyValue(_)) {\n                            return Err(\"import attribute entry must be key: value\".into());\n                        }\n                    }\n                }\n            }\n        }\n    }\n    Ok(())\n}","typeGuard":"fn prop_is_key_value(p: &Prop) -> bool {\n    matches!(p, Prop::KeyValue(_))\n}","tryCatchPattern":"let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| program.babelify(&ctx)))\n    .map_err(|_| anyhow::anyhow!(\"invalid import attribute entry (must be KeyValue)\"))?;","preventionTips":["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"],"tags":["swc","babelify","panic","import-attributes","shorthand-property","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-14T00:17:10.932Z"}