Destructuring consistency fixes (#1417)

- Use AST_Destructuring for lhf assignment patterns
- Use AST_DefaultAssign for default assignments
- Add more checks for lhs expressions
- Add lots of testing
- Cleanup ast (e.g. remove default property)
- Fix #1402 based on a patch from @kzc
- Refine spread allowance in array destructring pattern
- Add destructuring AST tree checker
This commit is contained in:
Anthony Van de Gejuchte
2017-02-24 01:49:19 +01:00
committed by Alex Lam S.L
parent 85c1cba760
commit 07734b000a
16 changed files with 1253 additions and 141 deletions

View File

@@ -74,6 +74,7 @@ function Compressor(options, false_by_default) {
pure_funcs : null,
negate_iife : !false_by_default,
screw_ie8 : true,
ecma : 5,
drop_console : false,
angular : false,
warnings : true,
@@ -2317,7 +2318,7 @@ merge(Compressor.prototype, {
if (!fun) return self;
var args = fun.argnames.map(function(arg, i){
return make_node(AST_String, self.args[i], {
value: arg.print_to_string()
value: arg.print_to_string({ecma: compressor.option("ecma")})
});
});
var code = OutputStream();
@@ -2886,6 +2887,22 @@ merge(Compressor.prototype, {
return self;
});
OPT(AST_DefaultAssign, function(self, compressor){
if (!compressor.option("evaluate")) {
return self;
}
var evaluateRight = self.right.evaluate(compressor);
// `[x = undefined] = foo` ---> `[x] = foo`
if (evaluateRight.length > 1 && evaluateRight[1] === undefined) {
self = self.left;
} else {
self.right = evaluateRight[0];
}
return self;
});
OPT(AST_Conditional, function(self, compressor){
if (!compressor.option("conditionals")) return self;
if (self.condition instanceof AST_Seq) {