enforce inline scope restriction (#2106)

fixes #2105
This commit is contained in:
Alex Lam S.L
2017-06-16 03:21:38 +08:00
committed by GitHub
parent 57dc4fb32f
commit 33405bb24b
2 changed files with 61 additions and 17 deletions

View File

@@ -2944,18 +2944,18 @@ merge(Compressor.prototype, {
OPT(AST_Call, function(self, compressor){ OPT(AST_Call, function(self, compressor){
var exp = self.expression; var exp = self.expression;
if (compressor.option("reduce_vars") && exp instanceof AST_SymbolRef) { var fn = exp;
var fixed = exp.fixed_value();
if (fixed instanceof AST_Function) exp = fixed;
}
if (compressor.option("unused") if (compressor.option("unused")
&& exp instanceof AST_Function && (fn instanceof AST_Function
&& !exp.uses_arguments || compressor.option("reduce_vars")
&& !exp.uses_eval) { && fn instanceof AST_SymbolRef
&& (fn = fn.fixed_value()) instanceof AST_Function)
&& !fn.uses_arguments
&& !fn.uses_eval) {
var pos = 0, last = 0; var pos = 0, last = 0;
for (var i = 0, len = self.args.length; i < len; i++) { for (var i = 0, len = self.args.length; i < len; i++) {
var trim = i >= exp.argnames.length; var trim = i >= fn.argnames.length;
if (trim || exp.argnames[i].__unused) { if (trim || fn.argnames[i].__unused) {
var node = self.args[i].drop_side_effect_free(compressor); var node = self.args[i].drop_side_effect_free(compressor);
if (node) { if (node) {
self.args[pos++] = node; self.args[pos++] = node;
@@ -3156,15 +3156,15 @@ merge(Compressor.prototype, {
} }
} }
} }
if (exp instanceof AST_Function) { var stat = fn instanceof AST_Function && fn.body[0];
var stat = exp.body[0]; if (compressor.option("inline") && stat instanceof AST_Return) {
if (compressor.option("inline") && stat instanceof AST_Return) { var value = stat.value;
var value = stat && stat.value; if (!value || value.is_constant_expression()) {
if (!value || value.is_constant_expression()) { var args = self.args.concat(value || make_node(AST_Undefined, self));
var args = self.args.concat(value || make_node(AST_Undefined, self)); return make_sequence(self, args).transform(compressor);
return make_sequence(self, args).transform(compressor);
}
} }
}
if (exp instanceof AST_Function) {
if (compressor.option("inline") if (compressor.option("inline")
&& !exp.name && !exp.name
&& exp.body.length == 1 && exp.body.length == 1

View File

@@ -1108,3 +1108,47 @@ var_catch_toplevel: {
}(); }();
} }
} }
issue_2105: {
options = {
collapse_vars: true,
inline: true,
reduce_vars: true,
side_effects: true,
unused: true,
}
input: {
!function(factory) {
factory();
}( function() {
return function(fn) {
fn()().prop();
}( function() {
function bar() {
var quux = function() {
console.log("PASS");
}, foo = function() {
console.log;
quux();
};
return { prop: foo };
}
return bar;
} );
});
}
expect: {
!void function() {
var quux = function() {
console.log("PASS");
};
return {
prop: function() {
console.log;
quux();
}
};
}().prop();
}
expect_stdout: "PASS"
}