swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
30.96k stars 1.21k forks source link

minifier: incorrectly alters function length by removing the initializer values. #9356

Closed tmdghks closed 1 month ago

tmdghks commented 1 month ago

Describe the bug

A minifier should not delete the default value of a function argument when evaluating the length of the function, as this can change the function's length.

In the following input code, there is an argument with the initializer 42. Therefore, the length of the function should be 0 because function length does not count the argument with initializer. However, the minifier deletes the initializer of this argument, which inadvertently changes the function's length.

Input code

console.log((function ({ } = 42) { }).length); // its output value is 0

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": true,
        "conditionals": true,
        "dead_code": true,
        "directives": true,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": true,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": true,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": true,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": true,
        "negate_iife": true,
        "properties": true,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": true,
        "switches": true,
        "typeofs": true,
        "unsafe": false,
        "unsafe_arrows": false,
        "unsafe_comps": false,
        "unsafe_Function": false,
        "unsafe_math": false,
        "unsafe_symbols": false,
        "unsafe_methods": false,
        "unsafe_proto": false,
        "unsafe_regexp": false,
        "unsafe_undefined": false,
        "unused": true,
        "const_to_let": true,
        "pristine_globals": true
      },
      "mangle": false
    }
  },
  "module": {
    "type": "commonjs"
  },
  "minify": false,
  "isModule": false
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.4-nightly-20240729.1&code=H4sIAAAAAAAAA0vOzyvOz0nVy8lP19BIK81LLsnMz1PQqFaoVbBVMDHSVACyNPVyUvPSSzI0rbkA287lnS8AAAA%3D&config=H4sIAAAAAAAAA32UO5LbMAyG%2Bz2FR3WKjMscIF3OwKFJUKZDEhoC9Fqz47sHeviRNeROwocfIAEQXx%2B7XXci1%2F3afcmn%2FAy2EtT7v1hoLGwvYunAZUuuxoG7Hzd6ogkFmwhm03UhHdvaA88q2v%2Fc71dFlxAJborVlmOJYXzO6TAPFYiebGKVkC1DYfpfv7KKnxPg2p7tB8QEtrwhxpKJhaGHqgV2mJIdCMzZViXKdFJbI6GWYoKNwZuh4qDy4iNHLJLzlXqw3jj0oKBYwXE8gyaTXCIrJNdT7jNjD4fW93Ofv6nhbFOzrOSEy9wSOa0S9YiR2IRWtBIucKMGC1yL%2B10Zg6nArZZX3Qlj2ejJXwCpQLJExWbQ4s4eQeZpSx3eKmMJMrI8KlzmW7tlgV6KamIMSmWnykDlqHWzgm8Opso67Tgr3igfRQ8GQpBZUULTZ2R31JLyOAAGBUh%2FbdCmagHm%2Fgo3%2BPQg3uDfckvWB2z1yJaP25TGfMD0JkEGPqJ%2F4yCtYNzGVbbEZdjmrXiQ0QCvujSawesSkAfAaNK8L19mQ56HRDR9wsNjTawO1%2Fsezrb0j%2Fe%2BrOKP1aHL6NsM1yU%2F9XdazdKOjOVE3cPztorvp%2B8i%2FbnJl9jXf3MIldo2BgAA

SWC Info output

No response

Expected behavior

console.log((function({}=42) {}).length); // it should be preserved

Actual behavior

console.log((function({}) {}.length); // its output value is 1

Version

1.7.4-nightly-20240729.1

Additional context

In addition, this removing process is only occurring when using assignment form like {} = something. However, when using assignment form like [] = 42, a = 2, the minifier doesn't remove the initializer.

swc-bot commented 2 weeks ago

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.