NUglify icon indicating copy to clipboard operation
NUglify copied to clipboard

Invalid renaming and reordering of Javascript functions.

Open MaxwellDAssistek opened this issue 2 years ago • 6 comments

Describe the bug It seems that when code has variables, declared at the top, a function declared inside of an if statement and then more functions declared lower, the lower functions get reordered to the top and renamed to the same name as the function inside of the if.

To Reproduce

var result = Uglify.Js(@"$(document).ready(function () {
    let test = 123;
    if (!window.a) {
        function idle() {
            console.log(""test"")
        }
    }
    function cancelTest() {
        console.log(""test2"")
    }
    function why() {
        console.log(""test3"")
    }
    window.test(why, cancelTest)
});");
Console.WriteLine(result.Code);

Returns: (beautified)

$(document).ready(function() {
    function n() {
        console.log("test2")
    }

    function t() {
        console.log("test3")
    }
    if (!window.a) {
        function n() {
            console.log("test")
        }
    }
    window.test(t, n)
})

The issue being that the n() function inside of the if {} block will override the n() function above.

Expected output code

$(document).ready(function() {
    function n() {
        console.log("test2")
    }

    function t() {
        console.log("test3")
    }
    if (!window.a) {
        function a() {
            console.log("test")
        }
    }
    window.test(t, n)
})

MaxwellDAssistek avatar May 09 '22 19:05 MaxwellDAssistek

Curious

Ok good find, I'll take a look

trullock avatar May 09 '22 19:05 trullock

Need to check if the opposite of this also behaves, i.e. a deliberate overriding - does this correctly remain overridden when crunched?

trullock avatar May 16 '22 09:05 trullock

In response to the comment that I got notified of, but can't see here:

In my actual implementation idle() is being used in that same block. I just didn't include it as it did not affect the issue. What I submitted was the minimal reproducer.

The following exhibits the same issue.

var result = Uglify.Js(@"$(document).ready(function () {
    let test = 123;
    if (!window.a) {
        function idle() {
            console.log(""test"")
        }
        if (window.b) {
            b(idle)
        } else {
            setTimeout(idle, 500)
        }
    }
    function cancelTest() {
        console.log(""test2"")
    }
    function why() {
        console.log(""test3"")
    }
    window.test(why, cancelTest)
});");
Console.WriteLine(result.Code);

Results in: (beautified)

$(document).ready(function() {
    function n() {
        console.log("test2")
    }

    function t() {
        console.log("test3")
    }
    if (!window.a) {
        function n() {
            console.log("test")
        }
        window.b ? b(n) : setTimeout(n, 500)
    }
    window.test(t, n)
})

MaxwellDAssistek avatar May 16 '22 13:05 MaxwellDAssistek

Yeah sorry I wrote some BS and deleted it 🤣

trullock avatar May 16 '22 13:05 trullock

Yeah sorry I wrote some BS and deleted it 🤣

No worries, thanks for your work on this project!

MaxwellDAssistek avatar May 16 '22 13:05 MaxwellDAssistek

@MaxwellDAssistek have you tried adding "use strict";? This solved my similar issue, see ; #299 / #334

mattscotty avatar Aug 09 '22 15:08 mattscotty