refine is_reachable()

add test
This commit is contained in:
alexlamsl 2017-12-28 01:48:37 +08:00
parent 294fcf2c23
commit 3c4bb1cf55
2 changed files with 47 additions and 9 deletions

View File

@ -4005,7 +4005,7 @@ merge(Compressor.prototype, {
}
if (defs) defs.push(arg.definition());
}
return !defs || defs.length == 0 || !fn.body[0].is_reachable(defs);
return !defs || defs.length == 0 || !is_reachable(fn.body[0], defs);
}
function flatten_args(fn) {
@ -4845,7 +4845,7 @@ merge(Compressor.prototype, {
return self;
});
AST_Node.DEFMETHOD("is_reachable", function(defs) {
function is_reachable(node, defs) {
var reachable = false;
var find_ref = new TreeWalker(function(node) {
if (reachable) return true;
@ -4853,15 +4853,19 @@ merge(Compressor.prototype, {
return reachable = true;
}
});
this.walk(new TreeWalker(function(node) {
var scan_scope = new TreeWalker(function(node) {
if (reachable) return true;
if (node instanceof AST_Scope) {
node.walk(find_ref);
var parent = scan_scope.parent();
if (!(parent instanceof AST_Call && parent.expression === node)) {
node.walk(find_ref);
}
return true;
}
}));
});
node.walk(scan_scope);
return reachable;
});
}
var ASSIGN_OPS = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ];
var ASSIGN_OPS_COMMUTATIVE = [ '*', '|', '^', '&' ];
@ -4876,7 +4880,7 @@ merge(Compressor.prototype, {
parent = compressor.parent(level++);
if (parent instanceof AST_Exit) {
if (in_try(level, parent instanceof AST_Throw)) break;
if (self.is_reachable([ def ])) break;
if (is_reachable(self, [ def ])) break;
if (self.operator == "=") return self.right;
return make_node(AST_Binary, self, {
operator: self.operator.slice(0, -1),

View File

@ -1509,7 +1509,7 @@ issue_2663_1: {
function createFn(j) {
return function() {
console.log(j);
}
};
}
for (i in { a: 1, b: 2, c: 3 })
o[i] = createFn(i);
@ -1523,7 +1523,7 @@ issue_2663_1: {
function createFn(j) {
return function() {
console.log(j);
}
};
}
for (i in { a: 1, b: 2, c: 3 })
o[i] = createFn(i);
@ -1539,6 +1539,40 @@ issue_2663_1: {
}
issue_2663_2: {
options = {
inline: true,
reduce_vars: true,
side_effects: true,
unused: true,
}
input: {
(function() {
var i;
function fn(j) {
return function() {
console.log(j);
}();
}
for (i in { a: 1, b: 2, c: 3 })
fn(i);
})();
}
expect: {
(function() {
var i;
for (i in { a: 1, b: 2, c: 3 })
j = i, console.log(j);
var j;
})();
}
expect_stdout: [
"a",
"b",
"c",
]
}
issue_2663_3: {
options = {
inline: true,
reduce_vars: true,