function-destructuring-parameters.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. describe("parsing", () => {
  2. test("single parameter, single name", () => {
  3. expect(`function testFunction({ a }) { }`).toEval();
  4. });
  5. test("single parameter, single name with rest values", () => {
  6. expect(`function testFunction({ a, ...b }) { }`).toEval();
  7. });
  8. test("single parameter, single aliased name", () => {
  9. expect(`function testFunction({ a: b }) { }`).toEval();
  10. });
  11. test("single parameter, single aliased name with rest values", () => {
  12. expect(`function testFunction({ a: b, ...c }) { }`).toEval();
  13. });
  14. test("multiple parameters, single destructuring parameter", () => {
  15. expect(`function testFunction(a, { b }) { }`).toEval();
  16. });
  17. test("multiple destructuring parameters", () => {
  18. expect(`function testFunction({ a }, { b }) { }`).toEval();
  19. });
  20. test("multiple destructuring parameters with rest parameters", () => {
  21. expect(`function testFunction({ a }, { bar, ...b }) { }`).toEval();
  22. });
  23. test("multiple destructuring parameters with rest parameters 2", () => {
  24. expect(`function testFunction({ bar, ...a }, { bar, ...b }) { }`).toEval();
  25. });
  26. test("multiple destructuring parameters, array patterns", () => {
  27. expect(`function testFunction({ bar, ...a }, [ b ]) { }`).toEval();
  28. });
  29. test("multiple destructuring parameters with rest parameters, array patterns", () => {
  30. expect(`function testFunction({ bar, ...a }, [ b, ...rest ]) { }`).toEval();
  31. });
  32. test("multiple destructuring parameters with rest parameters, array patterns with recursive patterns", () => {
  33. expect(`function testFunction({ bar, ...a }, [ b, [ c ] ]) { }`).toEval();
  34. });
  35. test("multiple destructuring parameters with rest parameters, array patterns with recursive patterns 2", () => {
  36. expect(`function testFunction({ bar, ...a }, [ b, [ c, ...{ d } ] ]) { }`).toEval();
  37. });
  38. });
  39. describe("evaluating", () => {
  40. test("single parameter, single name", () => {
  41. function testFunction({ a }) {
  42. return a;
  43. }
  44. expect(testFunction({ a: 42 })).toBe(42);
  45. });
  46. test("single parameter, single name with rest values", () => {
  47. function testFunction({ a, ...b }) {
  48. return b.foo;
  49. }
  50. expect(testFunction({ a: 42, foo: "yoinks" })).toBe("yoinks");
  51. });
  52. test("single parameter, single aliased name", () => {
  53. function testFunction({ a: b }) {
  54. return b;
  55. }
  56. expect(testFunction({ a: 42, foo: "yoinks" })).toBe(42);
  57. });
  58. test("single parameter, single aliased name with rest values", () => {
  59. function testFunction({ a: b, ...c }) {
  60. return b + c.foo;
  61. }
  62. expect(testFunction({ a: 42, foo: "yoinks" })).toBe("42yoinks");
  63. });
  64. test("multiple parameters, single destructuring parameter", () => {
  65. function testFunction(a, { b }) {
  66. return a + b;
  67. }
  68. expect(testFunction("27", { b: 42 })).toBe("2742");
  69. });
  70. test("multiple destructuring parameters", () => {
  71. function testFunction({ a }, { b }) {
  72. return a + b;
  73. }
  74. expect(testFunction({ a: "27" }, { b: 42 })).toBe("2742");
  75. });
  76. test("multiple destructuring parameters with rest parameters", () => {
  77. function testFunction({ a }, { bar, ...b }) {
  78. return bar;
  79. }
  80. expect(testFunction({ a: "27" }, { b: 42 })).toBeUndefined();
  81. });
  82. test("multiple destructuring parameters with rest parameters 2", () => {
  83. function testFunction({ bar, ...a }, { bar, ...b }) {
  84. return a.foo + b.foo;
  85. }
  86. expect(testFunction({ foo: "27" }, { foo: 42 })).toBe("2742");
  87. });
  88. test("multiple destructuring parameters, array patterns", () => {
  89. function testFunction({ bar, ...a }, [b]) {
  90. return a.foo + b;
  91. }
  92. expect(testFunction({ foo: "27" }, [42])).toBe("2742");
  93. });
  94. test("multiple destructuring parameters with rest parameters, array patterns", () => {
  95. function testFunction({ bar, ...a }, [b, ...rest]) {
  96. return rest.length + a.foo;
  97. }
  98. expect(testFunction({ foo: "20" }, [0, 1, 2, 3, 4])).toBe("420");
  99. });
  100. test("multiple destructuring parameters with rest parameters, array patterns with recursive patterns", () => {
  101. function testFunction({ bar, ...a }, [b, [c]]) {
  102. return a.foo + b + c;
  103. }
  104. expect(testFunction({ foo: "20" }, [0, [1, 2, 3, 4]])).toBe("2001");
  105. });
  106. test("multiple destructuring parameters with rest parameters, array patterns with recursive patterns 2", () => {
  107. function testFunction({ bar, ...a }, [b, [c, ...{ length }]]) {
  108. return a.foo + b + c + length;
  109. }
  110. expect(testFunction({ foo: "20" }, [0, [1, 2, 3, 4]])).toBe("20013");
  111. });
  112. test("patterns with default", () => {
  113. function testFunction({ a = "27", b: bar }) {
  114. return a + bar;
  115. }
  116. expect(testFunction({ b: 42 })).toBe("2742");
  117. });
  118. });