Skip to content

Commit

Permalink
avoid arguments and eval in reduce_vars (#1924)
Browse files Browse the repository at this point in the history
fixes #1922
  • Loading branch information
alexlamsl committed May 12, 2017
1 parent aa7e878 commit f631d64
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
16 changes: 11 additions & 5 deletions lib/compress.js
Expand Up @@ -326,10 +326,14 @@ merge(Compressor.prototype, {
// So existing transformation rules can work on them.
node.argnames.forEach(function(arg, i) {
var d = arg.definition();
d.fixed = function() {
return iife.args[i] || make_node(AST_Undefined, iife);
};
mark(d, true);
if (!node.uses_arguments && d.fixed === undefined) {
d.fixed = function() {
return iife.args[i] || make_node(AST_Undefined, iife);
};
mark(d, true);
} else {
d.fixed = false;
}
});
}
if (node instanceof AST_If || node instanceof AST_DWLoop) {
Expand Down Expand Up @@ -414,7 +418,9 @@ merge(Compressor.prototype, {

function reset_def(def) {
def.escaped = false;
if (toplevel || !def.global || def.orig[0] instanceof AST_SymbolConst) {
if (def.scope.uses_eval) {
def.fixed = false;
} else if (toplevel || !def.global || def.orig[0] instanceof AST_SymbolConst) {
def.fixed = undefined;
} else {
def.fixed = false;
Expand Down
57 changes: 52 additions & 5 deletions test/compress/reduce_vars.js
Expand Up @@ -41,22 +41,22 @@ reduce_vars: {
var A = 1;
(function() {
console.log(-3);
console.log(-4);
console.log(A - 5);
})();
(function f1() {
var a = 2;
console.log(-3);
console.log(a - 5);
eval("console.log(a);");
})();
(function f2(eval) {
var a = 2;
console.log(-3);
console.log(a - 5);
eval("console.log(a);");
})(eval);
(function() {
return "yes";
})();
console.log(2);
console.log(A + 1);
}
expect_stdout: true
}
Expand Down Expand Up @@ -1732,7 +1732,10 @@ redefine_arguments_3: {
console.log(function() {
var arguments;
return typeof arguments;
}(), "number", "undefined");
}(), "number", function(x) {
var arguments = x;
return typeof arguments;
}());
}
expect_stdout: "object number undefined"
}
Expand Down Expand Up @@ -2122,3 +2125,47 @@ issue_1865: {
}
expect_stdout: true
}

issue_1922_1: {
options = {
evaluate: true,
reduce_vars: true,
unused: true,
}
input: {
console.log(function(a) {
arguments[0] = 2;
return a;
}(1));
}
expect: {
console.log(function(a) {
arguments[0] = 2;
return a;
}(1));
}
expect_stdout: "2"
}

issue_1922_2: {
options = {
evaluate: true,
reduce_vars: true,
unused: true,
}
input: {
console.log(function() {
var a;
eval("a = 1");
return a;
}(1));
}
expect: {
console.log(function() {
var a;
eval("a = 1");
return a;
}(1));
}
expect_stdout: "1"
}

0 comments on commit f631d64

Please sign in to comment.