swc-project / swc

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

Hoisted var declaration will cause unexpected simplification with different target #6419

Open hyf0 opened 1 year ago

hyf0 commented 1 year ago

Describe the bug

Hoisted var declaration will cause unexpected simplification with different target

Input code

(function () {
    var first = state();
    var on = true;
    var obj = {
        state: state
    };
    log(first)
    log(obj.state());
    function state() {
        return on ? "ON" : "OFF";
    }
})();

Config

{
  "jsc": {
    "transform": {
      "react": {
        "refresh": {}
      }
    },
    "parser": {
      "syntax": "ecmascript",
      "jsx": true
    },
    "target": "es5",
    "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": "es6"
  },
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.15&code=H4sIAAAAAAAAA9NIK81LLsnMz1PQ0FSo5uIsSyxSSMssKi5RsFUoLkksSdXQtIaIApXYKpQUlabC%2BElZQAGgFk6wOiuIci7OWqB8Tn66BtgUTQgbqFYPahrIOLidUDGwKUWpJaVFeSBr7BWU%2FP2UFKyAlJubElB9LVetJsgdXACvpGevrgAAAA%3D%3D&config=H4sIAAAAAAAAA22TS5LjIAyG78I625lFDjC7OQNFQDj0GOSSRDquVO7ewk5wnMwOffwSenEzX%2BzN8WaEXOGIlJtB4Lysh0jAZz3e7%2FeDmRwxULvguYi7mqMBnx17SpOYg8ZSJFRBteJoAGkK%2FqVXIyKDOUY3MhxMTiXFuQXymCd9gttZPWqGItx1jgi%2FeY15MCfEETTNd9s6tqkIDECbq8dxdBODvTjqHu01R4mx7FAVCHYinF5oCUkSFg32ZAFcsB4DdJAIvKQLbBKNoZLCmljPZIEBTnUYWvNWJVzcWJ30WHBd2qAvdr8zJhYba%2BE3tMt0RWuRD1WKlkAqlafmC1PZ9eEfgOY5OubiMmyeC486h70y%2FkeVStQhytypTnjLqsCgxdmUYq%2BwZQ0kaesWQageWoV%2BC%2F6Au4I4BbAQo3a7O%2FN3En%2Fegsk8AcZuatdc3Gawmva5T3vaVuAD%2FtGs5HUcD56dnN8Zz%2FmE40eIDHLG8IG1EYLvkHR7r9M7rSWAthnCy0XlZvY11eEL2rF9tWefdSHUxw4jnvr66o%2FMrgx9Ldt3zhhqA7electX%2FW2UJ%2F77uFk8fwARW3AUJAQAAA%3D%3D

Expected behavior

__With the ES5 target you will get image

Actual behavior

If you set the target to ES2022, you will get image

Version

1.3.15

Additional context

Not the same problem, but could be related https://github.com/terser/terser/issues/1306

kdy1 commented 1 year ago

Oh... This was about implicit undefined.

larsgw commented 1 year ago

(reply moved to https://github.com/swc-project/swc/issues/7331)

kdy1 commented 1 year ago

@larsgw It's not related. Please file another issue

larsgw commented 1 year ago

Okay, thank you.