Allow expand in array literals
This commit is contained in:
committed by
Richard van Velzen
parent
0a3d780327
commit
f9cab7ad61
@@ -371,15 +371,15 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
|
||||
}
|
||||
}, 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",
|
||||
$propdoc: {
|
||||
symbol: "AST_Symbol the thing to be expanded"
|
||||
expression: "AST_Symbol the thing to be expanded"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
var self = this;
|
||||
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);
|
||||
}
|
||||
if (node instanceof AST_Expansion) {
|
||||
out.push(node.symbol);
|
||||
out.push(node.expression);
|
||||
}
|
||||
}));
|
||||
return out;
|
||||
|
||||
@@ -657,7 +657,7 @@ function OutputStream(options) {
|
||||
|
||||
DEFPRINT(AST_Expansion, function (self, output) {
|
||||
output.print('...');
|
||||
self.symbol.print(output);
|
||||
self.expression.print(output);
|
||||
});
|
||||
|
||||
DEFPRINT(AST_Destructuring, function (self, output) {
|
||||
|
||||
@@ -1166,7 +1166,7 @@ function parse($TEXT, options) {
|
||||
next();
|
||||
a.push(new AST_Expansion({
|
||||
start: prev(),
|
||||
symbol: as_symbol(AST_SymbolFunarg),
|
||||
expression: as_symbol(AST_SymbolFunarg),
|
||||
end: S.token,
|
||||
}));
|
||||
} else {
|
||||
@@ -1385,7 +1385,7 @@ function parse($TEXT, options) {
|
||||
var symbol = _make_symbol(sym_type);
|
||||
children.push(new AST_Expansion({
|
||||
start: prev(),
|
||||
symbol: symbol,
|
||||
expression: symbol,
|
||||
end: S.token
|
||||
}));
|
||||
next();
|
||||
@@ -1586,6 +1586,9 @@ function parse($TEXT, options) {
|
||||
if (allow_trailing_comma && is("punc", closing)) break;
|
||||
if (is("punc", ",") && allow_empty) {
|
||||
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 {
|
||||
a.push(expression(false));
|
||||
}
|
||||
@@ -1956,7 +1959,7 @@ function parse($TEXT, options) {
|
||||
next();
|
||||
args.push(new AST_Expansion({
|
||||
start: prev(),
|
||||
symbol: as_symbol(AST_SymbolFunarg)
|
||||
expression: as_symbol(AST_SymbolFunarg)
|
||||
}));
|
||||
} else {
|
||||
args.push(expression(false));
|
||||
|
||||
@@ -227,4 +227,8 @@ TreeTransformer.prototype = new TreeWalker;
|
||||
self.value = self.value.transform(tw);
|
||||
});
|
||||
|
||||
_(AST_Expansion, function(self, tw){
|
||||
self.expression = self.expression.transform(tw);
|
||||
});
|
||||
|
||||
})();
|
||||
|
||||
@@ -72,3 +72,76 @@ constant_join_2: {
|
||||
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]
|
||||
}
|
||||
}
|
||||
@@ -117,7 +117,7 @@ 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.equal(expanding_def.name.names[1].expression.TYPE, 'SymbolVar');
|
||||
|
||||
// generators
|
||||
var generators_def = UglifyJS.parse('function* fn() {}').body[0];
|
||||
|
||||
Reference in New Issue
Block a user