add new arrows compress option (#2154)
Convert ES5 style anonymous function expressions to arrow functions if permissible by language semantics. Note: `arrows` requires that the `ecma` compress option is set to `6` or greater. fixes #2150
This commit is contained in:
@@ -48,6 +48,7 @@ function Compressor(options, false_by_default) {
|
||||
return new Compressor(options, false_by_default);
|
||||
TreeTransformer.call(this, this.before, this.after);
|
||||
this.options = defaults(options, {
|
||||
arrows : !false_by_default,
|
||||
booleans : !false_by_default,
|
||||
cascade : !false_by_default,
|
||||
collapse_vars : !false_by_default,
|
||||
@@ -2047,7 +2048,7 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
|
||||
OPT(AST_Block, function(self, compressor){
|
||||
if (!(self.body instanceof AST_Node)) tighten_body(self.body, compressor);
|
||||
tighten_body(self.body, compressor);
|
||||
return self;
|
||||
});
|
||||
|
||||
@@ -3273,11 +3274,12 @@ merge(Compressor.prototype, {
|
||||
var fun;
|
||||
ast.walk(new TreeWalker(function(node) {
|
||||
if (fun) return true;
|
||||
if (node instanceof AST_Lambda) {
|
||||
if (node instanceof AST_Function) {
|
||||
fun = node;
|
||||
return true;
|
||||
}
|
||||
}));
|
||||
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()
|
||||
@@ -4499,13 +4501,37 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
|
||||
OPT(AST_Arrow, function(self, compressor){
|
||||
if (self.body.length === 1 && self.body[0] instanceof AST_Return) {
|
||||
if (!(self.body instanceof AST_Node)) tighten_body(self.body, compressor);
|
||||
if (compressor.option("arrows")
|
||||
&& self.body.length == 1
|
||||
&& self.body[0] instanceof AST_Return) {
|
||||
var value = self.body[0].value;
|
||||
self.body = value ? value : [];
|
||||
}
|
||||
return self;
|
||||
});
|
||||
|
||||
OPT(AST_Function, function(self, compressor){
|
||||
tighten_body(self.body, compressor);
|
||||
if (compressor.option("arrows")
|
||||
&& compressor.option("ecma") >= 6
|
||||
&& !self.name
|
||||
&& !self.is_generator
|
||||
&& !self.uses_arguments
|
||||
&& !self.uses_eval) {
|
||||
var has_special_symbol = false;
|
||||
self.walk(new TreeWalker(function(node) {
|
||||
if (has_special_symbol) return true;
|
||||
if (node instanceof AST_Symbol && !node.definition()) {
|
||||
has_special_symbol = true;
|
||||
return true;
|
||||
}
|
||||
}));
|
||||
if (!has_special_symbol) return make_node(AST_Arrow, self, self).optimize(compressor);
|
||||
}
|
||||
return self;
|
||||
});
|
||||
|
||||
OPT(AST_Class, function(self, compressor){
|
||||
// HACK to avoid compress failure.
|
||||
// AST_Class is not really an AST_Scope/AST_Block as it lacks a body.
|
||||
|
||||
Reference in New Issue
Block a user