This project is read-only.
1
Vote

Variable renaming should consider existing variables that will not be renamed because of EvalTreatment value

description

Assume the following code:
var minifier = new Minifier();
var codeSettings = new CodeSettings();
codeSettings.EvalTreatment = EvalTreatment.MakeImmediateSafe;
codeSettings.PreserveImportantComments = false;

const string js = @"
var foo = (function () {
    var willBeRenamedToN = {
        baz: 'console.log(23)'
    };

    return {
        log: function (n) {
            console.log(n.text);
            eval(willBeRenamedToN.baz);
        }
    }
}());

foo.log({
    text: 'bar'
});";


var @out = minifier.MinifyJavaScript( js, codeSettings );
The value of @out is
var foo=function(){var n={baz:"console.log(23)"};return{log:function(n){console.log(n.text);eval(n.baz)}}}();foo.log({text:"bar"})
The original JS prints "bar" then 23. The minified prints only "bar".

The reason is that willBeRenamedToN is renamed to n even if it already exists in the function that has an eval. The result is that n.baz has a new meaning and the result is undefined. I understand that using MakeAllSafe as my EvalTreatment will "fix" the issue, but I feel like the renaming should be a little more aware of the context to avoid creating conflicts while renaming.

comments