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 inlines hoisted var variable usage incorrectly #8707

Open
hyp3rflow opened this issue Mar 7, 2024 · 0 comments
Open

Minifier inlines hoisted var variable usage incorrectly #8707

hyp3rflow opened this issue Mar 7, 2024 · 0 comments
Labels
Milestone

Comments

@hyp3rflow
Copy link
Sponsor Contributor

Describe the bug

The hoisted var variable can be accessed without ReferenceError, but its value is undefined before initialization.
Currently minifier doesn't care about this and inlines it using the invalid value in declaration statement.

Input code

function foo() {
  const a = x;
  var x = 42;
  return a;
}

console.log(foo())

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es5",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": false,
        "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": true,
        "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"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.4.6-nightly-20240307.1&code=H4sIAAAAAAAAA0srzUsuyczPU0jLz9fQVKjmUlBIzs8rLlFIVLBVqLAGcssSixQqgBwTIxCvKLWktChPIdGaq5aLC6QyPydVLyc%2FXQOsXxMANYklvk8AAAA%3D&config=H4sIAAAAAAAAA32US5LjIAyG932KlNe9nVnMAXo3Z6AICIc0IBcS6bi6cveRH3lMR87O1qcHSD%2F6ftvtuiO57s%2FuWz7lZ7CVoN7%2BxUJjYXsWSwcuW3I1Dty9X%2BmRJhRsIphNl4V0bGsPPEfRr9W9S4gEV%2FfVlmOJYXws6DAPFYgebGKVfC1DYfo%2FfmUVvybAtT3a94gJbHlBjCUTC0MPVUvsMCU7EJiT3eBZGhYJtRoTbAzeDBUHlRcfOWKRpM%2FUg%2FXGoQcFxQqO4wm0MKklYYXkfsqBZ%2Bxh3%2Fp%2BnvKPaDjZ1CwrNeE8z0ROq2Q9YCQ2oRWtRwvc6MEC1%2B7%2BYDGYCtxqeUZHjGUj6hNAGpAsUbEZtAPNHkH0tBUdXkbGEkSyPCpc9K1dskAvPTUxBqWxU2OgctSGWcE3B1NjnXacFW9ok6IHAyGIVJTU9BXZHbSiPA6AQQEyXhs0US3A3F7hBp%2Fewwv8IbdkXV%2BrR7Z82KY05j2mFwUy8AH9CwcZBeM2rrIlzsM2b8WDSAO86tJoBs87QPTPaNK8LJ%2B0Ia9DMpo%2B4f6%2BJVaHy20JZ1v6%2B3Nf9vDb6tBl9G2G64af5rvs5d%2Fd3em6hW8H7yL9vUbORS%2F%2FAI2shQwtBgAA

SWC Info output

No response

Expected behavior

console.log(void 0);

or don't inline function

Actual behavior

console.log(42);

Version

1.4.6-nightly-20240307.1

Additional context

No response

@hyp3rflow hyp3rflow added the C-bug label Mar 7, 2024
@kdy1 kdy1 self-assigned this Mar 8, 2024
@kdy1 kdy1 added this to the Planned milestone Mar 8, 2024
@kdy1 kdy1 removed their assignment Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants