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

Conflicting variable scopes after minification #389

Open waiwaing opened 7 months ago

waiwaing commented 7 months ago

Describe the bug In the below code, both selected and getArray are renamed to i. However, the variable scoping is incorrect - the selected in deselect conflicts with the declaration of getArray.

To Reproduce

(function (Bar) {
    function getArray() {
        return [];
    }
    let domElement = null;
    function initialise(input) {
        if (domElement === null || input.length === 0) {
            return;
        }
        let selected = -1;
        function deselect() {
            selected = -1;
        }
        domElement.on("input focus", (() => {
            domElement.val() === "" ? (selected = -1) : deselect();
        }));
    }
    Bar.initialise = () => initialise(getArray());
})(Foo.Bar);

This file is run as:

var input = File.ReadAllText("input.js");
var output = Uglify.Js(input).Code;

Minified output (after formatting)

(function (n) {
    function i() {
        return []
    }
    function r(n) {
        function r() {
            i = -1
        }
        if (t !== null && n.length !== 0) {
            let i = -1;
            t.on("input focus", () => {
                t.val() === "" ? i = -1 : r()
            })
        }
    }
    let t = null;
    n.initialise = () => r(i())
})(Foo.Bar)
trullock commented 7 months ago

Its the use of let/const thats causing the bug, workaround is to use var.

Pull requests welcome