improve parse errors for destructuring spread elements

This commit is contained in:
kzc 2017-08-01 23:39:04 -04:00
parent 6f52e39ad3
commit 38e33407df
2 changed files with 10 additions and 4 deletions

View File

@ -1522,7 +1522,7 @@ function parse($TEXT, options) {
} }
if (is_expand) { if (is_expand) {
if (!is("punc", "]")) { if (!is("punc", "]")) {
unexpected(); // Must be last element token_error(S.token, "Rest element must be last element");
} }
elements[elements.length - 1] = new AST_Expansion({ elements[elements.length - 1] = new AST_Expansion({
start: expand_token, start: expand_token,
@ -1606,7 +1606,7 @@ function parse($TEXT, options) {
} }
if (is_expand) { if (is_expand) {
if (!is("punc", "}")) { if (!is("punc", "}")) {
unexpected(); // Must be last element token_error(S.token, "Rest element must be last element");
} }
} }
else if (is("operator", "=")) { else if (is("operator", "=")) {

View File

@ -253,13 +253,19 @@ describe("Left-hand side expressions", function () {
// Multiple spreads are not allowed in destructuring array // Multiple spreads are not allowed in destructuring array
expect("[...a, ...b] = [1, 2, 3, 4]", "Spread must the be last element in destructuring array"); expect("[...a, ...b] = [1, 2, 3, 4]", "Spread must the be last element in destructuring array");
// Array spread must be last in destructuring declaration
expect("let [ ...x, a ] = o;", "Rest element must be last element");
// Only one spread per destructuring array declaration
expect("let [ a, ...x, ...y ] = o;", "Rest element must be last element");
// Spread in block should not be allowed // Spread in block should not be allowed
expect("{...a} = foo", "Unexpected token: expand (...)"); expect("{...a} = foo", "Unexpected token: expand (...)");
// Object spread must be last in destructuring declaration // Object spread must be last in destructuring declaration
expect("let { ...x, a } = o;", "Unexpected token: punc (,)"); expect("let { ...x, a } = o;", "Rest element must be last element");
// Only one spread per destructuring declaration // Only one spread per destructuring declaration
expect("let { a, ...x, ...y } = o;", "Unexpected token: punc (,)"); expect("let { a, ...x, ...y } = o;", "Rest element must be last element");
}); });
}); });