swc icon indicating copy to clipboard operation
swc copied to clipboard

minifier: invalid function handling in switch statement

Open hyp3rflow opened this issue 2 years ago • 2 comments

Describe the bug

Even if the function in switch statement has some expressions; like console.log("hi"), the minified result remains unchanged.

Input code

const k = (() => {
    switch (1) { 
        case 0: 
            function x() {}
        default:
            x(); 
    } 
})();

Config

No response

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.4.11&code=H4sIAAAAAAAAA1WMvQqAIBSFd5%2FijLrVati7iClJoYNKgfjuXQuKvu38mhhSxgYFzgXUjMpApMNns4KPAhW30zE6WQzyMzquBJN9DDjpoLY3W6zTZc%2FyV6bO9MwbWBOk2AWJvP1RggAAAA%3D%3D&config=H4sIAAAAAAAAA32UO3LjMAyG%2B5zCozrFjostcoDtcgYOLYIyvSShIUDHmozvvtDDj40hdRI%2B%2FAAJgPh%2B2%2B2aE7XNx%2B5bPuWnt4Wg3P%2FFQkNmexFLA22y1JbQc%2FN%2BoycakbeRYDJdZ9KwLR3wpKL9r%2F1%2BUTQRkeCmWGwp5OCH55wtpr4A0ZNNrBKyJshM%2F%2BsXVvBrBFzqs%2F2AGMHmDWIsmZAZOiha4BZjtD2BOduiRBlPaksg1FKMsDI40xfsVZ5d4IBZcr5SB9aZFh0oKBRoOZxBk0kukWWS6yn3mbCDQ%2B26qc8%2F1HC2sVpWcsJlaomcVol6xEBsfM1aCWe4UoMZLsX9qQzeFOBa8qvuhCGv9OQvgFQgWqJsE2hxJw8v87Sm9pvKkL2MLA8Kl%2FnWbpmhk6KaELxS2bEyUDho3SzgagtjZVvtOAteKR8FBwa8l1lRQtNX4PaoJeWhB%2FQKkP5ar03VDMz9Fa7w8UFs4D9yS9YHbPFIlo%2FrlIZ0wLiRIAEf0W04SCsY13GRLXHp13nNDmQ0wKkulSbwugTkATCaOO3Ll9mQ5yERTRfx8FgTi8P1voeTzd3jvc%2Br%2BG1xaBK6OsFlyY%2F9nVfz7%2BbhdNvC94M3gT5vyjns9R%2F7ntJqMQYAAA%3D%3D

SWC Info output

No response

Expected behavior

maybe

let k = void 0;

Actual behavior

let k = void (void 0)();

which results in calling undefined ((void 0) is not a function)

Version

1.4.11

Additional context

No response

hyp3rflow avatar Apr 04 '24 06:04 hyp3rflow

Hello @kdy1, @magic-akari

could you help me send my first pr?

yazaldefilimone avatar Jun 18 '24 10:06 yazaldefilimone

@yazaldefilimone The code for the optimizer that uses variable data lives at https://github.com/swc-project/swc/tree/1839980185a5771f8e51fe6912438a08f2ed2fc2/crates/swc_ecma_minifier/src/compress/optimize and the code for variable analyzer lives at https://github.com/swc-project/swc/tree/1839980185a5771f8e51fe6912438a08f2ed2fc2/crates/swc_ecma_usage_analyzer/src/analyzer

Both the optimizer and analyzer use the visitor pattern, and you can follow visitor methods to understand the flow.

The visitor visits all fields of each types, and you can see the types at https://rustdoc.swc.rs/swc_ecma_ast/

kdy1 avatar Jun 19 '24 00:06 kdy1