| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
|
| 6 |
const bindingPatterns = [ |
| 7 |
"[]", |
| 8 |
"[a]", |
| 9 |
"[a, b]", |
| 10 |
"[a, ...b]", |
| 11 |
"[...a]", |
| 12 |
"[...[]]", |
| 13 |
|
| 14 |
"{}", |
| 15 |
"{p: a}", |
| 16 |
"{p: a = 0}", |
| 17 |
"{p: {}}", |
| 18 |
"{p: a, q: b}", |
| 19 |
"{a}", |
| 20 |
"{a, b}", |
| 21 |
"{a = 0}", |
| 22 |
]; |
| 23 |
|
| 24 |
const functions = [ |
| 25 |
p => `function f(${p}) {}`, |
| 26 |
p => `function* g(${p}) {}`, |
| 27 |
p => `({m(${p}) {}});`, |
| 28 |
p => `(class {m(${p}) {}});`, |
| 29 |
p => `(${p}) => {};`, |
| 30 |
]; |
| 31 |
|
| 32 |
for (let pattern of bindingPatterns) { |
| 33 |
for (let fn of functions) { |
| 34 |
// No leading parameters. |
| 35 |
eval(fn(`...${pattern}`)); |
| 36 |
|
| 37 |
// Leading normal parameters. |
| 38 |
eval(fn(`x, ...${pattern}`)); |
| 39 |
eval(fn(`x, y, ...${pattern}`)); |
| 40 |
|
| 41 |
// Leading parameters with defaults. |
| 42 |
eval(fn(`x = 0, ...${pattern}`)); |
| 43 |
eval(fn(`x = 0, y = 0, ...${pattern}`)); |
| 44 |
|
| 45 |
// Leading array destructuring parameters. |
| 46 |
eval(fn(`[], ...${pattern}`)); |
| 47 |
eval(fn(`[x], ...${pattern}`)); |
| 48 |
eval(fn(`[x = 0], ...${pattern}`)); |
| 49 |
eval(fn(`[...x], ...${pattern}`)); |
| 50 |
|
| 51 |
// Leading object destructuring parameters. |
| 52 |
eval(fn(`{}, ...${pattern}`)); |
| 53 |
eval(fn(`{p: x}, ...${pattern}`)); |
| 54 |
eval(fn(`{x}, ...${pattern}`)); |
| 55 |
eval(fn(`{x = 0}, ...${pattern}`)); |
| 56 |
|
| 57 |
// Trailing parameters after rest parameter. |
| 58 |
assertThrowsInstanceOf(() => eval(fn(`...${pattern},`)), SyntaxError); |
| 59 |
assertThrowsInstanceOf(() => eval(fn(`...${pattern}, x`)), SyntaxError); |
| 60 |
assertThrowsInstanceOf(() => eval(fn(`...${pattern}, x = 0`)), SyntaxError); |
| 61 |
assertThrowsInstanceOf(() => eval(fn(`...${pattern}, ...x`)), SyntaxError); |
| 62 |
assertThrowsInstanceOf(() => eval(fn(`...${pattern}, []`)), SyntaxError); |
| 63 |
assertThrowsInstanceOf(() => eval(fn(`...${pattern}, {}`)), SyntaxError); |
| 64 |
|
| 65 |
// Rest parameter with defaults. |
| 66 |
assertThrowsInstanceOf(() => eval(fn(`...${pattern} = 0`)), SyntaxError); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
for (let fn of functions) { |
| 71 |
// Missing name, incomplete patterns. |
| 72 |
assertThrowsInstanceOf(() => eval(fn(`...`)), SyntaxError); |
| 73 |
assertThrowsInstanceOf(() => eval(fn(`...[`)), SyntaxError); |
| 74 |
assertThrowsInstanceOf(() => eval(fn(`...{`)), SyntaxError); |
| 75 |
|
| 76 |
// Invalid binding name. |
| 77 |
assertThrowsInstanceOf(() => eval(fn(`...[0]`)), SyntaxError); |
| 78 |
assertThrowsInstanceOf(() => eval(fn(`...[p.q]`)), SyntaxError); |
| 79 |
} |
| 80 |
|
| 81 |
// Rest parameters aren't valid in getter/setter methods. |
| 82 |
assertThrowsInstanceOf(() => eval(`({get p(...[]) {}})`), SyntaxError); |
| 83 |
assertThrowsInstanceOf(() => eval(`({set p(...[]) {}})`), SyntaxError); |
| 84 |
|
| 85 |
|
| 86 |
if (typeof reportCompare === "function") |
| 87 |
reportCompare(0, 0); |