swc/crates
CPunisher 7344a638b5
feat(es/minifier): Optimize switch with side effect and termination tests (#9677)
**Description:**

This is learnt from https://github.com/terser/terser/pull/1044
**Key point:** all the cases (statements) after the last side-effect and
non-terminated one are useless.

There are also some points on whether a statement is side-effect free:
- **Var declaration:** I think var declaration always contains side
effect.
- **Function declaration:** In non-strict mode, function declaration is
same as var declaration. In strict mode, function is declared in
enclosing block scope, so it's side-effect free. But there is a bad case
when we call the function before it is declared in the switch case:
```js
"use strict";
const k = (() => {
    let x = 1;
    switch (x) {
        case y():
        case x: {
            async function y() {
                console.log(123);
            }
        }
    }
    return x;
})();
```

**This is an existing bug.** I'm not sure whether we should fix it since
it is a very bad code and I also find some similar bad cases with
terser.

~I'm also not sure it's appropriate to introduce context variable
`in_strict` for `StmtExt:: may_have_side_effects`, because `in_strict`
could change from `false` to `true`. But this is a **conservative**
mistake and does not break the program. Maybe we should use visitor for
context-aware side effect checker in the future.~

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/8953
2024-10-29 10:39:05 +09:00
..
ast_node chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
better_scoped_tls chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
binding_macros chore: Publish crates with swc_core v1.0.1 2024-10-15 23:13:49 +09:00
dbg-swc chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
from_variant chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
jsdoc chore: Publish crates with swc_core v1.0.2 2024-10-17 10:54:06 +09:00
preset_env_base chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
string_enum chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc feat(es/minifier): Optimize switch with side effect and termination tests (#9677) 2024-10-29 10:39:05 +09:00
swc_allocator chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_atoms chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_bundler chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_cached chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_cli_impl chore: Publish crates with swc_core v1.0.6 2024-10-26 17:33:51 +09:00
swc_common chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_compiler_base chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_config chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_config_macro chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_core chore: Publish crates with swc_core v1.0.6 2024-10-26 17:33:51 +09:00
swc_css chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_css_ast chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_css_codegen chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_css_codegen_macros chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_css_compat chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_css_lints chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_css_minifier chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_css_modules chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_css_parser chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_css_prefixer chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_css_utils chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_css_visit chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_ecma_ast chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_ecma_codegen fix(es/codegen): Improve EndsWithAlphaNum (#9675) 2024-10-24 16:34:18 +09:00
swc_ecma_codegen_macros chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_ecma_compat_bugfixes chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_compat_common chore: Publish crates with swc_core v1.0.1 2024-10-15 23:13:49 +09:00
swc_ecma_compat_es3 chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_compat_es2015 chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_compat_es2016 chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_compat_es2017 chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_compat_es2018 chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_compat_es2019 chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_compat_es2020 chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_compat_es2021 chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_compat_es2022 chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_ext_transforms chore: Publish crates with swc_core v1.0.1 2024-10-15 23:13:49 +09:00
swc_ecma_lints chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_loader chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_ecma_minifier feat(es/minifier): Optimize switch with side effect and termination tests (#9677) 2024-10-29 10:39:05 +09:00
swc_ecma_parser feat(es/parser): Ability to get script's potential module errors (#9682) 2024-10-29 09:50:14 +09:00
swc_ecma_preset_env chore: Publish crates with swc_core v1.0.2 2024-10-17 10:54:06 +09:00
swc_ecma_quote chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_ecma_quote_macros chore: Publish crates with swc_core v1.0.2 2024-10-17 10:54:06 +09:00
swc_ecma_testing chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_ecma_transforms chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_transforms_base chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_transforms_classes chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_transforms_compat chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_transforms_macros chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_ecma_transforms_module chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_transforms_optimization feat(es/minifier): Optimize switch with side effect and termination tests (#9677) 2024-10-29 10:39:05 +09:00
swc_ecma_transforms_proposal feat(es/minifier): Optimize switch with side effect and termination tests (#9677) 2024-10-29 10:39:05 +09:00
swc_ecma_transforms_react chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_transforms_testing chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_transforms_typescript chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_ecma_usage_analyzer feat(es/minifier): Optimize switch with side effect and termination tests (#9677) 2024-10-29 10:39:05 +09:00
swc_ecma_utils feat(es/minifier): Optimize switch with side effect and termination tests (#9677) 2024-10-29 10:39:05 +09:00
swc_ecma_visit chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_ecmascript chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_eq_ignore_macros chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_error_reporters chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_estree_ast chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_estree_compat chore: Publish crates with swc_core v1.0.2 2024-10-17 10:54:06 +09:00
swc_fast_graph chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_fast_ts_strip chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_graph_analyzer chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_html chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_html_ast chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_html_codegen chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_html_codegen_macros chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_html_minifier chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
swc_html_parser chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_html_utils chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_html_visit chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_macros_common chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_malloc chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_node_bundler chore: Publish crates with swc_core v1.0.2 2024-10-17 10:54:06 +09:00
swc_node_comments chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_nodejs_common chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_plugin chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_plugin_macro chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_plugin_proxy chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_plugin_runner chore: Publish crates with swc_core v1.0.2 2024-10-17 10:54:06 +09:00
swc_plugin_testing refactor(visit): Pre-generate visitor code (#9262) 2024-07-17 23:00:45 +00:00
swc_timer chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_trace_macro chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_transform_common chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_typescript chore: Publish crates with swc_core v1.0.6 2024-10-26 17:33:51 +09:00
swc_visit chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_x_optimizer feat(x): Initialize project (#8999) 2024-05-31 00:56:21 +00:00
swc_xml chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_xml_ast chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_xml_codegen chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_xml_codegen_macros chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_xml_parser chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
swc_xml_visit chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
testing chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00
testing_macros chore: Publish crates with swc_core v1.0.0 2024-10-12 16:20:07 +09:00