diff --git a/lib/ast.js b/lib/ast.js index 160b1cc2..fc03a2c0 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -395,9 +395,9 @@ var AST_ArrowParametersOrSeq = DEFNODE("ArrowParametersOrSeq", "expressions", { default: default_seen_above, names: ex.properties.map(to_fun_args) }); - } else if (ex instanceof AST_ObjectSymbol) { + } else if (ex instanceof AST_ObjectKeyVal && ex.shorthand) { return new AST_SymbolFunarg({ - name: ex.symbol.name, + name: ex.key, start: ex.start, end: ex.end }); @@ -981,10 +981,11 @@ var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", { } }); -var AST_ObjectKeyVal = DEFNODE("ObjectKeyVal", "quote", { +var AST_ObjectKeyVal = DEFNODE("ObjectKeyVal", "quote shorthand", { $documentation: "A key: value object property", $propdoc: { - quote: "[string] the original quote character" + quote: "[string] the original quote character", + shorthand: "[boolean] whether this is a shorthand key:value pair, expressed as just the key." } }, AST_ObjectProperty); @@ -998,18 +999,6 @@ var AST_ObjectComputedKeyVal = DEFNODE("ObjectComputedKeyVal", null, { } }, AST_ObjectProperty); -var AST_ObjectSymbol = DEFNODE("ObjectSymbol", "symbol", { - $propdoc: { - symbol: "[AST_SymbolRef] what symbol it is" - }, - $documentation: "A symbol in an object", - _walk: function (visitor) { - return visitor._visit(this, function(){ - this.symbol._walk(visitor); - }); - } -}, AST_ObjectProperty); - var AST_ObjectSetter = DEFNODE("ObjectSetter", "static", { $propdoc: { static: "[boolean] whether this is a static setter (classes only)" diff --git a/lib/output.js b/lib/output.js index 0dd5dcb9..cbd13893 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1301,6 +1301,14 @@ function OutputStream(options) { DEFPRINT(AST_ObjectKeyVal, function(self, output){ var key = self.key; var quote = self.quote; + var print_as_shorthand = self.shorthand && + self.value instanceof AST_Symbol && + self.key == self.value.print_to_string(); + + if (print_as_shorthand) { + output.print_name(key); + return; + } if (output.option("quote_keys")) { output.print_string(key + ""); } else if ((typeof key == "number" @@ -1359,23 +1367,6 @@ function OutputStream(options) { self.default.print(output) } }); - DEFPRINT(AST_ObjectSymbol, function(self, output){ - var name = self.mangled_key || self.symbol.name; - var def = self.symbol.definition(); - if (def && def.mangled_name) { - output.print(name); - output.print(':'); - output.space(); - output.print(def.mangled_name); - } else if (!(def && def.mangled_name) && self.mangled_key) { - output.print(name); - output.print(':'); - output.space(); - output.print(def.name); - } else { - output.print(name); - } - }); DEFPRINT(AST_Undefined, function(self, output){ output.print("void 0"); }); diff --git a/lib/parse.js b/lib/parse.js index 5463576d..6bc79215 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1514,14 +1514,16 @@ function parse($TEXT, options) { })); } else if (!is("punc", ":")) { // It's one of those object destructurings, the value is its own name - a.push(new AST_ObjectSymbol({ + a.push(new AST_ObjectKeyVal({ start: start, end: start, - symbol: new AST_SymbolRef({ + key: name, + value: new AST_SymbolRef({ start: start, end: start, name: name - }) + }), + shorthand: true, })); } else { expect(":"); diff --git a/lib/propmangle.js b/lib/propmangle.js index 86da5de9..050f07d7 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -90,9 +90,6 @@ function mangle_properties(ast, options) { if (node instanceof AST_ObjectKeyVal) { add(node.key); } - else if (node instanceof AST_ObjectSymbol) { - add(node.symbol.name); - } else if (node instanceof AST_ObjectProperty) { // setter or getter, since KeyVal is handled above add(node.key.name); @@ -117,11 +114,6 @@ function mangle_properties(ast, options) { if (node instanceof AST_ObjectKeyVal) { node.key = mangle(node.key); } - else if (node instanceof AST_ObjectSymbol) { - if (should_mangle(node.symbol.name)) { - node.mangled_key = mangle(node.symbol.name) - } - } else if (node instanceof AST_ObjectProperty) { // setter or getter node.key.name = mangle(node.key.name); diff --git a/lib/transform.js b/lib/transform.js index 2cea8705..dc3a068f 100644 --- a/lib/transform.js +++ b/lib/transform.js @@ -219,10 +219,6 @@ TreeTransformer.prototype = new TreeWalker; self.properties = do_list(self.properties, tw); }); - _(AST_ObjectSymbol, function(self, tw){ - self.symbol = self.symbol.transform(tw); - }); - _(AST_ObjectProperty, function(self, tw){ self.value = self.value.transform(tw); }); diff --git a/test/compress/harmony.js b/test/compress/harmony.js index fec3a835..da3f9d4e 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -54,6 +54,20 @@ computed_property_names: { expect_exact: 'obj({["x"+"x"]:6});' } +shorthand_properties: { + mangle = true; + input: (function() { + var prop = 1; + const value = {prop}; + return value; + })(); + expect: (function() { + var a = 1; + const b = {prop:a}; + return b; + })(); +} + typeof_arrow_functions: { options = { evaluate: true