@@ -790,6 +790,7 @@ merge(Compressor.prototype, {
|
|||||||
right: candidate.value
|
right: candidate.value
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
candidate.write_only = false;
|
||||||
return candidate;
|
return candidate;
|
||||||
}
|
}
|
||||||
// These node types have child nodes that execute sequentially,
|
// These node types have child nodes that execute sequentially,
|
||||||
@@ -2177,7 +2178,12 @@ merge(Compressor.prototype, {
|
|||||||
var drop_funcs = !(self instanceof AST_Toplevel) || compressor.toplevel.funcs;
|
var drop_funcs = !(self instanceof AST_Toplevel) || compressor.toplevel.funcs;
|
||||||
var drop_vars = !(self instanceof AST_Toplevel) || compressor.toplevel.vars;
|
var drop_vars = !(self instanceof AST_Toplevel) || compressor.toplevel.vars;
|
||||||
if (!drop_funcs && !drop_vars) return;
|
if (!drop_funcs && !drop_vars) return;
|
||||||
var assign_as_unused = !/keep_assign/.test(compressor.option("unused"));
|
var assign_as_unused = /keep_assign/.test(compressor.option("unused")) ? return_false : function(node) {
|
||||||
|
if (node instanceof AST_Assign && (node.write_only || node.operator == "=")) {
|
||||||
|
return node.left;
|
||||||
|
}
|
||||||
|
if (node instanceof AST_Unary && node.write_only) return node.expression;
|
||||||
|
};
|
||||||
var in_use = [];
|
var in_use = [];
|
||||||
var in_use_ids = Object.create(null); // avoid expensive linear scans of in_use
|
var in_use_ids = Object.create(null); // avoid expensive linear scans of in_use
|
||||||
if (self instanceof AST_Toplevel && compressor.top_retain) {
|
if (self instanceof AST_Toplevel && compressor.top_retain) {
|
||||||
@@ -2227,12 +2233,8 @@ merge(Compressor.prototype, {
|
|||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (assign_as_unused
|
if (assign_as_unused(node) instanceof AST_SymbolRef && scope === self) {
|
||||||
&& node instanceof AST_Assign
|
if (node instanceof AST_Assign) node.right.walk(tw);
|
||||||
&& node.operator == "="
|
|
||||||
&& node.left instanceof AST_SymbolRef
|
|
||||||
&& scope === self) {
|
|
||||||
node.right.walk(tw);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_SymbolRef) {
|
if (node instanceof AST_SymbolRef) {
|
||||||
@@ -2396,14 +2398,17 @@ merge(Compressor.prototype, {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (drop_vars && assign_as_unused
|
if (drop_vars) {
|
||||||
&& node instanceof AST_Assign
|
var def = assign_as_unused(node);
|
||||||
&& node.operator == "="
|
if (def instanceof AST_SymbolRef
|
||||||
&& node.left instanceof AST_SymbolRef) {
|
&& !((def = def.definition()).id in in_use_ids)
|
||||||
var def = node.left.definition();
|
|
||||||
if (!(def.id in in_use_ids)
|
|
||||||
&& self.variables.get(def.name) === def) {
|
&& self.variables.get(def.name) === def) {
|
||||||
return maintain_this_binding(tt.parent(), node, node.right.transform(tt));
|
if (node instanceof AST_Assign) {
|
||||||
|
return maintain_this_binding(tt.parent(), node, node.right.transform(tt));
|
||||||
|
}
|
||||||
|
return make_node(AST_Number, node, {
|
||||||
|
value: 0
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// certain combination of unused name + side effect leads to:
|
// certain combination of unused name + side effect leads to:
|
||||||
@@ -2642,7 +2647,10 @@ merge(Compressor.prototype, {
|
|||||||
return make_sequence(this, [ left, right ]);
|
return make_sequence(this, [ left, right ]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
def(AST_Assign, return_this);
|
def(AST_Assign, function(compressor){
|
||||||
|
this.write_only = !this.left.has_side_effects(compressor);
|
||||||
|
return this;
|
||||||
|
});
|
||||||
def(AST_Conditional, function(compressor){
|
def(AST_Conditional, function(compressor){
|
||||||
var consequent = this.consequent.drop_side_effect_free(compressor);
|
var consequent = this.consequent.drop_side_effect_free(compressor);
|
||||||
var alternative = this.alternative.drop_side_effect_free(compressor);
|
var alternative = this.alternative.drop_side_effect_free(compressor);
|
||||||
@@ -2663,7 +2671,10 @@ merge(Compressor.prototype, {
|
|||||||
return node;
|
return node;
|
||||||
});
|
});
|
||||||
def(AST_Unary, function(compressor, first_in_statement){
|
def(AST_Unary, function(compressor, first_in_statement){
|
||||||
if (unary_side_effects(this.operator)) return this;
|
if (unary_side_effects(this.operator)) {
|
||||||
|
this.write_only = !this.expression.has_side_effects(compressor);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef) return null;
|
if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef) return null;
|
||||||
var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
|
var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
|
||||||
if (first_in_statement
|
if (first_in_statement
|
||||||
@@ -3485,7 +3496,7 @@ merge(Compressor.prototype, {
|
|||||||
operator: car.operator,
|
operator: car.operator,
|
||||||
expression: left
|
expression: left
|
||||||
});
|
});
|
||||||
}
|
} else car.write_only = false;
|
||||||
if (parent) {
|
if (parent) {
|
||||||
parent[field] = car;
|
parent[field] = car;
|
||||||
expressions[i] = expressions[j];
|
expressions[i] = expressions[j];
|
||||||
|
|||||||
@@ -863,7 +863,7 @@ collapse_vars_unary: {
|
|||||||
input: {
|
input: {
|
||||||
function f0(o, p) {
|
function f0(o, p) {
|
||||||
var x = o[p];
|
var x = o[p];
|
||||||
delete x;
|
return delete x;
|
||||||
}
|
}
|
||||||
function f1(n) {
|
function f1(n) {
|
||||||
var k = !!n;
|
var k = !!n;
|
||||||
@@ -893,7 +893,7 @@ collapse_vars_unary: {
|
|||||||
expect: {
|
expect: {
|
||||||
function f0(o, p) {
|
function f0(o, p) {
|
||||||
var x = o[p];
|
var x = o[p];
|
||||||
delete x;
|
return delete x;
|
||||||
}
|
}
|
||||||
function f1(n) {
|
function f1(n) {
|
||||||
return n > +!!n
|
return n > +!!n
|
||||||
|
|||||||
@@ -1090,6 +1090,7 @@ var_catch_toplevel: {
|
|||||||
a--;
|
a--;
|
||||||
try {
|
try {
|
||||||
a++;
|
a++;
|
||||||
|
x();
|
||||||
} catch(a) {
|
} catch(a) {
|
||||||
if (a) var a;
|
if (a) var a;
|
||||||
var a = 10;
|
var a = 10;
|
||||||
@@ -1099,9 +1100,8 @@ var_catch_toplevel: {
|
|||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
!function() {
|
!function() {
|
||||||
a--;
|
|
||||||
try {
|
try {
|
||||||
a++;
|
x();
|
||||||
} catch(a) {
|
} catch(a) {
|
||||||
var a;
|
var a;
|
||||||
}
|
}
|
||||||
@@ -1153,3 +1153,89 @@ issue_2105: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "PASS"
|
expect_stdout: "PASS"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
issue_2226_1: {
|
||||||
|
options = {
|
||||||
|
side_effects: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f1() {
|
||||||
|
var a = b;
|
||||||
|
a += c;
|
||||||
|
}
|
||||||
|
function f2(a) {
|
||||||
|
a <<= b;
|
||||||
|
}
|
||||||
|
function f3(a) {
|
||||||
|
--a;
|
||||||
|
}
|
||||||
|
function f4() {
|
||||||
|
var a = b;
|
||||||
|
return a *= c;
|
||||||
|
}
|
||||||
|
function f5(a) {
|
||||||
|
x(a /= b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f1() {
|
||||||
|
b;
|
||||||
|
c;
|
||||||
|
}
|
||||||
|
function f2(a) {
|
||||||
|
b;
|
||||||
|
}
|
||||||
|
function f3(a) {
|
||||||
|
0;
|
||||||
|
}
|
||||||
|
function f4() {
|
||||||
|
var a = b;
|
||||||
|
return a *= c;
|
||||||
|
}
|
||||||
|
function f5(a) {
|
||||||
|
x(a /= b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
issue_2226_2: {
|
||||||
|
options = {
|
||||||
|
cascade: true,
|
||||||
|
sequences: true,
|
||||||
|
side_effects: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
console.log(function(a, b) {
|
||||||
|
a += b;
|
||||||
|
return a;
|
||||||
|
}(1, 2));
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
console.log(function(a, b) {
|
||||||
|
return a += b;
|
||||||
|
}(1, 2));
|
||||||
|
}
|
||||||
|
expect_stdout: "3"
|
||||||
|
}
|
||||||
|
|
||||||
|
issue_2226_3: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
side_effects: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
console.log(function(a, b) {
|
||||||
|
a += b;
|
||||||
|
return a;
|
||||||
|
}(1, 2));
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
console.log(function(a, b) {
|
||||||
|
return a += 2;
|
||||||
|
}(1));
|
||||||
|
}
|
||||||
|
expect_stdout: "3"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user