implement object rest/spread (#2265)

- improve parse errors for destructuring spread elements
- `unsafe` for object literals with rest elements

Miscellaneous
- increase mocha unicode surrogate test timeout
This commit is contained in:
kzc
2017-08-02 01:47:58 -04:00
committed by Alex Lam S.L
parent f54ab16843
commit 4700c14855
5 changed files with 117 additions and 16 deletions

View File

@@ -1522,7 +1522,7 @@ function parse($TEXT, options) {
}
if (is_expand) {
if (!is("punc", "]")) {
unexpected(); // Must be last element
croak("Rest element must be last element");
}
elements[elements.length - 1] = new AST_Expansion({
start: expand_token,
@@ -1547,18 +1547,33 @@ function parse($TEXT, options) {
} else {
expect(",");
}
if (is("expand", "...")) {
is_expand = true;
expand_token = S.token;
used_parameters.mark_spread(S.token);
next();
}
if (is("name") && (is_token(peek(), "punc") || is_token(peek(), "operator")) && [",", "}", "="].indexOf(peek().value) !== -1) {
used_parameters.add_parameter(S.token);
elements.push(new AST_ObjectKeyVal({
start: prev(),
key: S.token.value,
value: new symbol_type({
start: S.token,
name: S.token.value,
end: S.token
}),
end: prev()
}));
var value = new symbol_type({
start: S.token,
name: S.token.value,
end: S.token,
});
if (is_expand) {
elements.push(new AST_Expansion({
start: expand_token,
expression: value,
end: value.end,
}));
} else {
elements.push(new AST_ObjectKeyVal({
start: prev(),
key: S.token.value,
value: value,
end: value.end,
}));
}
next();
} else if (is("punc", "}")) {
continue; // Allow trailing hole
@@ -1589,7 +1604,12 @@ function parse($TEXT, options) {
}));
}
}
if (is("operator", "=")) {
if (is_expand) {
if (!is("punc", "}")) {
croak("Rest element must be last element");
}
}
else if (is("operator", "=")) {
used_parameters.mark_default_assignment(S.token);
next();
elements[elements.length - 1].value = new AST_DefaultAssign({
@@ -2143,7 +2163,18 @@ function parse($TEXT, options) {
if (!options.strict && is("punc", "}"))
// allow trailing comma
break;
start = S.token;
if (start.type == "expand") {
next();
a.push(new AST_Expansion({
start: start,
expression: expression(false),
end: prev(),
}));
continue;
}
var name = as_property_name();
var value;