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 produces incorrect code when template literals are used #7969

Closed
abdulsattar opened this issue Sep 18, 2023 · 2 comments · Fixed by #7971
Closed

Minifier produces incorrect code when template literals are used #7969

abdulsattar opened this issue Sep 18, 2023 · 2 comments · Fixed by #7971
Assignees
Labels
Milestone

Comments

@abdulsattar
Copy link

Describe the bug

The minifier is reusing a variable defined inside of a function when it's not safe to do so. This changes the runtime behavior as demonstrated in this repro.

Input code

let a = 0;
function f(arr) {
  let b = a;
  return `${arr.map((child) => {
  a = b + "str";
})}`;
}

function g(arr) {
    return f(arr);
}
globalThis.g = g;
g([1,2,3])
console.log(a);

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2015",
    "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": {
        "toplevel": false,
        "keep_classnames": false,
        "keep_fnames": false,
        "keep_private_props": false,
        "ie8": false,
        "safari10": false
      }
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": true,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.84&code=H4sIAAAAAAAAA0XNTQrDIBAF4L2nGEIXSoP0ZynJKborhag1RphqUbMKuXsnLTSbgcd7fIOugoYOToqNc7Q1pAgj1zkLWBgAUm2o1opCdnXOEYbDQr186Tfndgr4FND13%2FUGGThCU2puFFvFOtBlu%2Bx3%2Bc%2F9vm07j8lovE2hSE%2BSV8zz%2B7m9tNeHYDbFktBJTIQI9QGn86FuuAAAAA%3D%3D&config=H4sIAAAAAAAAA41VwW7bMAy99ysCn3doB3QY9gG77RsExaIcZbJoiJSboMi%2Fj1bsNG1oY5cg5tMjRfKRen%2Fa7Zojtc2v3bv8lY%2FBZoJ8%2BxYLnRPbk1gaaHtLbQ4DN98W9EgT5G0kqKbLFWnY5g64suj788vrzGgiIsHCmG19SMGf72O22A8ZiO5sYhWXpYfE9Jk%2FYxnfJoBzubfvESPYtIEYSyYkhg6y5rjFGO1AYEabFS%2FTTW0OhFqICSwMzgwZBxVPLnDAJDEfUQfWmRYdKFDI0HIYQaNJLKElkvSUfCrsYF%2B6rvb5CxtGG4tlJSacakvktorXAwZi40vSSngFV2pwBefifmUGbzJwyemRd8SQVnryF0AqEC1Rsj1ofusJL3paY%2FtNZkheJMtnBRd9a1km6KSoJgSvVHaqDGQOWjczuNLCVNlWu84Mr5SPggMD3otWFNf0Frg9aEH5PAB6BZD%2BWq%2Bp6gqY2xSu4NNAbMC%2FJUvWBTaf6C0f1lE693uMGwF64AO6jQPSCsZ1OMuWOA3reEkORBrg1COFKvC4BGQAGE2s%2B%2FJBGzIe4tF0Efcfa2I%2BcLnt4d6mrs773b5kHCKMENdk%2FB8jsonK3cZJ1ctkP0wv%2FNREaWXwwsvzp2dDUnlafmtSTY%2Bu3CVUNXl9Tn40H4eWl2OpWhPoz0Ksdbr8A8XlsejjBgAA

SWC Info output

No response

Expected behavior

Unminified code prints 0str while minified code prints 0strstrstr.

Actual behavior

No response

Version

1.3.85

Additional context

No response

@kdy1
Copy link
Member

kdy1 commented Sep 19, 2023

Investigation

This only occurs if minifier is invoked using transform().

kdy1 added a commit that referenced this issue Sep 20, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.87 Sep 22, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 22, 2023

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.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants