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

Conflicting variable scopes after minification #389

Open
waiwaing opened this issue Apr 15, 2024 · 1 comment
Open

Conflicting variable scopes after minification #389

waiwaing opened this issue Apr 15, 2024 · 1 comment

Comments

@waiwaing
Copy link

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
Copy link
Owner

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

Pull requests welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants