diff --git a/.changeset/swc-args-destructure-fix.md b/.changeset/swc-args-destructure-fix.md new file mode 100644 index 000000000000..033a724c4ee8 --- /dev/null +++ b/.changeset/swc-args-destructure-fix.md @@ -0,0 +1,6 @@ +--- +swc_core: patch +swc_ecma_minifier: patch +--- + +fix(es/minifier): mark destructured `let`/`const` bindings as having unknown param count diff --git a/crates/swc_ecma_minifier/src/usage_analyzer/analyzer/mod.rs b/crates/swc_ecma_minifier/src/usage_analyzer/analyzer/mod.rs index e703f91c2b62..952330fcc83a 100644 --- a/crates/swc_ecma_minifier/src/usage_analyzer/analyzer/mod.rs +++ b/crates/swc_ecma_minifier/src/usage_analyzer/analyzer/mod.rs @@ -1513,6 +1513,16 @@ where .var_or_default(var.id.to_id()) .store_param_count(Value::Unknown), } + } else if !matches!(&decl.name, Pat::Ident(_)) { + // Destructuring binding (object/array/rest pattern). The + // bound identifiers are read from runtime properties or + // elements, so we cannot statically determine the arity + // of any callable that reaches them. + for id in find_pat_ids::<_, Id>(&decl.name) { + self.data + .var_or_default(id) + .store_param_count(Value::Unknown); + } } } } diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/11829/config.json b/crates/swc_ecma_minifier/tests/fixture/issues/11829/config.json new file mode 100644 index 000000000000..13417f01ff90 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/11829/config.json @@ -0,0 +1,12 @@ +{ + "unused": true, + "reduce_vars": false, + "reduce_funcs": false, + "inline": false, + "evaluate": false, + "collapse_vars": false, + "side_effects": false, + "keep_fargs": true, + "passes": 1, + "toplevel": false +} diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/11829/destructure-decl-stale-arity/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/11829/destructure-decl-stale-arity/input.js new file mode 100644 index 000000000000..9ca0295fe24f --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/11829/destructure-decl-stale-arity/input.js @@ -0,0 +1,7 @@ +function run(opts) { + let { match } = opts; + if (!match) match = () => true; + return match("hello", "world"); +} + +console.log(run({ match: (a, b) => a + " " + b })); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/11829/destructure-decl-stale-arity/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/11829/destructure-decl-stale-arity/output.js new file mode 100644 index 000000000000..738ccd3d4a84 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/11829/destructure-decl-stale-arity/output.js @@ -0,0 +1,8 @@ +function run(opts) { + let { match } = opts; + if (!match) match = ()=>true; + return match("hello", "world"); +} +console.log(run({ + match: (a, b)=>a + " " + b +}));