fix variable accounting in inline (#2085)

fixes #2084
This commit is contained in:
Alex Lam S.L
2017-06-13 01:40:14 +08:00
committed by GitHub
parent 5ef7cb372a
commit 2bdc8802dd
3 changed files with 67 additions and 20 deletions

View File

@@ -3171,16 +3171,16 @@ merge(Compressor.prototype, {
&& !exp.uses_arguments && !exp.uses_arguments
&& !exp.uses_eval && !exp.uses_eval
&& !self.has_pure_annotation(compressor)) { && !self.has_pure_annotation(compressor)) {
var body; var value;
if (stat instanceof AST_Return) { if (stat instanceof AST_Return) {
body = stat.value.clone(true); value = stat.value.clone(true);
} else if (stat instanceof AST_SimpleStatement) { } else if (stat instanceof AST_SimpleStatement) {
body = []; value = make_node(AST_UnaryPrefix, stat, {
merge_sequence(body, stat.body.clone(true)); operator: "void",
merge_sequence(body, make_node(AST_Undefined, self)); expression: stat.body.clone(true)
body = make_sequence(self, body); });
} }
if (body) { if (value) {
var fn = exp.clone(); var fn = exp.clone();
fn.argnames = []; fn.argnames = [];
fn.body = []; fn.body = [];
@@ -3200,17 +3200,25 @@ merge(Compressor.prototype, {
})); }));
} }
fn.body.push(make_node(AST_Return, self, { fn.body.push(make_node(AST_Return, self, {
value: body value: value
})); }));
body = fn.transform(compressor).body; var body = fn.transform(compressor).body;
if (body.length == 0) return make_node(AST_Undefined, self); if (body.length == 0) return make_node(AST_Undefined, self);
if (body.length == 1 && body[0] instanceof AST_Return) { if (body.length == 1 && body[0] instanceof AST_Return) {
if (!body[0].value) return make_node(AST_Undefined, self); value = body[0].value;
body = best_of(compressor, body[0].value, self); if (!value) return make_node(AST_Undefined, self);
value.walk(new TreeWalker(function(node) {
if (value === self) return true;
if (node instanceof AST_SymbolRef && exp.variables.has(node.name)) {
value = self;
return true;
}
}));
if (value !== self) value = best_of(compressor, value, self);
} else { } else {
body = self; value = self;
} }
if (body !== self) return body; if (value !== self) return value;
} }
} }
if (compressor.option("side_effects") && all(exp.body, is_empty)) { if (compressor.option("side_effects") && all(exp.body, is_empty)) {

View File

@@ -297,3 +297,42 @@ webkit: {
expect_exact: "console.log((function(){1+1}).a=1);" expect_exact: "console.log((function(){1+1}).a=1);"
expect_stdout: "1" expect_stdout: "1"
} }
issue_2084: {
options = {
collapse_vars: true,
conditionals: true,
evaluate: true,
inline: true,
passes: 2,
reduce_vars: true,
sequences: true,
side_effects: true,
unused: true,
}
input: {
var c = 0;
!function() {
!function(c) {
c = 1 + c;
var c = 0;
function f14(a_1) {
if (c = 1 + c, 0 !== 23..toString())
c = 1 + c, a_1 && (a_1[0] = 0);
}
f14();
}(-1);
}();
console.log(c);
}
expect: {
var c = 0;
!function(c) {
c = 1 + c,
c = 1 + (c = 0),
0 !== 23..toString() && (c = 1 + c);
}(-1),
console.log(c);
}
expect_stdout: "0"
}

View File

@@ -151,7 +151,7 @@ negate_iife_4: {
})(); })();
} }
expect: { expect: {
t ? console.log(true) : console.log(false), console.log("something"), void 0; t ? console.log(true) : console.log(false), void console.log("something");
} }
} }
@@ -174,7 +174,7 @@ negate_iife_5: {
})(); })();
} }
expect: { expect: {
t ? foo(true) : bar(false), console.log("something"), void 0; t ? foo(true) : bar(false), void console.log("something");
} }
} }
@@ -197,7 +197,7 @@ negate_iife_5_off: {
})(); })();
} }
expect: { expect: {
t ? foo(true) : bar(false), console.log("something"), void 0; t ? foo(true) : bar(false), void console.log("something");
} }
} }
@@ -214,7 +214,7 @@ issue_1254_negate_iife_true: {
}; };
})()(); })()();
} }
expect_exact: 'console.log("test"),void 0;' expect_exact: 'void console.log("test");'
expect_stdout: true expect_stdout: true
} }
@@ -231,7 +231,7 @@ issue_1254_negate_iife_nested: {
}; };
})()()()()(); })()()()()();
} }
expect_exact: '(console.log("test"),void 0)()()();' expect_exact: '(void console.log("test"))()()();'
} }
negate_iife_issue_1073: { negate_iife_issue_1073: {
@@ -382,7 +382,7 @@ wrap_iife: {
}; };
})()(); })()();
} }
expect_exact: 'console.log("test"),void 0;' expect_exact: 'void console.log("test");'
} }
wrap_iife_in_expression: { wrap_iife_in_expression: {
@@ -416,7 +416,7 @@ wrap_iife_in_return_call: {
})(); })();
})()(); })()();
} }
expect_exact: '(console.log("test"),void 0)();' expect_exact: '(void console.log("test"))();'
} }
pure_annotation: { pure_annotation: {