madskristensen / BundlerMinifier

Visual Studio extension
Other
611 stars 171 forks source link

BuildBundlerMinifier: Variables of for..of loop are minified incorrectly, resulting in erroneous syntax #591

Open msvprogs opened 1 year ago

msvprogs commented 1 year ago

Installed product versions

Description

Minification of for..of loop replaces the loop variable and iterable object variable with the same names, which is syntactically incorrect and throws Uncaught ReferenceError: can't access lexical declaration 'n' before initialization.

Steps to recreate

Consider the following code block:

function forEachTest() {
    const arr = [1, 2, 3];
    arr.push(4);

    for (const num of arr) {
        console.log(num);
    }
}

With following bundleconfig.json settings:

  {
    "outputFileName": "wwwroot/js/site.min.js",
    "inputFiles": [
      "wwwroot/js/site.bundle.js"
    ],
    "minify": {
      "enabled": true,
      "renameLocals": true
    },
    "sourceMap": false
  }

Current behavior

The code block is minified incorrectly - the names of loop variable and iterable variable are the same:

function forEachTest(){const n=[1,2,3];n.push(4);for(const n of n)console.log(n)}

Expected behavior

For example, this way:

function forEachTest(){const n=[1,2,3];n.push(4);for(const a of n)console.log(a)}