{"record":{"id":"ea9124e65ccd12e6","repo":"swc-project/swc","slug":"illegal-conversion-cannot-convert-to-expr-l","errorCode":null,"errorMessage":"illegal conversion: Cannot convert {:?} to Expr::Lit","messagePattern":"illegal conversion: Cannot convert (.+?) to Expr::Lit","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_estree_compat/src/babelify/module_decl.rs","lineNumber":131,"sourceCode":"                    _ => 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)\n                    }\n                    _ => panic!(\n                        \"illegal conversion: Cannot convert {:?} to key, value\",\n                        &prop\n                    ),\n                };\n                ImportAttribute {\n                    base: ctx.base(obj_span),","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_estree_compat/src/babelify/module_decl.rs#L113-L149","documentation":"Import attribute values must be string literals per the import-attributes spec, and this conversion only maps Lit::Str. If the with-clause object contains a non-string literal value — a number, boolean, null, regex, or BigInt — the inner match panics with 'illegal conversion: Cannot convert ... to Expr::Lit'. The Babel ImportAttribute simply has no field for non-string literal values, so the converter refuses rather than lying about the AST.","triggerScenarios":"Parsing and babelifying `import x from './m' with { type: 42 }` or `with { type: true }` — any literal other than a string as the attribute value.","commonSituations":"Typos where developers drop quotes in the with-clause; codegen emitting unquoted values; malformed inputs hitting AST conversion tooling.","solutions":["Fix the source: quote the attribute value (`with { type: 'json' }`)","Pre-validate the with-clause: each value must be Expr::Lit(Lit::Str)","Report a clear syntax-level error for such files instead of letting babelify panic","Catch the panic via catch_unwind in batch converters"],"exampleFix":"// before\nimport x from './x.json' with { type: 42 };\n\n// after\nimport x from './x.json' with { type: 'json' };","handlingStrategy":"validation","validationCode":"fn import_attr_values_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.value, Expr::Lit(lit) if matches!(**lit, Lit::Str(_))) {\n                                return Err(\"import attribute value must be a string literal\".into());\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    Ok(())\n}","typeGuard":"fn lit_is_attr_string(l: &Lit) -> bool {\n    matches!(l, Lit::Str(_))\n}","tryCatchPattern":"let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| program.babelify(&ctx)))\n    .map_err(|_| anyhow::anyhow!(\"invalid import attribute value (must be string literal)\"))?;","preventionTips":["Always quote import attribute values","Validate with-clause values in ingestion pipelines","Add a lint rule for import attributes if your tool consumes third-party JS"],"tags":["swc","babelify","panic","import-attributes","literal-value","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"}