Allow expand in array literals

This commit is contained in:
Anthony Van de Gejuchte
2016-06-11 01:40:45 +02:00
committed by Richard van Velzen
parent 0a3d780327
commit f9cab7ad61
6 changed files with 89 additions and 9 deletions

View File

@@ -371,15 +371,15 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
} }
}, AST_Scope); }, AST_Scope);
var AST_Expansion = DEFNODE("Expansion", "symbol", { var AST_Expansion = DEFNODE("Expansion", "expression", {
$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", $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",
$propdoc: { $propdoc: {
symbol: "AST_Symbol the thing to be expanded" expression: "AST_Symbol the thing to be expanded"
}, },
_walk: function(visitor) { _walk: function(visitor) {
var self = this; var self = this;
return visitor._visit(this, function(){ return visitor._visit(this, function(){
self.symbol.walk(visitor); self.expression.walk(visitor);
}); });
} }
}); });
@@ -516,7 +516,7 @@ var AST_Destructuring = DEFNODE("Destructuring", "names is_array default", {
out.push(node); out.push(node);
} }
if (node instanceof AST_Expansion) { if (node instanceof AST_Expansion) {
out.push(node.symbol); out.push(node.expression);
} }
})); }));
return out; return out;

View File

@@ -657,7 +657,7 @@ function OutputStream(options) {
DEFPRINT(AST_Expansion, function (self, output) { DEFPRINT(AST_Expansion, function (self, output) {
output.print('...'); output.print('...');
self.symbol.print(output); self.expression.print(output);
}); });
DEFPRINT(AST_Destructuring, function (self, output) { DEFPRINT(AST_Destructuring, function (self, output) {

View File

@@ -1166,7 +1166,7 @@ function parse($TEXT, options) {
next(); next();
a.push(new AST_Expansion({ a.push(new AST_Expansion({
start: prev(), start: prev(),
symbol: as_symbol(AST_SymbolFunarg), expression: as_symbol(AST_SymbolFunarg),
end: S.token, end: S.token,
})); }));
} else { } else {
@@ -1385,7 +1385,7 @@ function parse($TEXT, options) {
var symbol = _make_symbol(sym_type); var symbol = _make_symbol(sym_type);
children.push(new AST_Expansion({ children.push(new AST_Expansion({
start: prev(), start: prev(),
symbol: symbol, expression: symbol,
end: S.token end: S.token
})); }));
next(); next();
@@ -1586,6 +1586,9 @@ function parse($TEXT, options) {
if (allow_trailing_comma && is("punc", closing)) break; if (allow_trailing_comma && is("punc", closing)) break;
if (is("punc", ",") && allow_empty) { if (is("punc", ",") && allow_empty) {
a.push(new AST_Hole({ start: S.token, end: S.token })); a.push(new AST_Hole({ start: S.token, end: S.token }));
} else if (is("expand", "...")) {
next();
a.push(new AST_Expansion({start: S.token, expression: expression(),end: S.token}));
} else { } else {
a.push(expression(false)); a.push(expression(false));
} }
@@ -1956,7 +1959,7 @@ function parse($TEXT, options) {
next(); next();
args.push(new AST_Expansion({ args.push(new AST_Expansion({
start: prev(), start: prev(),
symbol: as_symbol(AST_SymbolFunarg) expression: as_symbol(AST_SymbolFunarg)
})); }));
} else { } else {
args.push(expression(false)); args.push(expression(false));

View File

@@ -227,4 +227,8 @@ TreeTransformer.prototype = new TreeWalker;
self.value = self.value.transform(tw); self.value = self.value.transform(tw);
}); });
_(AST_Expansion, function(self, tw){
self.expression = self.expression.transform(tw);
});
})(); })();

View File

@@ -72,3 +72,76 @@ constant_join_2: {
var f = "strstr" + variable + "foobarmoo" + foo; var f = "strstr" + variable + "foobarmoo" + foo;
} }
} }
spread_with_variable_as_last_element: {
input: {
var values = [4, 5, 6];
var a = [1, 2, 3, ...values];
}
expect: {
var values = [4, 5, 6];
var a = [1, 2, 3, ...values];
}
}
spread_with_variable_in_middle: {
input: {
var values = [4, 5, 6];
var a = [1, 2, 3, ...values, 7,,,];
}
expect: {
var values = [4, 5, 6];
var a = [1, 2, 3, ...values, 7,,,];
}
}
spread_with_variable_at_front: {
input: {
var values = [1, 2, 3];
var a = [...values, 4, 5, 6];
}
expect: {
var values = [1, 2, 3];
var a = [...values, 4, 5, 6];
}
}
spread_with_variable_at_front_after_elisions: {
input: {
var values = [1, 2, 3];
var a = [,,,...values, 4, 5, 6];
}
expect: {
var values = [1, 2, 3];
var a = [,,,...values, 4, 5, 6];
}
}
spread_with_array_at_end: {
input: {
var a = [1, 2, ...[4, 5, 6]];
}
expect: {
var a = [1, 2, ...[4, 5, 6]];
}
}
spread_with_logical_expression_at_end: {
options = { evaluate: true }
input: {
var a = [1, 2, 3, ...[2+2]]
}
expect: {
var a = [1, 2, 3, ...[4]]
}
}
spread_with_logical_expression_at_middle: {
options = { evaluate: true }
input: {
var a = [1, 1, ...[1+1, 1+2, 2+3], 8]
}
expect: {
var a = [1, 1, ...[2, 3, 5], 8]
}
}

View File

@@ -117,7 +117,7 @@ module.exports = function () {
ok.equal(expanding_def.name.names[0].TYPE, 'SymbolVar'); 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].TYPE, 'Expansion');
ok.equal(expanding_def.name.names[1].symbol.TYPE, 'SymbolVar'); ok.equal(expanding_def.name.names[1].expression.TYPE, 'SymbolVar');
// generators // generators
var generators_def = UglifyJS.parse('function* fn() {}').body[0]; var generators_def = UglifyJS.parse('function* fn() {}').body[0];