diff --git a/lib/output.js b/lib/output.js index e2f7f060..5d8bef10 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1179,6 +1179,23 @@ function OutputStream(options) { var def = self.definition(); output.print_name(def ? def.mangled_name || def.name : self.name); }); + 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 fd870bbb..3d3eb664 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1410,9 +1410,6 @@ function parse($TEXT, options) { if (!is("punc", ":")) { // It's one of those object destructurings, the value is its own name - if (!S.in_parameters) { - croak("Invalid syntax", S.token.line, S.token.col); - } a.push(new AST_ObjectSymbol({ start: start, end: start, diff --git a/lib/propmangle.js b/lib/propmangle.js index 840bda91..ff782b57 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -90,6 +90,9 @@ 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); @@ -111,6 +114,11 @@ 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 7858759a..266e686f 100644 --- a/lib/transform.js +++ b/lib/transform.js @@ -219,6 +219,10 @@ 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/destructuring.js b/test/compress/destructuring.js index 30fbbee8..6ea54ac1 100644 --- a/test/compress/destructuring.js +++ b/test/compress/destructuring.js @@ -32,3 +32,12 @@ destructuring_vardef_in_loops: { } expect_exact: "for(var[x,y]in pairs);for(var[a]=0;;);for(var{c}of cees);" } +destructuring_expressions: { + input: { + ({a, b}); + [{a}]; + f({x}); + } + expect_exact: "({a,b});[{a}];f({x});" +} + diff --git a/test/parser.js b/test/parser.js index 66676b9d..a84c2df9 100644 --- a/test/parser.js +++ b/test/parser.js @@ -118,13 +118,6 @@ module.exports = function () { ok.equal(expanding_def.name.names[0].TYPE, 'SymbolVar'); ok.equal(expanding_def.name.names[1].TYPE, 'Expansion'); ok.equal(expanding_def.name.names[1].symbol.TYPE, 'SymbolVar'); - - ok.throws(function () { - // Note: this *is* a valid destructuring, but before we implement - // destructuring (right now it's only destructuring *arguments*), - // this won't do. - UglifyJS.parse('[{a}]'); - }); } // Run standalone