Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minifier: incorrectly removes functions and classes used in for statements #9466

Closed
tmdghks opened this issue Aug 20, 2024 · 1 comment · Fixed by #9474
Closed

minifier: incorrectly removes functions and classes used in for statements #9466

tmdghks opened this issue Aug 20, 2024 · 1 comment · Fixed by #9474
Assignees
Labels
Milestone

Comments

@tmdghks
Copy link

tmdghks commented Aug 20, 2024

Describe the bug

It appears that the minifier is incorrectly removing functions and classes that are reused within a for statement.
In the following example, both function x and class y are used inside for statements, and therefore should not be removed by the minifier. However, the minifier removes them, causing the code to break.

Input code

"use strict";
let k = function() {
    function x() {}
    class y{}
    for (x of ['']);
    for (y in ['']);
}();

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
    },
    "externalHelpers": true
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": false
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.14&code=H4sIAAAAAAAAA1MqLU5VKC4pykwuUbLmykktUchWsFVIK81LLsnMz9PQVKjmUgACmIBCBUioFiyWnJNYXKxQCeWl5RcpaFQo5KcpRKurx2paIwQrFTLzYIK1GprWAH7VDnNzAAAA&config=H4sIAAAAAAAAA32VO5LbMAyG%2B5zCozpFxkWKHCCzTc7AoUlQppcPDQF6rdnx3QNR8mPXkDoLH36ABED488du153QdH92n%2FyTPwZdEMr9my04JtIXtnRgokZT%2FEDdzxs94YScDgjNdJ1JR7r0QE2F%2B1%2F7%2FaLoQs4IN8Viiz55Nz7nNDkOBRCfbGzlkDVCIvyqX1jJHxOgUp%2Fth5wD6LRBlEblE0EPRQpscgh6QFBnXYQo00l18ZilFBOsBFYNJQ8iT9aTz4lzvlIL2iqTLQjIFzDkzyDJOBfLEvL1hPs0bOFQ%2B771%2BZsazjpUTUJOuLSW8GmFqMfskZSrSSrhDFdqMMOluN%2BV3qkCVEt61Z2yTys9eQfgCgSNmHQEKW7zcDxPa2q3qfTJ8cjSKHCeb%2BmWCXouqvLeCZWdKgOFvNTNArYamCprpOMseKV86C0ocI5nRQiNH57MUUpK4wDZCYD7q500VTNQ91e4wqcHsYH%2F8i1JHrDFI2o6rlMc4yGHjQQR6JjthgO3gvI6LrwlLsM6r8kCjwZY0aViA69LgB8AZRXavnyZDX4eHFH1IR8ea2JxuN73cNSpf7z3L6sYLgSFV8wbhAHKU4Tm0MVsa1Mu%2FwBT8%2Be9%2Fbt7ON1W9P1Wncd%2FN%2BWc8%2FofsXtHPU4GAAA%3D

SWC Info output

No response

Expected behavior

"use strict";
let k = function() {
	function x() {} // These should be preserved
	class y{}      // These should be preserved 
	for (x of ['']);
	for(y in ['']);
}();

Actual behavior

"use strict";
let k = function() {
    for (x of [
        ''
    ]);
    for(y in [
        ''
    ]);
}();

Version

1.7.14

Additional context

No response

@tmdghks tmdghks added the C-bug label Aug 20, 2024
@kdy1 kdy1 added this to the Planned milestone Aug 20, 2024
@kdy1 kdy1 self-assigned this Aug 20, 2024
@kdy1 kdy1 closed this as completed in ac432c4 Aug 21, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.7.17 Aug 23, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented Sep 23, 2024

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.

@swc-project swc-project locked as resolved and limited conversation to collaborators Sep 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants