implement compress option computed_props (#2361)

transforms `{["computed"]: 1}` into `{computed: 1}`
This commit is contained in:
kzc
2017-10-15 23:35:04 -04:00
committed by Alex Lam S.L
parent f79f737fb2
commit f496ac5c85
3 changed files with 77 additions and 0 deletions

View File

@@ -53,6 +53,7 @@ function Compressor(options, false_by_default) {
cascade : !false_by_default,
collapse_vars : !false_by_default,
comparisons : !false_by_default,
computed_props: !false_by_default,
conditionals : !false_by_default,
dead_code : !false_by_default,
drop_console : false,
@@ -4869,6 +4870,18 @@ 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
}
// p:function(){} ---> p(){}
// p:function*(){} ---> *p(){}
// p:async function(){} ---> async p(){}