deferSpreadRestError.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. var tests = [
  7. {
  8. name: "Deferred spread/rest errors in lambda formals",
  9. body: function () {
  10. assert.doesNotThrow(function () { (a, b = [...[1,2,3]], ...rest) => {}; }, "Correct spread and rest usage");
  11. assert.doesNotThrow(function () { (a, b = ([...[1,2,3]]), ...rest) => {}; }, "Correct spread and rest usage with added parens");
  12. assert.doesNotThrow(function () { (a, b = (([...[1,2,3]])), ...rest) => {}; }, "Correct spread and rest usage with added parens");
  13. assert.throws(function () { eval("(a = ...NaN, b = [...[1,2,3]], ...rest) => {};"); },
  14. SyntaxError,
  15. "Invalid spread with valid rest throws on the first invalid spread",
  16. "Unexpected ... operator");
  17. assert.throws(function () { eval("(a = (...NaN), ...b = [...[1,2,3]], ...rest) => {};"); },
  18. SyntaxError,
  19. "Invalid spread in parens with invalid and valid rest throws on the first invalid spread",
  20. "Invalid use of the ... operator. Spread can only be used in call arguments or an array literal.");
  21. assert.throws(function () { eval("(a = (...NaN), ...b = [...[1,2,3]], rest) => {};"); },
  22. SyntaxError,
  23. "Invalid spread in parens with invalid rest throws on the first invalid spread",
  24. "Invalid use of the ... operator. Spread can only be used in call arguments or an array literal.");
  25. assert.throws(function () { eval("(a = [...NaN], ...b = [...[1,2,3]], rest) => {};"); },
  26. SyntaxError,
  27. "Invalid spread (runtime error) with invalid rest throws on the first invalid rest",
  28. "Unexpected ... operator");
  29. assert.throws(function () { eval("(a, ...b, ...rest) => {};"); },
  30. SyntaxError,
  31. "Invalid rest with valid rest throws on the first invalid rest",
  32. "Unexpected ... operator");
  33. assert.throws(function () { eval("(...rest = ...NaN) => {};"); },
  34. SyntaxError,
  35. "Invalid rest with invalid spread initializer throws on the invalid rest",
  36. "The rest parameter cannot have a default initializer.");
  37. }
  38. },
  39. {
  40. name: "Nested parenthesized expressions",
  41. body: function () {
  42. assert.throws(function () { eval("(function f() { if (...mznxbp) { (mmqykj) => undefined; } });"); },
  43. SyntaxError,
  44. "Parenthesized expression outside a function does not contribute to nested count",
  45. "Invalid use of the ... operator. Spread can only be used in call arguments or an array literal.");
  46. assert.throws(function () { eval("(a, (...b, ...a))"); },
  47. SyntaxError,
  48. "Nested parenthesized expression throws rest error before deferred spread error",
  49. "Unexpected ... operator");
  50. assert.throws(function () { eval("((...a)) => 1"); },
  51. SyntaxError,
  52. "Nested parenthesized expression as lambda formals throws deferred spread error",
  53. "Invalid use of the ... operator. Spread can only be used in call arguments or an array literal.");
  54. }
  55. }
  56. ];
  57. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });