simplify computed properties for methods, getters & setters (#2555)

fixes #2554
This commit is contained in:
Alex Lam S.L
2017-12-04 00:18:48 +08:00
committed by GitHub
parent 606f7a5b37
commit 87bae623e9
2 changed files with 226 additions and 12 deletions

View File

@@ -5341,7 +5341,31 @@ merge(Compressor.prototype, {
return self;
});
// ["p"]:1 ---> p:1
// [42]:1 ---> 42:1
function lift_key(self, compressor) {
if (!compressor.option("computed_props")) return self;
// save a comparison in the typical case
if (!(self.key instanceof AST_Constant)) return self;
// whitelist acceptable props as not all AST_Constants are true constants
if (self.key instanceof AST_String || self.key instanceof AST_Number) {
if (self.key.value == "constructor"
&& compressor.parent() instanceof AST_Class) return self;
if (self instanceof AST_ObjectKeyVal) {
self.key = self.key.value;
} else {
self.key = make_node(AST_SymbolMethod, self.key, {
name: self.key.value
});
}
}
return self;
}
OPT(AST_ObjectProperty, lift_key);
OPT(AST_ConciseMethod, function(self, compressor){
lift_key(self, compressor);
// p(){return x;} ---> p:()=>x
if (compressor.option("arrows")
&& compressor.parent() instanceof AST_Object
@@ -5362,18 +5386,7 @@ merge(Compressor.prototype, {
});
OPT(AST_ObjectKeyVal, function(self, compressor){
// ["p"]:1 ---> p:1
// [42]:1 ---> 42:1
if (compressor.option("computed_props")
&& self.key instanceof AST_Constant // save a comparison in the typical case
&& (
// whitelist acceptable props as not all AST_Constants are true constants
self.key instanceof AST_String
|| self.key instanceof AST_Number
)) {
self.key = self.key.value;
// fallthrough - `return self` not needed as transformed tree in good form
}
lift_key(self, compressor);
// p:function(){} ---> p(){}
// p:function*(){} ---> *p(){}
// p:async function(){} ---> async p(){}