enhance dead_code (#3575)

This commit is contained in:
Alex Lam S.L
2019-11-08 13:45:28 +08:00
committed by GitHub
parent 87e67ec299
commit 10648c9af6
2 changed files with 60 additions and 17 deletions

View File

@@ -6543,14 +6543,20 @@ merge(Compressor.prototype, {
}
}
} else if (self.left instanceof AST_SymbolRef) {
var def = self.left.definition();
if (def.scope === compressor.find_parent(AST_Lambda)) {
if (self.left.is_immutable()) return strip_assignment();
var def = self.left.definition();
var local = def.scope.resolve() === compressor.find_parent(AST_Lambda);
var level = 0, node, parent = self;
do {
node = parent;
parent = compressor.parent(level++);
if (parent instanceof AST_Exit) {
if (parent instanceof AST_Assign) {
if (!(parent.left instanceof AST_SymbolRef)) continue;
if (parent.left.definition() !== def) continue;
def.fixed = false;
return strip_assignment();
} else if (parent instanceof AST_Exit) {
if (!local) break;
if (in_try(level, parent)) break;
if (is_reachable(def.scope, [ def ])) break;
def.fixed = false;
@@ -6561,7 +6567,6 @@ merge(Compressor.prototype, {
|| parent instanceof AST_UnaryPrefix);
}
}
}
self = self.lift_sequences(compressor);
if (!compressor.option("assignments")) return self;
if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
@@ -6599,7 +6604,7 @@ merge(Compressor.prototype, {
self.right = make_node(AST_Null, right);
var may_throw = node.may_throw(compressor);
self.right = right;
var scope = self.left.definition().scope;
var scope = self.left.definition().scope.resolve();
var parent;
while ((parent = compressor.parent(level++)) !== scope) {
if (parent instanceof AST_Try) {

View File

@@ -1064,3 +1064,41 @@ issue_3552: {
}
expect_stdout: "PASS"
}
unreachable_assign: {
options = {
dead_code: true,
}
input: {
console.log(A = "P" + (A = "A" + (B = "S" + (A = B = "S"))), A, B);
}
expect: {
console.log(A = "P" + "A" + (B = "S" + "S"), A, B);
}
expect_stdout: "PASS PASS SS"
}
catch_return_assign: {
options = {
dead_code: true,
}
input: {
console.log(function() {
try {
throw "FAIL";
} catch (e) {
return e = "PASS";
}
}());
}
expect: {
console.log(function() {
try {
throw "FAIL";
} catch (e) {
return "PASS";
}
}());
}
expect_stdout: "PASS"
}