correctly determine scope of AST_This (#2109)

fixes #2107
This commit is contained in:
Alex Lam S.L
2017-06-16 14:54:46 +08:00
committed by GitHub
parent 33405bb24b
commit 11e63bc335
2 changed files with 40 additions and 8 deletions

View File

@@ -3207,18 +3207,21 @@ merge(Compressor.prototype, {
if (body.length == 1 && body[0] instanceof AST_Return) { if (body.length == 1 && body[0] instanceof AST_Return) {
value = body[0].value; value = body[0].value;
if (!value) return make_node(AST_Undefined, self); if (!value) return make_node(AST_Undefined, self);
value.walk(new TreeWalker(function(node) { var tw = new TreeWalker(function(node) {
if (value === self) return true; if (value === self) return true;
if (node instanceof AST_SymbolRef && matches(node.scope.find_variable(node)) if (node instanceof AST_SymbolRef) {
|| node instanceof AST_This && matches(node)) { var ref = node.scope.find_variable(node);
if (ref && ref.scope.parent_scope === fn.parent_scope) {
value = self;
return true;
}
}
if (node instanceof AST_This && !tw.find_parent(AST_Scope)) {
value = self; value = self;
return true; return true;
} }
});
function matches(ref) { value.walk(tw);
return ref && ref.scope.parent_scope === fn.parent_scope;
}
}));
if (value !== self) value = best_of(compressor, value, self); if (value !== self) value = best_of(compressor, value, self);
} else { } else {
value = self; value = self;

View File

@@ -414,3 +414,32 @@ inner_ref: {
} }
expect_stdout: "1 undefined" expect_stdout: "1 undefined"
} }
issue_2107: {
options = {
cascade: true,
collapse_vars: true,
inline: true,
sequences: true,
side_effects: true,
unused: true,
}
input: {
var c = 0;
!function() {
c++;
}(c++ + new function() {
this.a = 0;
var a = (c = c + 1) + (c = 1 + c);
return c++ + a;
}());
console.log(c);
}
expect: {
var c = 0;
c++, new function() {
this.a = 0, c = 1 + (c += 1), c++;
}(), c++, console.log(c);
}
expect_stdout: "5"
}