optimize unmodified variables
This commit is contained in:
parent
0111497fc9
commit
8646b15314
|
|
@ -67,6 +67,7 @@ function Compressor(options, false_by_default) {
|
||||||
if_return : !false_by_default,
|
if_return : !false_by_default,
|
||||||
join_vars : !false_by_default,
|
join_vars : !false_by_default,
|
||||||
collapse_vars : false,
|
collapse_vars : false,
|
||||||
|
reduce_vars : false,
|
||||||
cascade : !false_by_default,
|
cascade : !false_by_default,
|
||||||
side_effects : !false_by_default,
|
side_effects : !false_by_default,
|
||||||
pure_getters : false,
|
pure_getters : false,
|
||||||
|
|
@ -1107,7 +1108,7 @@ merge(Compressor.prototype, {
|
||||||
this._evaluating = true;
|
this._evaluating = true;
|
||||||
try {
|
try {
|
||||||
var d = this.definition();
|
var d = this.definition();
|
||||||
if (d && d.constant && d.init) {
|
if (d && (d.constant || compressor.option("reduce_vars") && !d.global && !d.modified) && d.init) {
|
||||||
return ev(d.init, compressor);
|
return ev(d.init, compressor);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
||||||
|
|
@ -197,7 +197,8 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
|
||||||
}
|
}
|
||||||
if (node instanceof AST_SymbolRef) {
|
if (node instanceof AST_SymbolRef) {
|
||||||
var name = node.name;
|
var name = node.name;
|
||||||
if (name == "eval" && tw.parent() instanceof AST_Call) {
|
var parent = tw.parent();
|
||||||
|
if (name == "eval" && parent instanceof AST_Call) {
|
||||||
for (var s = node.scope; s && !s.uses_eval; s = s.parent_scope) {
|
for (var s = node.scope; s && !s.uses_eval; s = s.parent_scope) {
|
||||||
s.uses_eval = true;
|
s.uses_eval = true;
|
||||||
}
|
}
|
||||||
|
|
@ -213,12 +214,14 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
|
||||||
g.global = true;
|
g.global = true;
|
||||||
globals.set(name, g);
|
globals.set(name, g);
|
||||||
}
|
}
|
||||||
node.thedef = g;
|
sym = g;
|
||||||
if (func && name == "arguments") {
|
if (func && name == "arguments") {
|
||||||
func.uses_arguments = true;
|
func.uses_arguments = true;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
node.thedef = sym;
|
node.thedef = sym;
|
||||||
|
if (parent instanceof AST_Assign || parent instanceof AST_Unary && (parent.operator === '++' || parent.operator === '--')) {
|
||||||
|
sym.modified = true;
|
||||||
}
|
}
|
||||||
node.reference();
|
node.reference();
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
60
test/compress/reduce_vars.js
Normal file
60
test/compress/reduce_vars.js
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
reduce_vars: {
|
||||||
|
options = {
|
||||||
|
conditionals : true,
|
||||||
|
evaluate : true,
|
||||||
|
global_defs : {
|
||||||
|
C : 0
|
||||||
|
},
|
||||||
|
reduce_vars : true,
|
||||||
|
unused : true
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var A = 1;
|
||||||
|
(function f0() {
|
||||||
|
var a = 2;
|
||||||
|
console.log(a - 5);
|
||||||
|
console.log(A - 5);
|
||||||
|
})();
|
||||||
|
(function f1() {
|
||||||
|
var a = 2;
|
||||||
|
console.log(a - 5);
|
||||||
|
eval("console.log(a);");
|
||||||
|
})();
|
||||||
|
(function f2(eval) {
|
||||||
|
var a = 2;
|
||||||
|
console.log(a - 5);
|
||||||
|
eval("console.log(a);");
|
||||||
|
})(eval);
|
||||||
|
(function f3() {
|
||||||
|
var b = typeof C !== "undefined";
|
||||||
|
var c = 4;
|
||||||
|
if (b) {
|
||||||
|
return 'yes';
|
||||||
|
} else {
|
||||||
|
return 'no';
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
console.log(A + 1);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var A = 1;
|
||||||
|
(function() {
|
||||||
|
console.log(-3);
|
||||||
|
console.log(A - 5);
|
||||||
|
})();
|
||||||
|
(function f1() {
|
||||||
|
var a = 2;
|
||||||
|
console.log(-3);
|
||||||
|
eval("console.log(a);");
|
||||||
|
})();
|
||||||
|
(function f2(eval) {
|
||||||
|
var a = 2;
|
||||||
|
console.log(-3);
|
||||||
|
eval("console.log(a);");
|
||||||
|
})(eval);
|
||||||
|
(function() {
|
||||||
|
return "yes";
|
||||||
|
})();
|
||||||
|
console.log(A + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user