{"record":{"id":"05ee540016ac43ea","repo":"swc-project/swc","slug":"illegal-conversion-cannot-convert-to-prop-k","errorCode":null,"errorMessage":"illegal conversion: Cannot convert {:?} to Prop::KeyValue","messagePattern":"illegal conversion: Cannot convert (.+?) to Prop::KeyValue","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_estree_compat/src/babelify/module_decl.rs","lineNumber":123,"sourceCode":"    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!(\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)","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_estree_compat/src/babelify/module_decl.rs#L105-L141","documentation":"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.","triggerScenarios":"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.","commonSituations":"Fuzzed or adversarial JS inputs, templated code that interpolates dynamic keys into with-clauses, hand-written fixtures exercising unusual syntax.","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"],"exampleFix":"// before\nimport x from './m' with { [type]: 'json' };\n\n// after\nimport x from './m' with { type: 'json' };","handlingStrategy":"validation","validationCode":"fn import_attr_keys_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 let Prop::KeyValue(kv) = &**prop {\n                            if !matches!(kv.key, PropName::Ident(_) | PropName::Str(_)) {\n                                return Err(\"non-Ident/Str key in import attributes\".into());\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    Ok(())\n}","typeGuard":"fn prop_name_is_attr_key(k: &PropName) -> bool {\n    matches!(k, PropName::Ident(_) | PropName::Str(_))\n}","tryCatchPattern":"let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| program.babelify(&ctx)))\n    .map_err(|_| anyhow::anyhow!(\"invalid import attribute key (must be identifier or string)\"))?;","preventionTips":["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"],"tags":["swc","babelify","panic","import-attributes","computed-key","prop-name","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"}