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

Uglyfying with RenamePairs does not consitently rename properties and methods of JS classes #406

Open MarcelVersteeg opened 1 month ago

MarcelVersteeg commented 1 month ago

Describe the bug Consider JavaScript like this:

class MyClass
{
    constructor(field)
    {
        this.field = field;
    }

    method()
    {
        field++;
    }

    get property()
    {
        return field;
    }
}

var myVar = new MyClass();
myVar.method();
var property = myVar.property;

Then I configure the Uglifier as follows (irrelevant options left out for brevity):

var settings = new CodeSettings
            {
                OutputMode = OutputMode.MultipleLines,

                // Your settings
                LocalRenaming = LocalRenaming.CrunchAll,
                PreserveFunctionNames = false,
                PreserveImportantComments = false,
                QuoteObjectLiteralProperties = false,
                RemoveFunctionExpressionNames = true,
                RemoveUnneededCode = true,
                RenamePairs = @"MyClass=a,field=b,method=c,property=d,myVar=e",
                ReorderScopeDeclarations = true,
                StrictMode = false,
                StripDebugStatements = true,
                TermSemicolons = false,
                WarningLevel = int.MaxValue
            };

Now, the resulting uglified JavaScript will become (indentation kept for readability):

class a
{
    constructor(b)
    {
        this.b = b;
    }

    method()
    {
        b++;
    }

    get property()
    {
        return b;
    }
}

var e = new a();
e.c();
var d = e.d

This code does not run correctly, as the actual method and property in the class are not renamed, but the locations where they are called are renamed.

To Reproduce See the code example

Minified output or stack trace See the description

Excepted output code

class a
{
    constructor(b)
    {
        this.b = b;
    }

    c()
    {
        b++;
    }

    get d()
    {
        return b;
    }
}

var e = new a();
e.c();
var d = e.d
MarcelVersteeg commented 1 month ago

As a side note: configuration properties that would rename all global variables and methods and properties of a class would also be great

trullock commented 1 month ago

Thanks for the clear bug report

PRs welcome, the issue will probably lie in one of the Analyzers