Merge pull request #768 from fabiosantoscode/feature/harmony-destructuring-expression

Feature/harmony destructuring expression
This commit is contained in:
Richard van Velzen
2015-08-25 19:00:36 +02:00
6 changed files with 38 additions and 10 deletions

View File

@@ -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");
});

View File

@@ -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,

View File

@@ -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);

View File

@@ -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);
});

View File

@@ -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});"
}

View File

@@ -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