enhance dead_code (#3551)
This commit is contained in:
@@ -3441,6 +3441,7 @@ merge(Compressor.prototype, {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Scope) {
|
if (node instanceof AST_Scope) {
|
||||||
|
if (node === self) return;
|
||||||
scopes.push(node);
|
scopes.push(node);
|
||||||
descend();
|
descend();
|
||||||
scopes.pop();
|
scopes.pop();
|
||||||
@@ -3451,6 +3452,7 @@ merge(Compressor.prototype, {
|
|||||||
result = false;
|
result = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (self.variables.has(node.name)) return true;
|
||||||
var def = node.definition();
|
var def = node.definition();
|
||||||
if (member(def.scope, scopes)) return true;
|
if (member(def.scope, scopes)) return true;
|
||||||
if (scope) {
|
if (scope) {
|
||||||
@@ -6429,24 +6431,37 @@ merge(Compressor.prototype, {
|
|||||||
var ASSIGN_OPS = makePredicate("+ - / * % >> << >>> | ^ &");
|
var ASSIGN_OPS = makePredicate("+ - / * % >> << >>> | ^ &");
|
||||||
var ASSIGN_OPS_COMMUTATIVE = makePredicate("* | ^ &");
|
var ASSIGN_OPS_COMMUTATIVE = makePredicate("* | ^ &");
|
||||||
OPT(AST_Assign, function(self, compressor) {
|
OPT(AST_Assign, function(self, compressor) {
|
||||||
var def;
|
if (compressor.option("dead_code")) {
|
||||||
if (compressor.option("dead_code")
|
if (self.left instanceof AST_PropAccess) {
|
||||||
&& self.left instanceof AST_SymbolRef
|
var exp = self.left.expression;
|
||||||
&& (def = self.left.definition()).scope === compressor.find_parent(AST_Lambda)) {
|
if (exp instanceof AST_Lambda
|
||||||
if (self.left.is_immutable()) return strip_assignment();
|
|| !compressor.has_directive("use strict")
|
||||||
var level = 0, node, parent = self;
|
&& exp instanceof AST_Constant
|
||||||
do {
|
&& !exp.may_throw_on_access(compressor)) {
|
||||||
node = parent;
|
return self.left instanceof AST_Dot ? self.right : make_sequence(self, [
|
||||||
parent = compressor.parent(level++);
|
self.left.property,
|
||||||
if (parent instanceof AST_Exit) {
|
self.right
|
||||||
if (in_try(level, parent)) break;
|
]).optimize(compressor);
|
||||||
if (is_reachable(def.scope, [ def ])) break;
|
|
||||||
def.fixed = false;
|
|
||||||
return strip_assignment();
|
|
||||||
}
|
}
|
||||||
} while (parent instanceof AST_Binary && parent.right === node
|
} else if (self.left instanceof AST_SymbolRef) {
|
||||||
|| parent instanceof AST_Sequence && parent.tail_node() === node
|
var def = self.left.definition();
|
||||||
|| parent instanceof AST_UnaryPrefix);
|
if (def.scope === compressor.find_parent(AST_Lambda)) {
|
||||||
|
if (self.left.is_immutable()) return strip_assignment();
|
||||||
|
var level = 0, node, parent = self;
|
||||||
|
do {
|
||||||
|
node = parent;
|
||||||
|
parent = compressor.parent(level++);
|
||||||
|
if (parent instanceof AST_Exit) {
|
||||||
|
if (in_try(level, parent)) break;
|
||||||
|
if (is_reachable(def.scope, [ def ])) break;
|
||||||
|
def.fixed = false;
|
||||||
|
return strip_assignment();
|
||||||
|
}
|
||||||
|
} while (parent instanceof AST_Binary && parent.right === node
|
||||||
|
|| parent instanceof AST_Sequence && parent.tail_node() === node
|
||||||
|
|| parent instanceof AST_UnaryPrefix);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self = self.lift_sequences(compressor);
|
self = self.lift_sequences(compressor);
|
||||||
if (!compressor.option("assignments")) return self;
|
if (!compressor.option("assignments")) return self;
|
||||||
|
|||||||
@@ -1013,3 +1013,32 @@ issue_3406: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "true"
|
expect_stdout: "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function_assign: {
|
||||||
|
options = {
|
||||||
|
dead_code: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
console.log(function() {
|
||||||
|
var a = "PASS";
|
||||||
|
function h(c) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
h.p = function(b) {
|
||||||
|
return b;
|
||||||
|
}.p = a;
|
||||||
|
return h;
|
||||||
|
}().p);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
console.log(function() {
|
||||||
|
var a = "PASS";
|
||||||
|
function h(c) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
h.p = a;
|
||||||
|
return h;
|
||||||
|
}().p);
|
||||||
|
}
|
||||||
|
expect_stdout: "PASS"
|
||||||
|
}
|
||||||
|
|||||||
@@ -2187,3 +2187,37 @@ issue_3515_3: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "PASS"
|
expect_stdout: "PASS"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function_assign: {
|
||||||
|
options = {
|
||||||
|
pure_getters: "strict",
|
||||||
|
reduce_vars: true,
|
||||||
|
side_effects: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
console.log(function() {
|
||||||
|
var a = "PASS";
|
||||||
|
function g(b) {
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
g.p = a;
|
||||||
|
function h(c) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
h.p = a;
|
||||||
|
return h;
|
||||||
|
}().p);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
console.log(function() {
|
||||||
|
var a = "PASS";
|
||||||
|
function h(c) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
h.p = a;
|
||||||
|
return h;
|
||||||
|
}().p);
|
||||||
|
}
|
||||||
|
expect_stdout: "PASS"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user