BundlerMinifier icon indicating copy to clipboard operation
BundlerMinifier copied to clipboard

Error when minifying javascript, wrong variable name was used

Open juliendogny opened this issue 4 years ago • 2 comments

Installed product versions

  • Visual Studio: 2017 Community
  • This extension: 2.8.396

Description

When minifying a JS file, a variable name was used with the same name as another one, result let n of n loop not working

Steps to recreate

  1. Script with navigator.serviceWorker.getRegistrations().then(function (registrations) { for (let registration of registrations) { if (registration.scope == 'https://mysite.com/') { registration.unregister(); } } });
  2. When minified becomes navigator.serviceWorker.getRegistrations().then(function(n){for(let n of n)n.scope=="https://mysite.com/"&&n.unregister()})

Current behavior

There is a use of the same variable name

Expected behavior

I corrected the minified version with this version and it worked navigator.serviceWorker.getRegistrations().then(function(n){for(let u of n)u.scope=="https://mysite.com/"&&u.unregister()})

juliendogny avatar Jun 19 '20 16:06 juliendogny

I have the same issue.

When I use a for of loop using let, inside a function it fails (if I substitute let with var it works). It has be in a function scope to fail:

function myFunc() {
    let items = document.getElementsByClassName("items");

    for (let item of items) {
        console.log(item);
    }
}
myFunc();

Produces:

myFunc(){let n=document.getElementsByClassName("items");for(let n of n)console.log(n)}

and produces the following error:

MyJs.min.js:1 Uncaught ReferenceError: Cannot access 'n' before initialization at myFunc (MyJs.min.js:1) at MyJs.min.js:14

chrissp26 avatar Apr 01 '21 12:04 chrissp26

I also have the same issue with the NuGet package version 3.2.449:

the following code:

static compareObjects(obj1, obj2) {
    let equal = true;
    for (const i in obj1) {
        if (!obj2.hasOwnProperty(i)) {
            equal = false;
        }
    }
    return equal;
}

when minified results in:

static compareObjects(n, t) {
    let i = !0;
    for (const n in n) t.hasOwnProperty(n) || (i = !1);
    return i
}

which when run excepts with:

cannot access 'n' before initialization

changing (const i in obj1) to (var i in obj1) get's it working, but this raises a warning on this line since i is never modified.

Xorcist77 avatar May 20 '21 19:05 Xorcist77