fix dead_code on exceptional return (#2930)

fixes #2929
This commit is contained in:
Alex Lam S.L
2018-02-18 04:36:00 +08:00
committed by GitHub
parent 4a528c469c
commit 2351a672ea
2 changed files with 36 additions and 5 deletions

View File

@@ -2863,6 +2863,9 @@ merge(Compressor.prototype, {
def(AST_ObjectProperty, function(compressor){ def(AST_ObjectProperty, function(compressor){
return this.value.may_throw(compressor); return this.value.may_throw(compressor);
}); });
def(AST_Return, function(compressor){
return this.value.may_throw(compressor);
});
def(AST_Sequence, function(compressor){ def(AST_Sequence, function(compressor){
return any(this.expressions, compressor); return any(this.expressions, compressor);
}); });
@@ -2882,8 +2885,7 @@ merge(Compressor.prototype, {
return !this.is_declared(compressor); return !this.is_declared(compressor);
}); });
def(AST_Try, function(compressor){ def(AST_Try, function(compressor){
return any(this.body, compressor) return this.bcatch ? this.bcatch.may_throw(compressor) : any(this.body, compressor)
|| this.bcatch && this.bcatch.may_throw(compressor)
|| this.bfinally && this.bfinally.may_throw(compressor); || this.bfinally && this.bfinally.may_throw(compressor);
}); });
def(AST_Unary, function(compressor){ def(AST_Unary, function(compressor){
@@ -5511,7 +5513,7 @@ merge(Compressor.prototype, {
node = parent; node = parent;
parent = compressor.parent(level++); parent = compressor.parent(level++);
if (parent instanceof AST_Exit) { if (parent instanceof AST_Exit) {
if (in_try(level, parent instanceof AST_Throw)) break; if (in_try(level, parent)) break;
if (is_reachable(def.scope, [ def ])) break; if (is_reachable(def.scope, [ def ])) break;
if (self.operator == "=") return self.right; if (self.operator == "=") return self.right;
def.fixed = false; def.fixed = false;
@@ -5545,13 +5547,17 @@ merge(Compressor.prototype, {
} }
return self; return self;
function in_try(level, no_catch) { function in_try(level, node) {
var right = self.right;
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;
var parent; var parent;
while ((parent = compressor.parent(level++)) !== scope) { while ((parent = compressor.parent(level++)) !== scope) {
if (parent instanceof AST_Try) { if (parent instanceof AST_Try) {
if (parent.bfinally) return true; if (parent.bfinally) return true;
if (no_catch && parent.bcatch) return true; if (may_throw && parent.bcatch) return true;
} }
} }
} }

View File

@@ -917,3 +917,28 @@ issue_2860_2: {
} }
expect_stdout: "1" expect_stdout: "1"
} }
issue_2929: {
options = {
dead_code: true,
}
input: {
console.log(function(a) {
try {
return null.p = a = 1;
} catch (e) {
return a ? "PASS" : "FAIL";
}
}());
}
expect: {
console.log(function(a) {
try {
return null.p = a = 1;
} catch (e) {
return a ? "PASS" : "FAIL";
}
}());
}
expect_stdout: "PASS"
}