trullock / NUglify

NUglify is a HTML, JavaScript and CSS minification Library for .NET (fork of AjaxMin + new features)
Other
396 stars 79 forks source link

Minified Javascript repeats variable names in nested functions #334

Closed mattscotty closed 2 years ago

mattscotty commented 2 years ago

Using latest (v1.20.2) and "Uglify.Js(text).Code" it appears that nested functions confuse the minifiers naming algorithm. Possibly linked to #324.

Take the following example (code reduced for brevity, the methods and variable values are not important for this example, just the original name vs the minified one);

    {
        const variable1 = document.getElementById('divMainPage');

        function doSomething(db) {
            const subVariable1 = db.transaction("txName", "readwrite");
            const subVariable2 = subVariable1.getDataObject();

            const subVariable3 = subVariable2.doSomething();

            subVariable3.onsuccess = function (event) {
                const subSubVariable1 = event.target.result;
                const subSubVariable2 = event.target.result;
                const subSubVariable3 = event.target.result;

                //DO SOMETHING WITH subSubVariable1
            };
        }
    }

Minified this becomes;

{
    const n = document.getElementById("divMainPage");

    function t(n) {
        const t = n.transaction("txName", "readwrite"),
            i = t.getDataObject(),
            r = i.doSomething();
        r.onsuccess = function(n) {
            const t = n.target.result,
                i = n.target.result,
                r = n.target.result
        }
    }
}

We can see from the above result that n (variable1 un-minified) is declared first, then the first function variable is also declared n (db un-minified), hiding the outer n (variable1 un-minified) from the functions scope.

Minified r also has a similar issue, where it is declared, then a nested function follows and r is declared again.

For context, the actual code is much more complicated and chrome has no issues, but safari is not happy; "ReferenceError: Can't find variable: r"

Thanks

mattscotty commented 2 years ago

Forgive me, I've been a bit thick here. I answered my own question 6 months ago... #299. "use strict" fixes the issue.