fix corner cases in switch and undefined (#1762)

- fix side effects in switch condition for singular blocks
- fix `undefined` confusion with local variable
- gate `OPT(AST_Switch)` with `switches`

fixes #1758
fixes #1759
This commit is contained in:
Alex Lam S.L
2017-04-02 14:52:25 +08:00
committed by GitHub
parent c076e7b60d
commit f7ca4f2297
5 changed files with 159 additions and 15 deletions

View File

@@ -77,6 +77,7 @@ function Compressor(options, false_by_default) {
screw_ie8 : true,
sequences : !false_by_default,
side_effects : !false_by_default,
switches : !false_by_default,
top_retain : null,
toplevel : !!(options && options["top_retain"]),
unsafe : false,
@@ -1054,7 +1055,7 @@ merge(Compressor.prototype, {
stat.value = cons_seq(stat.value);
}
else if (stat instanceof AST_Exit) {
stat.value = cons_seq(make_node(AST_Undefined, stat));
stat.value = cons_seq(make_node(AST_Undefined, stat).transform(compressor));
}
else if (stat instanceof AST_Switch) {
stat.expression = cons_seq(stat.expression);
@@ -2526,6 +2527,7 @@ merge(Compressor.prototype, {
});
OPT(AST_Switch, function(self, compressor){
if (!compressor.option("switches")) return self;
var branch;
var value = self.expression.evaluate(compressor);
if (value !== self.expression) {
@@ -2599,7 +2601,15 @@ merge(Compressor.prototype, {
has_break = true;
});
self.walk(tw);
if (!has_break) return make_node(AST_BlockStatement, self, body[0]).optimize(compressor);
if (!has_break) {
body = body[0].body.slice();
body.unshift(make_node(AST_SimpleStatement, self.expression, {
body: self.expression
}));
return make_node(AST_BlockStatement, self, {
body: body
}).optimize(compressor);
}
}
return self;
@@ -2904,7 +2914,7 @@ merge(Compressor.prototype, {
if (name instanceof AST_SymbolRef
&& name.name == "console"
&& name.undeclared()) {
return make_node(AST_Undefined, self).transform(compressor);
return make_node(AST_Undefined, self).optimize(compressor);
}
}
}