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 should preserve classes with a side effect in a static block #5588

Closed
kdy1 opened this issue Aug 22, 2022 · 3 comments · Fixed by #6480
Closed

Minifier should preserve classes with a side effect in a static block #5588

kdy1 opened this issue Aug 22, 2022 · 3 comments · Fixed by #6480

Comments

@kdy1
Copy link
Member

kdy1 commented Aug 22, 2022

Describe the bug

Minifier currently removes static block if a class is not used

Found by #5578

Input code

"use strict";
let getFoo;
class Foo {
    static #foo = 42;
    static{
        getFoo = ()=>this.#foo;
    }
}
expect(getFoo()).toBe(42);

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript"
    },
    "target": "es2022",
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": false,
        "conditionals": true,
        "dead_code": true,
        "directives": false,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": false,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": false,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": false,
        "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
    },
    "loose": false
  },
  "minify": false
}

Playground link

https://play.swc.rs/?version=1.2.241&code=H4sIAAAAAAAAA02LMQqAMBAE%2B3vFoU3SWAS7oIWF%2F5BwakCIeCsI4t%2BNpHGqZZitThVWHDGg8rQJeBGMKXkK26TKefJNnFFMiIHrOZuOW%2Bd%2FthQf5Z0DY7sea9TmO5T2oYfk2iXAlMxY2yANYlpn%2FQvXyVYQigAAAA%3D%3D&config=H4sIAAAAAAAAA22TTZLjIAyF7%2BJ1FlNZ9gHmGhQB4dADkguJdFxdffcWcYxjZ3Z%2Bn%2FVAf3wPn%2ByGj%2B9hsoWhtC%2BeUex9%2BBjAZcuuxEmGn9MgtowgDfP5z%2Fk8nIYcMYa5WRzlqQBz%2B9awmgFFRbCJ4aSk0JdKKVXVhSiBxTdtLJuIAiOUzeooJTsxmJst3dFusyUy4Q5VAW%2BmQtOrHX2USKhyDfVgvXHkoYNYwEm8webzeorGIGtqe%2BjhUsexNWoxw82mamWLgvujE3pnR1eKLCZU5APaJ7uwpdAnisEUkFpwve6TIu5D%2FgFoqskyo81w4EGH0QtfyH%2BiIgadpMydJnrktdgQRq3PxBh6x1reUCRCDyrgq4NWo9sOf8Jduhw9GAhBO97N%2FBXFXbfDZJ6AQpfaNxu2Bi%2FSrEu1p20P3uBfzUpeB%2FLk2cr1yHjOF0pvR2SQK%2Fk3rI0QOsKiK3yfjrSiB20z%2BJcflZtcNxh1%2BkImtUe29lk3Qj1mTHTpK6xvMVsc%2B2b%2BPAbGL3J9mIv%2BBWHfBxnjAwAA

Expected behavior

It should not be removed

Actual behavior

No response

Version

1.2.241

Additional context

No response

@kdy1 kdy1 added the C-bug label Aug 22, 2022
@kdy1 kdy1 added this to the Planned milestone Aug 22, 2022
kdy1 added a commit to kdy1/swc that referenced this issue Aug 22, 2022
kdy1 added a commit to kdy1/swc that referenced this issue Aug 22, 2022
kdy1 added a commit to kdy1/swc that referenced this issue Aug 23, 2022
kdy1 added a commit to kdy1/swc that referenced this issue Aug 25, 2022
kdy1 added a commit to kdy1/swc that referenced this issue Aug 25, 2022
kdy1 added a commit to kdy1/swc that referenced this issue Aug 25, 2022
kdy1 added a commit to kdy1/swc that referenced this issue Aug 25, 2022
kdy1 added a commit to kdy1/swc that referenced this issue Aug 25, 2022
@hyf0
Copy link
Contributor

hyf0 commented Nov 19, 2022

I wonder what fix would you accept?

  1. If a class has any static field, we just leave it as it is.
  2. Do more delegate analyzing for the static fields, only compress it if there are no side effects.

I found the solution 2 is kind of hard for your mentioned case. It could not simply call extract_class_side_effect to it and extract the statement getFoo = ()=>this.#foo;, because the block reference this.#foo.

@kdy1
Copy link
Member Author

kdy1 commented Nov 19, 2022

I think 1 is fine

@swc-bot
Copy link
Collaborator

swc-bot commented Dec 27, 2022

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 Dec 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging a pull request may close this issue.

3 participants