Tolerate expansions in vardefs, too!

This commit is contained in:
Fábio Santos
2015-08-14 02:19:53 +01:00
committed by Richard van Velzen
parent d4f17f29ae
commit 079aaa0d48
3 changed files with 22 additions and 11 deletions

View File

@@ -363,14 +363,10 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
} }
}, AST_Scope); }, AST_Scope);
// TODO besides parameters and function calls, expansions can go in var AST_Expansion = DEFNODE("Expansion", "symbol", {
// arrays, array destructuring parameters, and array destructuring $documentation: "An expandible argument, such as ...rest, a splat, such as [1,2,...all], or an expansion in a variable declaration, such as var [first, ...rest] = list",
// assignment. But I'm not adding this right now because I'm trying
// to do the most minimal and independent changesets.
var AST_Expansion = DEFNODE("AST_Expansion", "symbol", {
$documentation: "An expandible argument, such as ...rest",
$propdoc: { $propdoc: {
symbol: "AST_SymbolFunarg the name of the argument as a SymbolFunarg" symbol: "AST_Symbol the thing to be expanded"
}, },
_walk: function(visitor) { _walk: function(visitor) {
var self = this; var self = this;

View File

@@ -1221,6 +1221,15 @@ function parse($TEXT, options) {
children.push(new AST_Hole({ start: S.token, end: S.token })); children.push(new AST_Hole({ start: S.token, end: S.token }));
} else if (is("punc", "[") || is("punc", "{")) { } else if (is("punc", "[") || is("punc", "{")) {
children.push(destructuring_(sym_type)); children.push(destructuring_(sym_type));
} else if (is("expand", "...")) {
next();
var symbol = _make_symbol(sym_type);
children.push(new AST_Expansion({
start: prev(),
symbol: symbol,
end: S.token
}));
next();
} else if (is("name")) { } else if (is("name")) {
children.push(_make_symbol(sym_type)); children.push(_make_symbol(sym_type));
next(); next();

View File

@@ -105,13 +105,19 @@ module.exports = function () {
var nested_def = UglifyJS.parse('var [{x}] = foo').body[0].definitions[0]; var nested_def = UglifyJS.parse('var [{x}] = foo').body[0].definitions[0];
ok.equal(nested_def.name.names[0].names[0].TYPE, 'SymbolVar') ok.equal(nested_def.name.names[0].names[0].TYPE, 'SymbolVar');
ok.equal(nested_def.name.names[0].names[0].name, 'x') ok.equal(nested_def.name.names[0].names[0].name, 'x');
var holey_def = UglifyJS.parse('const [,,third] = [1,2,3]').body[0].definitions[0]; var holey_def = UglifyJS.parse('const [,,third] = [1,2,3]').body[0].definitions[0];
ok.equal(holey_def.name.names[0].TYPE, 'Hole') ok.equal(holey_def.name.names[0].TYPE, 'Hole');
ok.equal(holey_def.name.names[2].TYPE, 'SymbolConst') ok.equal(holey_def.name.names[2].TYPE, 'SymbolConst');
var expanding_def = UglifyJS.parse('var [first, ...rest] = [1,2,3]').body[0].definitions[0];
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 () { ok.throws(function () {
// Note: this *is* a valid destructuring, but before we implement // Note: this *is* a valid destructuring, but before we implement