{"record":{"id":"3c190e91aaf16eb0","repo":"swc-project/swc","slug":"illegal-conversion-cannot-convert-to-babelpa","errorCode":null,"errorMessage":"illegal conversion: Cannot convert {:?} to BabelParam","messagePattern":"illegal conversion: Cannot convert (.+?) to BabelParam","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_estree_compat/src/babelify/function.rs","lineNumber":85,"sourceCode":"                decorators: Some(self.decorators.babelify(ctx)),\n                ..a.babelify(ctx)\n            })),\n            Pat::Rest(r) => BabelParam::Rest(RestElement {\n                base: ctx.base(self.span),\n                decorators: Some(self.decorators.babelify(ctx)),\n                ..r.babelify(ctx)\n            }),\n            Pat::Object(o) => BabelParam::Pat(Pattern::Object(ObjectPattern {\n                base: ctx.base(self.span),\n                decorators: Some(self.decorators.babelify(ctx)),\n                ..o.babelify(ctx)\n            })),\n            Pat::Assign(a) => BabelParam::Pat(Pattern::Assignment(AssignmentPattern {\n                base: ctx.base(self.span),\n                decorators: Some(self.decorators.babelify(ctx)),\n                ..a.babelify(ctx)\n            })),\n            Pat::Expr(_) => panic!(\n                \"illegal conversion: Cannot convert {:?} to BabelParam\",\n                &self.pat\n            ),\n            Pat::Invalid(_) => panic!(\n                \"illegal conversion: Cannot convert {:?} to BabelParam\",\n                &self.pat\n            ),\n            #[cfg(swc_ast_unknown)]\n            _ => panic!(\"unable to access unknown nodes\"),\n        }\n    }\n}\n\nimpl Babelify for ParamOrTsParamProp {\n    type Output = BabelParam;\n\n    fn babelify(self, ctx: &Context) -> Self::Output {\n        match self {","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_estree_compat/src/babelify/function.rs#L67-L103","documentation":"While converting a function Param, swc_estree_compat maps the inner Pat to a BabelParam. Babel function parameters must be an Identifier or a Pattern (array/object/rest/assignment), but SWC's Pat enum also has Pat::Expr, which wraps an arbitrary expression and has no Babel-parameter equivalent. If a Param's pat is Pat::Expr, the conversion is illegal and the code panics rather than emitting a malformed Babel AST. The SWC parser never produces this shape for valid source code, so it almost always comes from programmatically built or transformed ASTs.","triggerScenarios":"Babelifying a Program built by custom code that puts Pat::Expr inside a Param (e.g. a transform or macro that rewrites parameters into expressions), or an AST deserialized from another tool that used the invalid shape.","commonSituations":"Custom swc plugins/transforms that manipulate parameter lists; codemod tooling constructing Params via ..Default::default() and accidentally leaving an Expr; AST fixtures from other ecosystems fed into babelify.","solutions":["Fix the AST producer: parameters must be Pat::Ident/Array/Object/Rest/Assign, never Pat::Expr","If a transform intentionally creates expression-like targets, restructure it to operate on valid parameter patterns before babelify","Add a pre-babelify validation pass over function params and reject/repair Pat::Expr early with a proper error","Wrap babelify in catch_unwind to surface a diagnosable error instead of an abort"],"exampleFix":"// before\nlet param = Param {\n    pat: Pat::Expr(Box::new(Expr::Ident(ident.clone()))),\n    ..Default::default()\n};\n\n// after\nlet param = Param {\n    pat: Pat::Ident(ident.into()),\n    ..Default::default()\n};","handlingStrategy":"type-guard","validationCode":"fn params_babelifiable(program: &Program) -> Result<(), String> {\n    for f in functions(program) {\n        for p in &f.params {\n            if matches!(p.pat, Pat::Expr(_)) {\n                return Err(format!(\"function has expression parameter pattern at {:?}\", p.span));\n            }\n        }\n    }\n    Ok(())\n}","typeGuard":"fn param_pat_babelifiable(pat: &Pat) -> bool {\n    !matches!(pat, Pat::Expr(_) | Pat::Invalid(_))\n}","tryCatchPattern":"let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| program.babelify(&ctx)))\n    .map_err(|_| anyhow::anyhow!(\"illegal BabelParam conversion: parameter pattern is Pat::Expr\"))?;","preventionTips":["Never construct Param with Pat::Expr; parameters are patterns, not expressions","Add a visitor assertion over params in transform test suites","Validate third-party or deserialized ASTs before feeding them to babelify","Write fixture tests covering parameter shapes to catch regressions in AST builders"],"tags":["swc","babelify","panic","function-parameters","invalid-ast","pattern-conversion"],"backgroundTag":"invalid-parameter-pattern","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-08-22T14:17:55.899Z"}