| 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 |
// Trailing comma in functions and generators. |
| 6 |
|
| 7 |
// 14.1 Function Definitions |
| 8 |
// FunctionDeclaration[Yield, Default]: |
| 9 |
// function BindingIdentifier[?Yield] ( FormalParameters[~Yield] ) { FunctionBody[~Yield] } |
| 10 |
|
| 11 |
// 14.4 Generator Function Definitions |
| 12 |
// GeneratorDeclaration[Yield, Default]: |
| 13 |
// function * BindingIdentifier[?Yield] ( FormalParameters[+Yield] ) { GeneratorBody } |
| 14 |
|
| 15 |
|
| 16 |
function functionDeclaration(argList, parameters = "", returnExpr = "") { |
| 17 |
return eval(` |
| 18 |
function f(${argList}) { |
| 19 |
var fun = f; |
| 20 |
return ${returnExpr}; |
| 21 |
} |
| 22 |
f(${parameters}); |
| 23 |
`); |
| 24 |
} |
| 25 |
|
| 26 |
function generatorDeclaration(argList, parameters = "", returnExpr = "") { |
| 27 |
return eval(` |
| 28 |
function* f(${argList}) { |
| 29 |
var fun = f; |
| 30 |
return ${returnExpr}; |
| 31 |
} |
| 32 |
f(${parameters}).next().value; |
| 33 |
`); |
| 34 |
} |
| 35 |
|
| 36 |
const tests = [ |
| 37 |
functionDeclaration, |
| 38 |
generatorDeclaration, |
| 39 |
]; |
| 40 |
|
| 41 |
// Ensure parameters are passed correctly. |
| 42 |
for (let test of tests) { |
| 43 |
assertEq(test("a, ", "10", "a"), 10); |
| 44 |
assertEq(test("a, b, ", "10, 20", "a + b"), 30); |
| 45 |
assertEq(test("a = 30, ", "", "a"), 30); |
| 46 |
assertEq(test("a = 30, b = 40, ", "", "a + b"), 70); |
| 47 |
|
| 48 |
assertEq(test("[a], ", "[10]", "a"), 10); |
| 49 |
assertEq(test("[a], [b], ", "[10], [20]", "a + b"), 30); |
| 50 |
assertEq(test("[a] = [30], ", "", "a"), 30); |
| 51 |
assertEq(test("[a] = [30], [b] = [40], ", "", "a + b"), 70); |
| 52 |
|
| 53 |
assertEq(test("{a}, ", "{a: 10}", "a"), 10); |
| 54 |
assertEq(test("{a}, {b}, ", "{a: 10}, {b: 20}", "a + b"), 30); |
| 55 |
assertEq(test("{a} = {a: 30}, ", "", "a"), 30); |
| 56 |
assertEq(test("{a} = {a: 30}, {b} = {b: 40}, ", "", "a + b"), 70); |
| 57 |
} |
| 58 |
|
| 59 |
// Ensure function length doesn't change. |
| 60 |
for (let test of tests) { |
| 61 |
assertEq(test("a, ", "", "fun.length"), 1); |
| 62 |
assertEq(test("a, b, ", "", "fun.length"), 2); |
| 63 |
|
| 64 |
assertEq(test("[a], ", "[]", "fun.length"), 1); |
| 65 |
assertEq(test("[a], [b], ", "[], []", "fun.length"), 2); |
| 66 |
|
| 67 |
assertEq(test("{a}, ", "{}", "fun.length"), 1); |
| 68 |
assertEq(test("{a}, {b}, ", "{}, {}", "fun.length"), 2); |
| 69 |
} |
| 70 |
|
| 71 |
for (let test of tests) { |
| 72 |
// Trailing comma in empty parameters list. |
| 73 |
assertThrowsInstanceOf(() => test(","), SyntaxError); |
| 74 |
|
| 75 |
// Leading comma. |
| 76 |
assertThrowsInstanceOf(() => test(", a"), SyntaxError); |
| 77 |
assertThrowsInstanceOf(() => test(", ...a"), SyntaxError); |
| 78 |
|
| 79 |
// Multiple trailing comma. |
| 80 |
assertThrowsInstanceOf(() => test("a, , "), SyntaxError); |
| 81 |
assertThrowsInstanceOf(() => test("a..., , "), SyntaxError); |
| 82 |
|
| 83 |
// Trailing comma after rest parameter. |
| 84 |
assertThrowsInstanceOf(() => test("...a, "), SyntaxError); |
| 85 |
assertThrowsInstanceOf(() => test("a, ...b, "), SyntaxError); |
| 86 |
|
| 87 |
// Elision. |
| 88 |
assertThrowsInstanceOf(() => test("a, , b"), SyntaxError); |
| 89 |
} |
| 90 |
|
| 91 |
if (typeof reportCompare === "function") |
| 92 |
reportCompare(0, 0); |