NUglify
NUglify copied to clipboard
Invalid renaming and reordering of Javascript functions.
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)
})
Curious
Ok good find, I'll take a look
Need to check if the opposite of this also behaves, i.e. a deliberate overriding - does this correctly remain overridden when crunched?
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)
})
Yeah sorry I wrote some BS and deleted it 🤣
Yeah sorry I wrote some BS and deleted it 🤣
No worries, thanks for your work on this project!
@MaxwellDAssistek have you tried adding "use strict";? This solved my similar issue, see ; #299 / #334