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

variable is declared multiple times when variable is reserved and there are 2 passes and target is >=es2015 #8622

Open
bradzacher opened this issue Feb 8, 2024 · 0 comments · May be fixed by #8883
Assignees
Labels
Milestone

Comments

@bradzacher
Copy link

bradzacher commented Feb 8, 2024

Describe the bug

When jsc.minify.compress.passes > 1 and jsx.minify.mangle.reserved includes a reserved name - SWC can produce code that semantically invalid with the same variable being definied multiple times.

Input code

export function foo(cond) {
  let reserved = 1;
  if (cond) {
    reserved = 2;
  }
  return [reserved, bar(cond)];
}

function bar(cond) {
  let reserved = 1;
  if (cond) {
    reserved = 2;
  }
  return reserved;
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2015",
    "loose": false,
    "minify": {
      "compress": {
        "passes": 2
      },
      "mangle": {
        "reserved": ["reserved"]
      }
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.4.0&code=H4sIAAAAAAAAA0utKMgvKlFIK81LLsnMz1NIy8%2FXSM7PS9FUqOZSUMhJLVEoSi1OLSpLTVGwVTC0BoplpikgqVBAljcCyddygcRKSovyFKJhcjoKSYlFEF2x1ly1XFxw%2B%2BDi2OwzJmCfCZp9MCmQFQByNT4O2QAAAA%3D%3D&config=H4sIAAAAAAAAA1WOQQ6DIBBF956CzLqL1qRd9A49QdMFwdFgQMgMNhrj3YsoajeEeX%2F%2Bnz8VQkDLCp5iit84eEmMtM%2BR8NgFOUQCqKxkRdoHuGS15UWqpWFMaF4VCJIaDMnF5fV23xxgnGPMjo1Z3el6PN9UznpC5hNL3ZhxYeXG5r2GlV1j8H89BiB9sYr0fQyf7C3ym1LAuqo%2FJUAYPa71H3As5aZ7fdD8ys5APRbzD1wt6BJUAQAA

SWC Info output

No response

Expected behavior

SWC produces valid code

Actual behavior

SWC produces broken code:

export function foo(e) {
    let reserved, reserved = 1;
    return e && (reserved = 2), [
        reserved,
        (reserved = 1, e && (reserved = 2), reserved)
    ];
}

Version

1.4.0

Additional context

Note: When targetting es5 the output uses var so it does not crash in a browser.

For comparison the 1 pass code:

export function foo(e) {
    let reserved = 1;
    return e && (reserved = 2), [
        reserved,
        function(e) {
            let reserved = 1;
            return e && (reserved = 2), reserved;
        }(e)
    ];
}

1 pass code with no reserved names:

export function foo(t) {
    let n = 1;
    return t && (n = 2), [
        n,
        function(t) {
            let n = 1;
            return t && (n = 2), n;
        }(t)
    ];
}

2 pass code with no reserved names:

export function foo(o) {
    let t, e = 1;
    return o && (e = 2), [
        e,
        (t = 1, o && (t = 2), t)
    ];
}
@kdy1 kdy1 self-assigned this Feb 8, 2024
@kdy1 kdy1 added this to the Planned milestone Feb 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