swc-project / swc

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

SWC produces incorrect code #7004

Closed just-boris closed 1 year ago

just-boris commented 1 year ago

Describe the bug

With the provided input code, option variable is being accessed before it is defined, causing TypeError: Cannot read properties of undefined (reading '__labelPrefix')

Input code

function getDescription(option, parentGroup) {
    return [
        parentGroup && parentGroup.label,
        option.__labelPrefix,
    ]
        .concat(option.tags)
}

function printDescription() {
  const option = {__labelPrefix: 'test', tags: []};
  const parent = null
  const desc = getDescription(option, parent);
  console.log(desc);
}

printDescription();

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": 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"
  },
  "minify": false,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.37&code=H4sIAAAAAAAAA32QTQqDMBCF9znFrPwByQGU7grddi8iNh0lEBKJEyiId280GpRCZxPmZd6bj%2BmdFiSNhgHpjpOwclzbzGxPAWNnUdPDGjfmMDPwZZGc1VBvzVqnGUiSc8tV90JVxMmQytt2058We%2FkJv02c4cJo0dFOwKkbppwtjPUH6WilvrAGMG%2BbaN8AN5gvS0pICSdKC1jzSqibpYqeAOw92ikV1bff4LW%2Fd8mPEKOQKzNkq8mLHveXsmLsCxkjU49uAQAA&config=H4sIAAAAAAAAA22TQXLDIAxF7%2BJ1tu2iB%2BiuZ2AUEA4tRh4k0ngyuXuFY%2BM46c56fMniS1y7b7bdx7UbITPm%2BsVTErh0Hx3aAdjmMEp3UJkiD5HxdugEco9SJfymZ5GIcTk9dENIwU%2B1kqVhzMhcvzWjDJiEmw5ypl8NJReNjkQRIb3EBtiEJNhj3lItxQgjozlDbhn1b5ADU9qhIujMmGl8oMkFCZS02MocgjOWHDYQMloJZ9wkWkMlibWx1skMHR5L31f37ko8QywgrRZeZhv0jy3vRIHF%2BJL4Ce06vaP7JRdV8CajlJxWzTeFtPPhB1H7jMCcYMAtc%2BZe57BX%2Bn9UIXkdokyN6oS3rhL2ejkTgm83rF1jlrC5ldEVi%2FWGdiu%2BwN2FODg06L263ZL5N4g9bcVkGpF8C9U18NsM7qFZ92lP6wq8wE%2FtSh7HsfAB5PTMeBqOFF9KDCgnci9YjRB6hlm39zI%2B05Icqs3oHg4K17CtqQ5fyMT61FafdSE0x%2FSRjm199UUOkPq2lrcKyJUKrrN581N9727b61zXib8W4VzoD8zDcjEPBAAA

Expected behavior

Expected to be minified code something like this (or anything else matching the original structure)

!function() {
    var option = {
        __labelPrefix: "test",
        tags: []
    };
    console.log([
        null,
        option.__labelPrefix
    ].concat(option.tags));
}();

Actual behavior

I am getting this

!function() {
    var option;
    console.log([
        null,
        option.__labelPrefix
    ].concat((option = {
        __labelPrefix: "test",
        tags: []
    }).tags));
}();

Version

1.3.37

Additional context

Originally reported as an issue to our lib: https://github.com/cloudscape-design/components/issues/785

swc-bot commented 1 year ago

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.