madskristensen / BundlerMinifier

Visual Studio extension
Other
611 stars 171 forks source link

Minified variables are not being used in JavaScript `[varName]: value` object syntax #593

Open Mike-Logit opened 1 year ago

Mike-Logit commented 1 year ago

Describe the bug The un-minified code looks like the following:

function initializePage(getTotalCostUrl, tokenHeaderName, tokenValue) { 
    function sendTotalCostRequest() {
        var payload = createTotalCostRequestPayload();

        $.ajax({
            url: getTotalCostUrl,
            headers: { [tokenHeaderName]: tokenValue },
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: onTotalCostRetrieved,
            data: JSON.stringify(payload)
        });
    }

    // other code omitted
}

And when minified using BundlerMinifier the output is this:

function initializePage(h, ut, ft) {
    function yi() {
        var n = st();

        $.ajax({
            url: h,
            headers: {
                [tokenHeaderName]: ft
            },
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: fi,
            data: JSON.stringify(n)
        })
    }

    // other code omitted
}   

The bug in question is this line: [tokenHeaderName]: ft. As you can see the tokenHeaderName is passed into the initializePage function and the minifier will change this variable to ut but it will not replace this reference to it on this line.

When unminified, this code is supposed to add a new property to the new JavaScript object where the property name is the string value assigned to the variable. So if tokenHeaderName has the string value of "RequestToken" then the object would have a RequestToken field.

However, instead tokenHeaderName is undefined because the minifier renamed the variable.

Expected behavior The [tokenHeaderName]: tokenValue syntax in Javascript should also be minified to use the minified variable name.

So in the example given, this should have been minified to output: [ut]: ft