trullock / NUglify

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

Function name collides with variable name #405

Open seth-drf opened 2 months ago

seth-drf commented 2 months ago

I'm using version 1.21.9.

Describe the bug NUglify is giving a function inside a block the same name as a variable in a closure.

To Reproduce How I'm running it:

var result = Uglify.Js(js);
var output = result.Code;
Console.WriteLine(output);

JavaScript inside js variable:

(function(){
    var v1 = true;
    function someFunction() {
        if (true) {
            myFunction();
            function myFunction() {
            }
        }
        function otherFunction() {
            console.log({v1});
        }
        otherFunction();
    }
    someFunction();
})();

Minified output Before formatting:

(function(){function t(){function t(){console.log({v1:n})}if(1){n();function n(){}}t()}var n=!0;t()})()

After formatting:

(function() {
    function t() {
        function t() {
            console.log({
                v1: n
            })
        }
        if (1) {
            n();
            function n() {}
        }
        t()
    }
    var n = !0;
    t()
}
)()

Excepted output code The v1 variable and the myFunction function should have different names.

Running the original JavaScript puts an object with v1 set to true into the console. Running the minified JavaScript puts an object with v1 set to the function.

aravindvemulaa commented 2 months ago

(function(){ var v1 = true; function someFunction() { if (true) { useMyFunction(); // Renamed to avoid collision function useMyFunction() { // Renamed to avoid collision } } function displayFunction() { // Renamed to avoid collision console.log({v1}); } displayFunction(); } someFunction(); })();

Changes ensure that function names are unique and do not conflict with other variable or function names, which could lead to issues in minification or during execution.

trullock commented 2 months ago

(function(){ var v1 = true; function someFunction() { if (true) { useMyFunction(); // Renamed to avoid collision function useMyFunction() { // Renamed to avoid collision } } function displayFunction() { // Renamed to avoid collision console.log({v1}); } displayFunction(); } someFunction(); })(); Changes ensure that function names are unique and do not conflict with other variable or function names, which could lead to issues in minification or during execution.

Pretty sure this wont help at all