trailing_comma_arrow.js (3878B)
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 CoverParenthesizedExpressionAndArrowParameterList production. 6 7 // 12.2 Primary Expression 8 // CoverParenthesizedExpressionAndArrowParameterList[Yield]: 9 // ( Expression[In, ?Yield] ) 10 // ( Expression[In, ?Yield] , ) 11 // () 12 // ( ...BindingIdentifier[?Yield] ) 13 // ( Expression[In, ?Yield] , ...BindingIdentifier[?Yield] ) 14 15 16 function arrow(argList, parameters = "", returnExpr = "") { 17 return eval(` 18 let fun = (${argList}) => { 19 return ${returnExpr}; 20 } 21 fun(${parameters}); 22 `); 23 } 24 25 function arrowConcise(argList, parameters = "", returnExpr = "null") { 26 return eval(` 27 let fun = (${argList}) => ${returnExpr}; 28 fun(${parameters}); 29 `); 30 } 31 32 const tests = [ 33 arrow, 34 arrowConcise, 35 ]; 36 37 // Ensure parameters are passed correctly. 38 for (let test of tests) { 39 assertEq(test("a, ", "10", "a"), 10); 40 assertEq(test("a, b, ", "10, 20", "a + b"), 30); 41 assertEq(test("a = 30, ", "", "a"), 30); 42 assertEq(test("a = 30, b = 40, ", "", "a + b"), 70); 43 44 assertEq(test("[a], ", "[10]", "a"), 10); 45 assertEq(test("[a], [b], ", "[10], [20]", "a + b"), 30); 46 assertEq(test("[a] = [30], ", "", "a"), 30); 47 assertEq(test("[a] = [30], [b] = [40], ", "", "a + b"), 70); 48 49 assertEq(test("{a}, ", "{a: 10}", "a"), 10); 50 assertEq(test("{a}, {b}, ", "{a: 10}, {b: 20}", "a + b"), 30); 51 assertEq(test("{a} = {a: 30}, ", "", "a"), 30); 52 assertEq(test("{a} = {a: 30}, {b} = {b: 40}, ", "", "a + b"), 70); 53 } 54 55 // Ensure function length doesn't change. 56 for (let test of tests) { 57 assertEq(test("a, ", "", "fun.length"), 1); 58 assertEq(test("a, b, ", "", "fun.length"), 2); 59 60 assertEq(test("[a], ", "[]", "fun.length"), 1); 61 assertEq(test("[a], [b], ", "[], []", "fun.length"), 2); 62 63 assertEq(test("{a}, ", "{}", "fun.length"), 1); 64 assertEq(test("{a}, {b}, ", "{}, {}", "fun.length"), 2); 65 } 66 67 for (let test of tests) { 68 // Trailing comma in empty parameters list. 69 assertThrowsInstanceOf(() => test(","), SyntaxError); 70 71 // Leading comma. 72 assertThrowsInstanceOf(() => test(", a"), SyntaxError); 73 assertThrowsInstanceOf(() => test(", ...a"), SyntaxError); 74 75 // Multiple trailing comma. 76 assertThrowsInstanceOf(() => test("a, , "), SyntaxError); 77 assertThrowsInstanceOf(() => test("a..., , "), SyntaxError); 78 79 // Trailing comma after rest parameter. 80 assertThrowsInstanceOf(() => test("...a, "), SyntaxError); 81 assertThrowsInstanceOf(() => test("a, ...b, "), SyntaxError); 82 83 // Elision. 84 assertThrowsInstanceOf(() => test("a, , b"), SyntaxError); 85 } 86 87 // Trailing comma in non-parenthesized arrow head. 88 assertThrowsInstanceOf(() => eval("a, => {}"), SyntaxError); 89 assertThrowsInstanceOf(() => eval("a, => null"), SyntaxError); 90 91 // Parenthesized expression is not an arrow function expression. 92 for (let trail of ["", ";", "\n => {}"]) { 93 assertThrowsInstanceOf(() => eval("(a,)" + trail), SyntaxError); 94 assertThrowsInstanceOf(() => eval("(a, b,)" + trail), SyntaxError); 95 assertThrowsInstanceOf(() => eval("(...a, )" + trail), SyntaxError); 96 assertThrowsInstanceOf(() => eval("(a, ...b, )" + trail), SyntaxError); 97 assertThrowsInstanceOf(() => eval("(a, , b)" + trail), SyntaxError); 98 99 assertThrowsInstanceOf(() => eval("(,)" + trail), SyntaxError); 100 assertThrowsInstanceOf(() => eval("(, a)" + trail), SyntaxError); 101 assertThrowsInstanceOf(() => eval("(, ...a)" + trail), SyntaxError); 102 assertThrowsInstanceOf(() => eval("(a, , )" + trail), SyntaxError); 103 assertThrowsInstanceOf(() => eval("(...a, , )" + trail), SyntaxError); 104 } 105 106 107 if (typeof reportCompare === "function") 108 reportCompare(0, 0);