destructuring.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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: "Basic parsing and early errors",
  9. body: function () {
  10. // No initializer
  11. assert.throws(function () { eval("var [];"); }, SyntaxError, "Destructured var array declaration must have at least one identifier reference", "Destructuring declarations must have an initializer");
  12. assert.throws(function () { eval("let [];"); }, SyntaxError, "Destructured let array declaration must have at least one identifier reference", "Destructuring declarations must have an initializer");
  13. assert.throws(function () { eval("const [];"); }, SyntaxError, "Destructured const array declaration must have at least one identifier reference", "Destructuring declarations must have an initializer");
  14. assert.throws(function () { eval("var [a];"); }, SyntaxError, "Destructured var array declaration must have an initializer", "Destructuring declarations must have an initializer");
  15. assert.throws(function () { eval("let [a];"); }, SyntaxError, "Destructured let array declaration must have an initializer", "Destructuring declarations must have an initializer");
  16. assert.throws(function () { eval("const [a];"); }, SyntaxError, "Destructured const array declaration must have an initializer", "Destructuring declarations must have an initializer");
  17. // No identifiers
  18. assert.doesNotThrow(function () { eval("var [] = [];"); }, "Destructured var array declaration with no identifiers does not throw");
  19. assert.doesNotThrow(function () { eval("let [] = [];"); }, "Destructured let array declaration with no identifiers does not throw");
  20. assert.doesNotThrow(function () { eval("const [] = [];"); }, "Destructured const array declaration with no identifiers does not throw");
  21. assert.doesNotThrow(function () { eval("[] = [];"); }, "Destructured array assignment with no identifiers does not throw");
  22. // Mismatched expression and initializer length
  23. assert.doesNotThrow(function () { eval("var [a] = [];"); }, "Destructured var array declaration with an empty initializer array does not throw");
  24. assert.doesNotThrow(function () { eval("let [a] = [];"); }, "Destructured let array declaration with an empty initializer array does not throw");
  25. assert.doesNotThrow(function () { eval("const [a] = [];"); }, "Destructured const array declaration with an empty initializer array does not throw");
  26. assert.doesNotThrow(function () { eval("var a; [a] = [];"); }, "Destructured var array assignment with an empty initializer array does not throw");
  27. assert.doesNotThrow(function () { eval("let a; [a] = [];"); }, "Destructured let array assignment with an empty initializer array does not throw");
  28. assert.doesNotThrow(function () { eval("var [a] = [1];"); }, "Destructured var array declaration with matching initializer array size does not throw");
  29. assert.doesNotThrow(function () { eval("let [a] = [2];"); }, "Destructured let array declaration with matching initializer array size does not throw");
  30. assert.doesNotThrow(function () { eval("const [a] = [1];"); }, "Destructured const array declaration with matching initializer array size does not throw");
  31. assert.doesNotThrow(function () { eval("var a; [a] = [1];"); }, "Destructured var array assignment with matching initializer array size does not throw");
  32. assert.doesNotThrow(function () { eval("let a; [a] = [2];"); }, "Destructured let array assignment with matching initializer array size does not throw");
  33. assert.doesNotThrow(function () { eval("var [a, b] = [1];"); }, "Destructured var array declaration with smaller initializer array size does not throw");
  34. assert.doesNotThrow(function () { eval("let [a, b] = [1];"); }, "Destructured let array declaration with smaller initializer array size does not throw");
  35. assert.doesNotThrow(function () { eval("const [a, b] = [1];"); }, "Destructured const array declaration with smaller initializer array size does not throw");
  36. assert.doesNotThrow(function () { eval("var a, b; [a, b] = [1];"); }, "Destructured var array assignment with smaller initializer array size does not throw");
  37. assert.doesNotThrow(function () { eval("let a, b; [a, b] = [1];"); }, "Destructured let array assignment with smaller initializer array size does not throw");
  38. assert.doesNotThrow(function () { eval("var [a] = [1, 2];"); }, "Destructured var array declaration with larger initializer array size does not throw");
  39. assert.doesNotThrow(function () { eval("let [a] = [1, 2];"); }, "Destructured let array declaration with larger initializer array size does not throw");
  40. assert.doesNotThrow(function () { eval("const [a] = [1, 2];"); }, "Destructured const array declaration with larger initializer array size does not throw");
  41. assert.doesNotThrow(function () { eval("var a; [a] = [1, 2];"); }, "Destructured var array assignment with larger initializer array size does not throw");
  42. assert.doesNotThrow(function () { eval("let a; [a] = [1, 2];"); }, "Destructured let array assignment with larger initializer array size does not throw");
  43. // Disallowed operators
  44. assert.throws(function () { eval("var [a--] = [];"); }, SyntaxError, "Destructured var array declaration with an operator throws", "Unexpected operator in destructuring expression");
  45. assert.throws(function () { eval("let [a--] = [];"); }, SyntaxError, "Destructured let array declaration with an operator throws", "Unexpected operator in destructuring expression");
  46. assert.throws(function () { eval("const [a--] = [];"); }, SyntaxError, "Destructured const array declaration with an operator throws", "Unexpected operator in destructuring expression");
  47. assert.throws(function () { eval("var [a + 1] = [];"); }, SyntaxError, "Destructured var array declaration with an operator throws", "Unexpected operator in destructuring expression");
  48. assert.throws(function () { eval("let [a + 1] = [];"); }, SyntaxError, "Destructured let array declaration with an operator throws", "Unexpected operator in destructuring expression");
  49. assert.throws(function () { eval("const [a + 1] = [];"); }, SyntaxError, "Destructured const array declaration with an operator throws", "Unexpected operator in destructuring expression");
  50. assert.throws(function () { eval("var [++a] = [];"); }, SyntaxError, "Destructured var array declaration with an operator throws", "Unexpected operator in destructuring expression");
  51. assert.throws(function () { eval("let [++a] = [];"); }, SyntaxError, "Destructured let array declaration with an operator throws", "Unexpected operator in destructuring expression");
  52. assert.throws(function () { eval("const [++a] = [];"); }, SyntaxError, "Destructured const array declaration with an operator throws", "Unexpected operator in destructuring expression");
  53. assert.throws(function () { eval("var a; [a--] = [];"); }, SyntaxError, "Destructured var array assignment with an operator throws", "Unexpected operator in destructuring expression");
  54. assert.throws(function () { eval("let a; [a--] = [];"); }, SyntaxError, "Destructured let array assignment with an operator throws", "Unexpected operator in destructuring expression");
  55. assert.throws(function () { eval("var a; [a + 1] = [];"); }, SyntaxError, "Destructured var array assignment with an operator throws", "Unexpected operator in destructuring expression");
  56. assert.throws(function () { eval("let a; [a + 1] = [];"); }, SyntaxError, "Destructured let array assignment with an operator throws", "Unexpected operator in destructuring expression");
  57. assert.throws(function () { eval("var a; [++a] = [];"); }, SyntaxError, "Destructured var array assignment with an operator throws", "Unexpected operator in destructuring expression");
  58. assert.throws(function () { eval("let a; [++a] = [];"); }, SyntaxError, "Destructured let array assignment with an operator throws", "Unexpected operator in destructuring expression");
  59. assert.doesNotThrow(function () { eval("var a = [1], i = 0; [a[i++]] = [];"); }, "Destructured var array assignment operators inside an identifier reference does not throw");
  60. assert.doesNotThrow(function () { eval("let a = [1], i = 0; [a[i++]] = [];"); }, "Destructured var array assignment operators inside an identifier reference does not throw");
  61. assert.doesNotThrow(function () { eval("var a = [1], i = 0; [a[(() => 1 + i)()]] = [];"); }, "Destructured var array assignment operators inside an identifier reference does not throw");
  62. assert.doesNotThrow(function () { eval("let a = [1], i = 0; [a[(() => 1 + i)()]] = [];"); }, "Destructured var array assignment operators inside an identifier reference does not throw");
  63. // Missing values
  64. assert.doesNotThrow(function () { eval("var [,] = [];"); }, "Destructured var array declaration with no identifiers and missing values does not throw");
  65. assert.doesNotThrow(function () { eval("let [,] = [];"); }, "Destructured let array declaration with no identifiers and missing values does not throw");
  66. assert.doesNotThrow(function () { eval("const [,] = [];"); }, "Destructured const array declaration with no identifiers and missing values does not throw");
  67. assert.doesNotThrow(function () { eval("[,] = [];"); }, "Destructured array assignment with no identifiers and missing values does not throw");
  68. assert.doesNotThrow(function () { eval("var [a,] = [];"); }, "Destructured var array declaration ending with a missing value does not throw");
  69. assert.doesNotThrow(function () { eval("let [a,] = [];"); }, "Destructured let array declaration ending with a missing value does not throw");
  70. assert.doesNotThrow(function () { eval("const [a,] = [];"); }, "Destructured const array declaration ending with a missing value does not throw");
  71. assert.doesNotThrow(function () { eval("var a; [a,] = [];"); }, "Destructured var array assignment ending with a missing value does not throw");
  72. assert.doesNotThrow(function () { eval("let a; [a,] = [];"); }, "Destructured let array assignment ending with a missing value does not throw");
  73. assert.doesNotThrow(function () { eval("var [a,,b] = [];"); }, "Destructured var array declaration with some missing values does not throw");
  74. assert.doesNotThrow(function () { eval("let [a,,b] = [];"); }, "Destructured let array declaration with some missing values does not throw");
  75. assert.doesNotThrow(function () { eval("const [a,,b] = [];"); }, "Destructured const array declaration with some missing values does not throw");
  76. assert.doesNotThrow(function () { eval("var a, b; [a,,b] = [];"); }, "Destructured var array assignment with some missing values does not throw");
  77. assert.doesNotThrow(function () { eval("let a, b; [a,,b] = [];"); }, "Destructured let array assignment with some missing values does not throw");
  78. assert.doesNotThrow(function () { eval("var [,,a] = [];"); }, "Destructured var array declaration beginning with missing values does not throw");
  79. assert.doesNotThrow(function () { eval("let [,,a] = [];"); }, "Destructured let array declaration beginning with missing values does not throw");
  80. assert.doesNotThrow(function () { eval("const [,,a] = [];"); }, "Destructured const array declaration beginning with missing values does not throw");
  81. assert.doesNotThrow(function () { eval("var a; [,,a] = [];"); }, "Destructured var array assignment beginning with missing values does not throw");
  82. assert.doesNotThrow(function () { eval("let a; [,,a] = [];"); }, "Destructured let array assignment beginning with missing values does not throw");
  83. assert.doesNotThrow(function () { eval("var [a] = [,,];"); }, "Destructured var array declaration with an initializer containing missing values does not throw");
  84. assert.doesNotThrow(function () { eval("let [a] = [,,];"); }, "Destructured let array declaration with an initializer containing missing values does not throw");
  85. assert.doesNotThrow(function () { eval("const [a] = [,,];"); }, "Destructured const array declaration with an initializer containing missing values does not throw");
  86. assert.doesNotThrow(function () { eval("var a; [a] = [,,];"); }, "Destructured var array assignment with an initializer containing missing values does not throw");
  87. assert.doesNotThrow(function () { eval("let a; [a] = [,,];"); }, "Destructured let array assignment with an initializer containing missing values does not throw");
  88. // Non-identifiers
  89. assert.throws(function () { eval("var [1] = [];"); }, SyntaxError, "Destructured var array declaration with no identifier references throws", "Destructuring expressions can only have identifier references");
  90. assert.throws(function () { eval("let [1] = [];"); }, SyntaxError, "Destructured let array declaration with no identifier references throws", "Destructuring expressions can only have identifier references");
  91. assert.throws(function () { eval("const [1] = [];"); }, SyntaxError, "Destructured const array declaration with no identifier references throws", "Destructuring expressions can only have identifier references");
  92. assert.throws(function () { eval("[1] = [];"); }, SyntaxError, "Destructured array assignment with no identifier references throws", "Destructuring expressions can only have identifier references");
  93. assert.throws(function () { eval("var [1, a] = [];"); }, SyntaxError, "Destructured var array declaration with valid and invalid identifier references throws", "Destructuring expressions can only have identifier references");
  94. assert.throws(function () { eval("let [1, a] = [];"); }, SyntaxError, "Destructured let array declaration with valid and invalid identifier references throws", "Destructuring expressions can only have identifier references");
  95. assert.throws(function () { eval("const [1, a] = [];"); }, SyntaxError, "Destructured const array declaration with valid and invalid identifier references throws", "Destructuring expressions can only have identifier references");
  96. assert.throws(function () { eval("var a; [1, a] = [];"); }, SyntaxError, "Destructured var array assignment with valid and invalid identifier references throws", "Destructuring expressions can only have identifier references");
  97. assert.throws(function () { eval("let a; [1, a] = [];"); }, SyntaxError, "Destructured let array assignment with valid and invalid identifier references throws", "Destructuring expressions can only have identifier references");
  98. // Rest parameter
  99. assert.doesNotThrow(function () { eval("var [...a] = [];"); }, "Destructured var array declaration with a single rest parameter does not throw");
  100. assert.doesNotThrow(function () { eval("let [...a] = [];"); }, "Destructured let array declaration with a single rest parameter does not throw");
  101. assert.doesNotThrow(function () { eval("const [...a] = [];"); }, "Destructured const array declaration with a single rest parameter does not throw");
  102. assert.doesNotThrow(function () { eval("var a; [...a] = [];"); }, "Destructured var array assignment with a single rest parameter does not throw");
  103. assert.doesNotThrow(function () { eval("let a; [...a] = [];"); }, "Destructured let array assignment with a single rest parameter does not throw");
  104. assert.throws(function () { eval("var [...a, ...b] = [];"); }, SyntaxError, "Destructured var array declaration with multiple rest parameters throws", "Destructuring rest variables must be in the last position of the expression");
  105. assert.throws(function () { eval("let [...a, ...b] = [];"); }, SyntaxError, "Destructured let array declaration with multiple rest parameters throws", "Destructuring rest variables must be in the last position of the expression");
  106. assert.throws(function () { eval("const [...a, ...b] = [];"); }, SyntaxError, "Destructured const array declaration with multiple rest parameters throws", "Destructuring rest variables must be in the last position of the expression");
  107. assert.throws(function () { eval("var a, b; [...a, ...b] = [];"); }, SyntaxError, "Destructured var array assignment with multiple rest parameters throws", "Destructuring rest variables must be in the last position of the expression");
  108. assert.throws(function () { eval("let a, b; [...a, ...b] = [];"); }, SyntaxError, "Destructured let array assignment with multiple rest parameters throws", "Destructuring rest variables must be in the last position of the expression");
  109. assert.throws(function () { eval("var [...a, b] = [];"); }, SyntaxError, "Destructured var array declaration with rest parameter in non-last position throws", "Destructuring rest variables must be in the last position of the expression");
  110. assert.throws(function () { eval("let [...a, b] = [];"); }, SyntaxError, "Destructured let array declaration with rest parameter in non-last position throws", "Destructuring rest variables must be in the last position of the expression");
  111. assert.throws(function () { eval("const [...a, b] = [];"); }, SyntaxError, "Destructured const array declaration with rest parameter in non-last position throws", "Destructuring rest variables must be in the last position of the expression");
  112. assert.throws(function () { eval("var a, b; [...a, b] = [];"); }, SyntaxError, "Destructured var array assignment with rest parameter in non-last position throws", "Destructuring rest variables must be in the last position of the expression");
  113. assert.throws(function () { eval("let a, b; [...a, b] = [];"); }, SyntaxError, "Destructured let array assignment with rest parameter in non-last position throws", "Destructuring rest variables must be in the last position of the expression");
  114. assert.throws(function () { eval("let [...a,] = [];"); }, SyntaxError, "Destructured array declaration has comma after rest parameter throws", "Destructuring rest variables must be in the last position of the expression");
  115. assert.throws(function () { eval("let a; [...a,] = [];"); }, SyntaxError, "Destructured array assignment has comma after rest parameter throws", "Destructuring rest variables must be in the last position of the expression");
  116. // Default values
  117. assert.doesNotThrow(function () { eval("var [a = 1] = [];"); }, "Destructured var array declaration with all default values does not throw");
  118. assert.doesNotThrow(function () { eval("let [a = 1] = [];"); }, "Destructured let array declaration with all default values does not throw");
  119. assert.doesNotThrow(function () { eval("const [a = 1] = [];"); }, "Destructured const array declaration with all default values does not throw");
  120. assert.doesNotThrow(function () { eval("var a; [a = 1] = [];"); }, "Destructured var array assignment with all default values does not throw");
  121. assert.doesNotThrow(function () { eval("let a; [a = 1] = [];"); }, "Destructured let array assignment with all default values does not throw");
  122. assert.doesNotThrow(function () { eval("var [a, b = 1] = [];"); }, "Destructured var array declaration with trailing default values does not throw");
  123. assert.doesNotThrow(function () { eval("let [a, b = 1] = [];"); }, "Destructured let array declaration with trailing default values does not throw");
  124. assert.doesNotThrow(function () { eval("const [a, b = 1] = [];"); }, "Destructured const array declaration with trailing default values does not throw");
  125. assert.doesNotThrow(function () { eval("var a, b; [a, b = 1] = [];"); }, "Destructured var array assignment with trailing default values does not throw");
  126. assert.doesNotThrow(function () { eval("let a, b; [a, b = 1] = [];"); }, "Destructured let array assignment with trailing default values does not throw");
  127. assert.doesNotThrow(function () { eval("var [a = 1, b] = [];"); }, "Destructured var array declaration with mixed default values does not throw");
  128. assert.doesNotThrow(function () { eval("let [a = 1, b] = [];"); }, "Destructured let array declaration with mixed default values does not throw");
  129. assert.doesNotThrow(function () { eval("const [a = 1, b] = [];"); }, "Destructured const array declaration with mixed default values does not throw");
  130. assert.doesNotThrow(function () { eval("var a, b; [a = 1, b] = [];"); }, "Destructured var array assignment with mixed default values does not throw");
  131. assert.doesNotThrow(function () { eval("let a, b; [a = 1, b] = [];"); }, "Destructured let array assignment with mixed default values does not throw");
  132. // Rest with default
  133. assert.throws(function () { eval("var [...a = 1] = [];"); }, SyntaxError, "Destructured var array declaration with a rest parameter with a default value throws", "Unexpected default initializer");
  134. assert.throws(function () { eval("let [...a = 1] = [];"); }, SyntaxError, "Destructured let array declaration with a rest parameter with a default value throws", "Unexpected default initializer");
  135. assert.throws(function () { eval("const [...a = 1] = [];"); }, SyntaxError, "Destructured const array declaration with a rest parameter with a default value throws", "Unexpected default initializer");
  136. assert.throws(function () { eval("var a; [...a = 1] = [];"); }, SyntaxError, "Destructured var array assignment with a rest parameter with a default value throws", "The rest parameter cannot have a default initializer.");
  137. assert.throws(function () { eval("let a; [...a = 1] = [];"); }, SyntaxError, "Destructured let array assignment with a rest parameter with a default value throws", "The rest parameter cannot have a default initializer.");
  138. // Nesting
  139. assert.doesNotThrow(function () { eval("var [[a]] = [[]];"); }, "Destructured var array declaration with nesting does not throw");
  140. assert.doesNotThrow(function () { eval("let [[a]] = [[]];"); }, "Destructured let array declaration with nesting does not throw");
  141. assert.doesNotThrow(function () { eval("const [[a]] = [[]];"); }, "Destructured const array declaration with nesting does not throw");
  142. assert.doesNotThrow(function () { eval("var a; [[a]] = [[]];"); }, "Destructured var array assignment with nesting does not throw");
  143. assert.doesNotThrow(function () { eval("let a; [[a]] = [[]];"); }, "Destructured let array assignment with nesting does not throw");
  144. assert.doesNotThrow(function () { eval("var [a, [b]] = [1, []];"); }, "Destructured var array declaration with some nesting does not throw");
  145. assert.doesNotThrow(function () { eval("let [a, [b]] = [1, []];"); }, "Destructured let array declaration with some nesting does not throw");
  146. assert.doesNotThrow(function () { eval("const [a, [b]] = [1, []];"); }, "Destructured const array declaration with some nesting does not throw");
  147. assert.doesNotThrow(function () { eval("var a, b; [a, [b]] = [1, []];"); }, "Destructured var array assignment with some nesting does not throw");
  148. assert.doesNotThrow(function () { eval("let a, b; [a, [b]] = [1, []];"); }, "Destructured let array assignment with some nesting does not throw");
  149. assert.throws(function () { eval("var [((a)] = [];"); }, SyntaxError, "Destructured var array declaration with a mismatched paren count throws", "Expected ')'");
  150. assert.throws(function () { eval("let [((a)] = [];"); }, SyntaxError, "Destructured let array declaration with a mismatched paren count throws", "Expected ')'");
  151. assert.throws(function () { eval("const [((a)] = [];"); }, SyntaxError, "Destructured const array declaration with a mismatched paren count throws", "Expected ')'");
  152. assert.throws(function () { eval("var a; [((a)] = [];"); }, SyntaxError, "Destructured var array assignment with a mismatched paren count throws", "Expected ')'");
  153. assert.throws(function () { eval("let a; [((a)] = [];"); }, SyntaxError, "Destructured let array assignment with a mismatched paren count throws", "Expected ')'");
  154. assert.throws(function () { eval("var [a)] = [];"); }, SyntaxError, "Destructured var array declaration with a mismatched paren count throws", "Expected ')'");
  155. assert.throws(function () { eval("let [a)] = [];"); }, SyntaxError, "Destructured let array declaration with a mismatched paren count throws", "Expected ')'");
  156. assert.throws(function () { eval("const [a)] = [];"); }, SyntaxError, "Destructured const array declaration with a mismatched paren count throws", "Expected ')'");
  157. assert.throws(function () { eval("var a; [a)] = [];"); }, SyntaxError, "Destructured var array assignment with a mismatched paren count throws", "Expected ']'");
  158. assert.throws(function () { eval("let a; [a)] = [];"); }, SyntaxError, "Destructured let array assignment with a mismatched paren count throws", "Expected ']'");
  159. assert.doesNotThrow(function () { eval("var [((((a)))), b] = [];"); }, "Destructured var array declaration with some nested parens does not throw");
  160. assert.doesNotThrow(function () { eval("let [((((a)))), b] = [];"); }, "Destructured let array declaration with some nested parens does not throw");
  161. assert.doesNotThrow(function () { eval("const [((((a)))), b] = [];"); }, "Destructured const array declaration with some nested parens does not throw");
  162. assert.doesNotThrow(function () { eval("var a, b; [((((a)))), b] = [];"); }, "Destructured var array assignment with some nested parens does not throw");
  163. assert.doesNotThrow(function () { eval("let a, b; [((((a)))), b] = [];"); }, "Destructured let array assignment with some nested parens does not throw");
  164. assert.doesNotThrow(function () { eval("var [[[...a]]] = [[[]]];"); }, "Destructured var array declaration with nested rest parameter does not throw");
  165. assert.doesNotThrow(function () { eval("let [[[...a]]] = [[[]]];"); }, "Destructured let array declaration with nested rest parameter does not throw");
  166. assert.doesNotThrow(function () { eval("const [[[...a]]] = [[[]]];"); }, "Destructured const array declaration with nested rest parameter does not throw");
  167. assert.doesNotThrow(function () { eval("var a; [[[...a]]] = [[[]]];"); }, "Destructured var array assignment with nested rest parameter does not throw");
  168. assert.doesNotThrow(function () { eval("let a; [[[...a]]] = [[[]]];"); }, "Destructured let array assignment with nested rest parameter does not throw");
  169. assert.doesNotThrow(function () { eval("var [[...a], ...b] = [[],];"); }, "Destructured var array declaration with valid nested rest parameters does not throw");
  170. assert.doesNotThrow(function () { eval("let [[...a], ...b] = [[],];"); }, "Destructured let array declaration with valid nested rest parameters does not throw");
  171. assert.doesNotThrow(function () { eval("const [[...a], ...b] = [[],];"); }, "Destructured const array declaration with valid nested rest parameters does not throw");
  172. assert.doesNotThrow(function () { eval("var a, b; [[...a], ...b] = [[],];"); }, "Destructured var array assignment with valid nested rest parameters does not throw");
  173. assert.doesNotThrow(function () { eval("let a, b; [[...a], ...b] = [[],];"); }, "Destructured let array assignment with valid nested rest parameters does not throw");
  174. assert.doesNotThrow(function () { eval("var [[(a)], ((((((([b])))))))] = [[],[]];"); }, "Destructured var array declaration with valid mixed paren and array nesting does not throw");
  175. assert.doesNotThrow(function () { eval("let [[(a)], ((((((([b])))))))] = [[],[]];"); }, "Destructured let array declaration with valid mixed paren and array nesting does not throw");
  176. assert.doesNotThrow(function () { eval("const [[(a)], ((((((([b])))))))] = [[],[]];"); }, "Destructured const array declaration with valid mixed paren and array nesting does not throw");
  177. assert.doesNotThrow(function () { eval("var a, b; [[(a)], ((((((([b])))))))] = [[],[]];"); }, "Destructured var array assignment with valid mixed paren and array nesting does not throw");
  178. assert.doesNotThrow(function () { eval("let a, b; [[(a)], ((((((([b])))))))] = [[],[]];"); }, "Destructured let array assignment with valid mixed paren and array nesting does not throw");
  179. // Redeclarations
  180. assert.doesNotThrow(function () { eval("var [a, a] = [];"); }, "Destructured var array declaration with a repeated identifier reference does not throw");
  181. assert.throws(function () { eval("let [a, a] = [];"); }, SyntaxError, "Destructured let array declaration with a repeated identifier reference throws", "Let/Const redeclaration");
  182. assert.throws(function () { eval("const [a, a] = [];"); }, SyntaxError, "Destructured const array declaration with a repeated identifier reference throws", "Let/Const redeclaration");
  183. assert.doesNotThrow(function () { eval("var a; [a, a] = [];"); }, "Destructured var array assignment with a repeated identifier reference does not throw");
  184. assert.doesNotThrow(function () { eval("let a; [a, a] = [];"); }, "Destructured let array assignment with a repeated identifier reference does not throw");
  185. // Identifier property references
  186. assert.doesNotThrow(function () { eval("var a = {}; [a.x] = [];"); }, "Destructured var array assignment with an identifier property reference does not throw");
  187. assert.doesNotThrow(function () { eval("let a = {}; [a.x] = [];"); }, "Destructured let array assignment with an identifier property reference does not throw");
  188. assert.doesNotThrow(function () { eval("var a = {}; [a[\"x\"]] = [];"); }, "Destructured var array assignment with an identifier property reference does not throw");
  189. assert.doesNotThrow(function () { eval("let a = {}; [a[\"x\"]] = [];"); }, "Destructured let array assignment with an identifier property reference does not throw");
  190. // Call expression property references
  191. assert.throws(function () { eval("function foo() { return {}; }; var [foo()] = [];"); }, SyntaxError, "Destructured var array declaration with a call expression throws", "Syntax error");
  192. assert.throws(function () { eval("function foo() { return {}; }; let [foo()] = [];"); }, SyntaxError, "Destructured let array declaration with a call expression throws", "Let/Const redeclaration");
  193. assert.throws(function () { eval("function foo() { return {}; }; const [foo()] = [];"); }, SyntaxError, "Destructured const array declaration with a call expression throws", "Let/Const redeclaration");
  194. assert.throws(function () { eval("function foo() { return {}; }; [foo()] = [];"); }, SyntaxError, "Destructured array assignment with a call expression throws", "Invalid destructuring assignment target");
  195. assert.throws(function () { eval("function foo() { return {}; }; var [foo().x] = [];"); }, SyntaxError, "Destructured var array declaration with a call expression property reference throws", "Syntax error");
  196. assert.throws(function () { eval("function foo() { return {}; }; let [foo().x] = [];"); }, SyntaxError, "Destructured let array declaration with a call expression property reference throws", "Let/Const redeclaration");
  197. assert.throws(function () { eval("function foo() { return {}; }; const [foo().x] = [];"); }, SyntaxError, "Destructured const array declaration with a call expression property reference throws", "Let/Const redeclaration");
  198. assert.doesNotThrow(function () { eval("function foo() { return {}; }; [foo().x] = [];"); }, "Destructured array assignment with super a property reference does not throw");
  199. assert.doesNotThrow(function () { eval("function foo() { return {}; }; [foo()[\"x\"]] = [];"); }, "Destructured array assignment with a call expression property reference does not throw");
  200. // Super property references
  201. assert.throws(function () { eval("class foo { method() { var [super()] = []; } }"); }, SyntaxError, "Destructured var array declaration with a super call throws", "The use of a keyword for an identifier is invalid");
  202. assert.throws(function () { eval("class foo { method() { let [super()] = []; } }"); }, SyntaxError, "Destructured let array declaration with a super call throws", "The use of a keyword for an identifier is invalid");
  203. assert.throws(function () { eval("class foo { method() { const [super()] = []; } }"); }, SyntaxError, "Destructured const array declaration with a super call throws", "The use of a keyword for an identifier is invalid");
  204. assert.throws(function () { eval("class foo { method() { [super()] = []; } }"); }, SyntaxError, "Destructured array assignment with a super call throws", "Invalid use of the 'super' keyword");
  205. assert.throws(function () { eval("class foo { method() { var [super.x] = []; } }"); }, SyntaxError, "Destructured var array declaration with a super property reference throws", "The use of a keyword for an identifier is invalid");
  206. assert.throws(function () { eval("class foo { method() { let [super.x] = []; } }"); }, SyntaxError, "Destructured let array declaration with a super property reference does not throw", "The use of a keyword for an identifier is invalid");
  207. assert.throws(function () { eval("class foo { method() { const [super.x] = []; } }"); }, SyntaxError, "Destructured const array declaration with a super property reference does not throw", "The use of a keyword for an identifier is invalid");
  208. assert.doesNotThrow(function () { eval("class foo { method() { [super.x] = []; } }"); }, "Destructured var array assignment with super a property reference does not throw");
  209. assert.doesNotThrow(function () { eval("class foo { method() { [super[\"x\"]] = []; } }"); }, "Destructured array assignment with a super property reference does not throw");
  210. // Destructured array expressions used in other constructs
  211. assert.doesNotThrow(function () { eval("var a; `${[a] = [1]}`"); }, "Destructured assignment does not throw inside a string template");
  212. // OS 597319: Parens before a default value should not throw
  213. assert.doesNotThrow(function () { eval("[((((vrh190 )))) = 5184] = []"); }, "Destructured array assignment with parens before a default expression should not throw");
  214. }
  215. },
  216. {
  217. name: "Array destructuring basic functionality",
  218. body : function () {
  219. // Single assignments & undefined
  220. {
  221. let a1; [a1] = [1];
  222. let [a2] = [1];
  223. assert.areEqual(a1, a2, "Destructured array declaration and assignment assigning single values match");
  224. assert.areEqual(a1, 1, "Destructured array assignment assigns single value correctly");
  225. assert.areEqual(a2, 1, "Destructured array declaration assigns single value correctly");
  226. }
  227. {
  228. let a1; [a1] = [];
  229. let [a2] = [];
  230. assert.areEqual(a1, a2, "Destructured array declaration and assignment assigning an empty array match");
  231. assert.areEqual(a1, undefined, "Destructured array assignment assigning an empty array results in undefined");
  232. assert.areEqual(a2, undefined, "Destructured array declaration assigning an empty array results in undefined");
  233. let a3; [a3] = [,1];
  234. let [a4] = [,1];
  235. assert.areEqual(a3, a4, "Destructured array declaration and assignment assigning an array with missing values match");
  236. assert.areEqual(a3, undefined, "Destructured array assignment assigning an array with missing values results in undefined");
  237. assert.areEqual(a4, undefined, "Destructured array declaration assigning an array with missing values in undefined");
  238. }
  239. // Multiple assignments
  240. {
  241. let a1, b1, c1, d1, e1;
  242. [a1, b1, c1, d1, e1] = [1, 2, 3, 4, 5];
  243. let [a2, b2, c2, d2, e2] = [1, 2, 3, 4, 5];
  244. assert.areEqual([a1, b1, c1, d1, e1], [a2, b2, c2, d2, e2], "Destructured array assignment and declaration assigning multiple values match");
  245. assert.areEqual([1, 2, 3, 4, 5], [a1, b1, c1, d1, e1], "Destructured array assignment assigns multiple values correctly");
  246. assert.areEqual([1, 2, 3, 4, 5], [a2, b2, c2, d2, e2], "Destructured array declaration assigns multiple values correctly");
  247. }
  248. {
  249. let a1, b1, c1, d1, e1;
  250. [a1, b1, c1, d1, e1] = [1, 2, 3];
  251. let [a2, b2, c2, d2, e2] = [1, 2, 3];
  252. assert.areEqual([a1, b1, c1, d1, e1], [a2, b2, c2, d2, e2], "Destructured array assignment and declaration assigning a smaller array match");
  253. assert.areEqual([1, 2, 3, undefined, undefined], [a1, b1, c1, d1, e1], "Destructured array assignment assigns a smaller array correctly");
  254. assert.areEqual([1, 2, 3, undefined, undefined], [a2, b2, c2, d2, e2], "Destructured array declaration assigns a smaller array correctly");
  255. }
  256. {
  257. let a1, b1, c1, d1, e1;
  258. [a1, b1, c1, d1, e1] = [1, , 3, , 5];
  259. let [a2, b2, c2, d2, e2] = [1, , 3, , 5];
  260. assert.areEqual([a1, b1, c1, d1, e1], [a2, b2, c2, d2, e2], "Destructured array assignment and declaration assigning an array with some missing values match");
  261. assert.areEqual([1, undefined, 3, undefined, 5], [a1, b1, c1, d1, e1], "Destructured array assignment assigns an array with some missing values correctly");
  262. assert.areEqual([1, undefined, 3, undefined, 5], [a2, b2, c2, d2, e2], "Destructured array declaration assigns an array with some missing values correctly");
  263. }
  264. // Rest
  265. {
  266. let rest1;
  267. [...rest1] = [1, 2, 3, 4, 5];
  268. let [...rest2] = [1, 2, 3, 4, 5];
  269. assert.areEqual(rest1, rest2, "Destructured array assignment and declaration using only a rest parameter match");
  270. assert.areEqual([1, 2, 3, 4, 5], rest1, "Destructured array assignment uses only a rest parameter correctly");
  271. assert.areEqual([1, 2, 3, 4, 5], rest2, "Destructured array declaration uses only a rest parameter correctly");
  272. }
  273. {
  274. let a1, b1, c1, d1, e1;
  275. [a1, b1, c1, ...rest1] = [1, 2, 3, 4, 5];
  276. let [a2, b2, c2, ...rest2] = [1, 2, 3, 4, 5];
  277. assert.areEqual([a1, b1, c1, rest1], [a2, b2, c2, rest2], "Destructured array assignment and declaration using a rest parameter match");
  278. assert.areEqual([1, 2, 3, [4, 5]], [a1, b1, c1, rest1], "Destructured array assignment uses a rest parameter correctly");
  279. assert.areEqual([1, 2, 3, [4, 5]], [a2, b2, c2, rest2], "Destructured array declaration uses a rest parameter correctly");
  280. let a3, b3, c3, d3, e3;
  281. [a3, b3, c3, ...rest3] = [1, 2, 3];
  282. let [a4, b4, c4, ...rest4] = [1, 2, 3];
  283. assert.areEqual([a3, b3, c3, rest3], [a4, b4, c4, rest4], "Destructured array assignment and declaration using a rest parameter and a smaller array match");
  284. assert.areEqual([1, 2, 3, []], [a3, b3, c3, rest3], "Destructured array assignment uses a rest parameter and a smaller array correctly");
  285. assert.areEqual([1, 2, 3, []], [a4, b4, c4, rest4], "Destructured array declaration uses a rest parameter and a smaller array correctly");
  286. }
  287. {
  288. let a1, b1;
  289. [[...a1], ...b1] = [[1, 2, 3, 4], 5, 6, 7, 8];
  290. let [[...a2], ...b2] = [[1, 2, 3, 4], 5, 6, 7, 8];
  291. assert.areEqual([a1, b1], [a2, b2], "Destructured array assignment and declaration with nested rest parameters match");
  292. assert.areEqual([a1, b1], [[1, 2, 3, 4], [5, 6, 7, 8]], "Destructured array assignment uses nested rest parameters correctly");
  293. assert.areEqual([a2, b2], [[1, 2, 3, 4], [5, 6, 7, 8]], "Destructured array declaration uses nested rest parameters correctly");
  294. }
  295. // Default values
  296. {
  297. let a1, b1, c1, d1, e1;
  298. [a1 = 1, b1 = 2, c1 = 3, d1 = 4, e1 = 5] = [];
  299. let [a2 = 1, b2 = 2, c2 = 3, d2 = 4, e2 = 5] = [];
  300. assert.areEqual([a1, b1, c1, d1, e1], [a2, b2, c2, d2, e2], "Destructured array assignment and declaration with default values assigning an empty array match");
  301. assert.areEqual([1, 2, 3, 4, 5], [a1, b1, c1, d1, e1], "Destructured array assignment assigns an array with default values assigning an empty array correctly");
  302. assert.areEqual([1, 2, 3, 4, 5], [a2, b2, c2, d2, e2], "Destructured array declaration assigns an array with default values assigning an empty array correctly");
  303. }
  304. {
  305. let a1, b1, c1, d1, e1;
  306. [a1 = 1, b1 = 2, c1 = 3, d1 = 4, e1 = 5] = [5, , 3, 2, ];
  307. let [a2 = 1, b2 = 2, c2 = 3, d2 = 4, e2 = 5] = [5, , 3, 2, ];
  308. assert.areEqual([a1, b1, c1, d1, e1], [a2, b2, c2, d2, e2], "Destructured array assignment and declaration assigning an array with some missing values match");
  309. assert.areEqual([5, 2, 3, 2, 5], [a1, b1, c1, d1, e1], "Destructured array assignment assigns an array with some missing values correctly");
  310. assert.areEqual([5, 2, 3, 2, 5], [a2, b2, c2, d2, e2], "Destructured array declaration assigns an array with some missing values correctly");
  311. }
  312. // Identifier references
  313. {
  314. let a = {};
  315. [a.x] = [10];
  316. assert.areEqual(10, a.x, "Destructured array with an object property assignment works correctly");
  317. [a["x"]] = [20];
  318. assert.areEqual(20, a["x"], "Destructured array with an object index assignment works correctly");
  319. var obj = { x: 10 };
  320. function foo() { return obj };
  321. [foo().x] = [20];
  322. assert.areEqual(20, foo().x, "Destructured array with a call result property assignment works correctly");
  323. [foo()["x"]] = [30];
  324. assert.areEqual(30, foo()["x"], "Destructured array with a call result index assignment works correctly");
  325. [...foo().x] = [20];
  326. assert.areEqual([20], foo().x, "Destructured array with a call result rest element works correctly");
  327. [...foo()["x"]] = [30];
  328. assert.areEqual([30], foo()["x"], "Destructured array with a call result rest element works correctly");
  329. class base {
  330. methodProp() { return {}; }
  331. methodIndex() { return {}; }
  332. methodRestProp() { return {}; }
  333. methodRestIndex() { return {}; }
  334. get x() { return this._x; }
  335. set x(v) { this._x = v; }
  336. };
  337. class extended extends base {
  338. methodProp() { return [super.x] = [10, 20, 30]; }
  339. methodIndex() { return [super["x"]] = [40, 50, 60]; }
  340. methodRestProp() { return [...super.x] = [10, 20, 30]; }
  341. methodRestIndex() { return [...super.x] = [40, 50, 60]; }
  342. getValue() { return super.x;}
  343. };
  344. let c = new extended();
  345. assert.areEqual(undefined, c.getValue(), "Super property before destructured assignment is undefined");
  346. c.methodProp();
  347. assert.areEqual(10, c.getValue(), "Super property after destructured super property assignment");
  348. c.methodIndex();
  349. assert.areEqual(40, c.getValue(), "Super property after destructured super index assignment");
  350. c.methodRestProp();
  351. assert.areEqual([10, 20, 30], c.getValue(), "Super property after destructured super property assignment");
  352. c.methodRestIndex();
  353. assert.areEqual([40, 50, 60], c.getValue(), "Super property after destructured super index assignment");
  354. }
  355. // Nesting
  356. {
  357. let a1; [[a1]] = [[1]];
  358. let [[a2]] = [[1]];
  359. assert.areEqual(a1, a2, "Destructured array declaration and assignment assigning single values match");
  360. assert.areEqual(a1, 1, "Destructured array assignment assigns single value correctly");
  361. assert.areEqual(a2, 1, "Destructured array declaration assigns single value correctly");
  362. }
  363. {
  364. let a1, b1, c1, d1, e1;
  365. [[a1, b1], c1, [d1, [e1]]] = [[1, 2], 3, [4, [5]]];
  366. let [[a2, b2], c2, [d2, [e2]]] = [[1, 2], 3, [4, [5]]];
  367. assert.areEqual([a1, b1, c1, d1, e1], [a2, b2, c2, d2, e2], "Destructured array assignment and declaration assigning multiple values match");
  368. assert.areEqual([1, 2, 3, 4, 5], [a1, b1, c1, d1, e1], "Destructured array assignment assigns multiple values correctly");
  369. assert.areEqual([1, 2, 3, 4, 5], [a2, b2, c2, d2, e2], "Destructured array declaration assigns multiple values correctly");
  370. }
  371. {
  372. let a1; [[a1, b1] = [1, 2]] = [];
  373. let [[a2, b2] = [1, 2]] = [];
  374. assert.areEqual([a1, b1], [a2, b2], "Destructured array declaration and assignment using nested values match");
  375. assert.areEqual([1, 2], [a1, b1], "Destructured array assignment assigns nested values correctly");
  376. assert.areEqual([1, 2], [a2, b2], "Destructured array declaration assigns nested values correctly");
  377. }
  378. {
  379. let a1; [[[a1] = [1], [[b1]] = [[2]]] = [, undefined]] = [undefined, ];
  380. let [[[a2] = [1], [[b2]] = [[2]]] = [, undefined]] = [undefined, ];
  381. assert.areEqual([a1, b1], [a2, b2], "Destructured array declaration and assignment using nested default values match");
  382. assert.areEqual([1, 2], [a1, b1], "Destructured array assignment assigns nested default values correctly");
  383. assert.areEqual([1, 2], [a2, b2], "Destructured array declaration assigns nested default values correctly");
  384. }
  385. // Other iterators
  386. {
  387. let a1; [a1, b1, c1, d1, ...rest1] = "testing";
  388. let [a2, b2, c2, d2, ...rest2] = "testing";
  389. assert.areEqual([a1, b1, c1, d1, rest1], [a2, b2, c2, d2, rest2], "Destructured array declaration and assignment using nested values match");
  390. assert.areEqual(["t", "e", "s", "t", ["i", "n", "g"]], [a1, b1, c1, d1, rest1], "Destructured array assignment assigns nested values correctly");
  391. assert.areEqual(["t", "e", "s", "t", ["i", "n", "g"]], [a2, b2, c2, d2, rest2], "Destructured array declaration assigns nested values correctly");
  392. }
  393. {
  394. let map = new Map();
  395. map.set(1, 6);
  396. map.set(2, 7);
  397. map.set(3, 8);
  398. map.set(4, 9);
  399. map.set(5, 10);
  400. let a1; [a1, b1, c1, d1, ...rest1] = map.entries();
  401. let [a2, b2, c2, d2, ...rest2] = map.entries();
  402. assert.areEqual([a1, b1, c1, d1, rest1], [a2, b2, c2, d2, rest2], "Destructured array declaration and assignment using nested values match");
  403. assert.areEqual([[1, 6], [2, 7], [3, 8], [4, 9], [[5, 10]]], [a1, b1, c1, d1, rest1], "Destructured array assignment assigns nested values correctly");
  404. assert.areEqual([[1, 6], [2, 7], [3, 8], [4, 9], [[5, 10]]], [a2, b2, c2, d2, rest2], "Destructured array declaration assigns nested values correctly");
  405. }
  406. {
  407. let getIteratorCalledCount = 0;
  408. let nextCalledCount = 0;
  409. let doneCalledCount = 0;
  410. let valueCalledCount = 0;
  411. let simpleIterator = {
  412. [Symbol.iterator]: function () {
  413. ++getIteratorCalledCount;
  414. return {
  415. i: 0,
  416. next: function () {
  417. ++nextCalledCount;
  418. var that = this;
  419. return {
  420. get done() {
  421. ++doneCalledCount;
  422. return that.i == 1;
  423. },
  424. get value() {
  425. ++valueCalledCount;
  426. return that.i++;
  427. }
  428. };
  429. }
  430. };
  431. }
  432. };
  433. let [a, b, c] = simpleIterator;
  434. assert.areEqual(1, getIteratorCalledCount, "GetIterator is called once per iterator");
  435. assert.areEqual(3, nextCalledCount, "'.next()' is called once per destructuring reference");
  436. assert.areEqual(3, doneCalledCount, "'done' is read once per destructuring reference");
  437. assert.areEqual(1, valueCalledCount, "'value' is read once per .next() call until done is true");
  438. [getIteratorCalledCount, nextCalledCount, doneCalledCount, valueCalledCount] = [0, 0, 0, 0];
  439. let [...rest] = simpleIterator;
  440. assert.areEqual(1, getIteratorCalledCount, "GetIterator is called once per iterator");
  441. assert.areEqual(2, nextCalledCount, "'.next()' is called until done is true when filling a rest element");
  442. assert.areEqual(2, doneCalledCount, "'done' is read until it is true");
  443. assert.areEqual(1, valueCalledCount, "'value' is read once for each .next() call until done is true");
  444. [getIteratorCalledCount, nextCalledCount, doneCalledCount, valueCalledCount] = [0, 0, 0, 0];
  445. [a, b, c, ...rest] = simpleIterator;
  446. assert.areEqual(1, getIteratorCalledCount, "GetIterator is called once per iterator");
  447. assert.areEqual(4, nextCalledCount, "'.next()' is called once per destructuring reference");
  448. assert.areEqual(4, doneCalledCount, "'done' is read once per destructuring reference");
  449. assert.areEqual(1, valueCalledCount, "'value' is read once per .next() call until done is true");
  450. }
  451. // Runtime type errors
  452. assert.throws(function () { eval("let [a] = 1;") }, TypeError, "Non-object used as an iterator in a declaration throws", "Object doesn't support property or method 'Symbol.iterator'");
  453. assert.throws(function () { eval("let a; [a] = 1;") }, TypeError, "Non-object used as an iterator an assignment throws" , "Object doesn't support property or method 'Symbol.iterator'");
  454. assert.throws(function () { eval("let [[a]] = [];") }, TypeError, "Nested non-object used as an iterator in a declaration throws", "Unable to get property 'Symbol.iterator' of undefined or null reference");
  455. assert.throws(function () { eval("let a; [[a]] = [];") }, TypeError, "Nested non-object used as an iterator an assignment throws", "Unable to get property 'Symbol.iterator' of undefined or null reference");
  456. // Array destructuring in various contexts
  457. {
  458. let a, b, c;
  459. function foo(x = [a, b = 2, ...c] = [1,,3,4,5,6,7]) {
  460. assert.areEqual([1, 2, [3,4,5,6,7]], [a, b, c], "Destructuring array assignment works correctly inside a default parameter expression");
  461. assert.areEqual([1,,3,4,5,6,7], x, "Destructuring array assignment evaluates to RHS in a default parameter expression");
  462. }
  463. foo();
  464. `${[a = 5, b, ...c] = [, 1, 3, 5, 7, 9]}`;
  465. assert.areEqual([5, 1, [3, 5, 7, 9]], [a, b, c], "Destructuring array assignment inside a string template works correctly");
  466. (() => [a,, b, c] = [1, 2, 3])();
  467. assert.areEqual([1, 3, undefined], [a, b, c], "Destructuring array assignment inside a lambda expression works correctly");
  468. }
  469. // nested destructuring
  470. {
  471. let [[a]=[1]] = [[2]];
  472. assert.areEqual(a, 2, "Nested destructuring - value is present");
  473. [[a]=[1]] = [[]];
  474. assert.areEqual(a, undefined, "Nested destructuring - value is present but undefined");
  475. [[a]=[1]] = [];
  476. assert.areEqual(a, 1, "Nested destructuring - value is not present - use default");
  477. [[a]=1] = [[]];
  478. assert.areEqual(a, undefined, "Nested destructuring - value is present - default is incorrect - does not have @@iterator");
  479. }
  480. // Bug OSG : 4533495
  481. {
  482. function foo() {
  483. for(var [i, j, k] in { qux: 1 }) {
  484. return i === "q" && j === "u" && k === "x";
  485. }
  486. }
  487. assert.isTrue(foo(), "Array destructuring pattern on for..in is initialized correctly");
  488. }
  489. {
  490. let obj1 = {};
  491. obj1[Symbol.iterator] = function () {
  492. return {
  493. next: function() {
  494. assert.fail("next function should not be called");
  495. }
  496. };
  497. };
  498. // Empty slot should not call next on the iterator.
  499. var [] = obj1;
  500. }
  501. {
  502. let obj1 = {};
  503. obj1[Symbol.iterator] = function () {
  504. return {
  505. next: function() {
  506. this.counter++;
  507. if (this.counter > 1)
  508. {
  509. assert.fail("next function should not be called for the second time");
  510. }
  511. return {value : undefined, done: false};
  512. },
  513. counter : 0
  514. };
  515. };
  516. // Second empty slot should not call next on the iterator.
  517. var [,] = obj1;
  518. }
  519. }
  520. }
  521. ];
  522. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });