destructuring_bugs.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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: "Destructuring bug fixes validation",
  9. body: function () {
  10. assert.throws(function () { eval("function f1() { var a = 10; [a+2] = []; }; f1();"); }, SyntaxError, "var empty object declaration pattern without an initializer is not valid syntax", "Unexpected operator in destructuring expression");
  11. assert.throws(function () { eval("function f2() { var a = 10; ({x:a+2} = {x:2}); }; f2();"); }, SyntaxError, "var empty object declaration pattern without an initializer is not valid syntax", "Unexpected operator in destructuring expression");
  12. assert.throws(function () { eval("function f3() { var a = 10; for ([a+2] in []) { } }; f3();"); }, SyntaxError, "var empty object declaration pattern without an initializer is not valid syntax", "Unexpected operator in destructuring expression");
  13. assert.doesNotThrow(function () { eval("(function () { var x; for ({x:x}.x of [1,2]) {}; })();"); }, "for..of initializer start with left curly but not a pattern is valid syntax");
  14. assert.throws(function () { eval("(function () { 'use strict'; [x] = [1]; let x = 2; })();"); }, ReferenceError, "A variable, defined in pattern, is used before its declaration in not valid syntax", "Use before declaration");
  15. assert.throws(function () { eval("(function () { 'use strict'; let x1 = 1; ({x:x1, y:{y1:y1}} = {x:11, y:{y1:22}}); let y1 = 2; })();"); }, ReferenceError, "A variable, defined in nested pattern, is used before its declaration is not valid syntax", "Use before declaration");
  16. assert.throws(function () { eval("(function () { 'use strict'; [eval] = []; })();"); }, SyntaxError, "variable name 'eval' defined in array pattern is not valid in strict mode", "Invalid usage of 'eval' in strict mode");
  17. assert.throws(function () { eval("let a = [a] = [10]"); }, ReferenceError, "A let variable is used in the array pattern in the same statement where it is declared", "Use before declaration");
  18. assert.throws(function () { eval("let a = {a:a} = {}"); }, ReferenceError, "A let variable is used in object pattern in the same statement where it is declared", "Use before declaration");
  19. assert.throws(function () { eval("var a = 1; (delete [a] = [2]);"); }, ReferenceError, "Array literal in unary expression should not be converted to array pattern", "Invalid left-hand side in assignment");
  20. assert.throws(function () { eval("var x, b; for ([x] = [((b) = 1)] of ' ') { }"); }, ReferenceError, "Initializer in for..in is not valid but no assert should be thrown", "Invalid left-hand side in assignment");
  21. assert.throws(function () { eval("for (let []; ;) { }"); }, SyntaxError, "Native for loop's head has one destructuring pattern without initializer", "Destructuring declarations must have an initializer");
  22. assert.throws(function () { eval("for (let a = 1, []; ;) { }"); }, SyntaxError, "Native for loop's head has second param as destructuring pattern without initializer", "Destructuring declarations must have an initializer");
  23. assert.throws(function () { eval("for (let [] = [], a = 1, {}; ;) { }"); }, SyntaxError, "Native for loop's head has third param as object destructuring pattern without initializer", "Destructuring declarations must have an initializer");
  24. assert.throws(function () { eval("for (let [[a] = []]; ;) { }"); }, SyntaxError, "Native for loop's head as destructuring pattern without initializer", "Destructuring declarations must have an initializer");
  25. assert.doesNotThrow(function () { eval("for ([]; ;) { break; }"); }, "Native for loop's head is an expression without initializer is valid syntax");
  26. assert.doesNotThrow(function () { eval("try { y; } catch({}) { }"); }, "Catching exception to empty pattern should not assert and is a valid syntax");
  27. assert.doesNotThrow(function () { eval("for({} = function (...a) { } in '' ) { }"); }, "Having a function with rest parameter as initializer should not assert and is a valid syntax");
  28. assert.doesNotThrow(function () { eval("for([] = ((...a) => {}) in '' ) { }"); }, "Having a lambda function with rest parameter as initializer should not assert and is a valid syntax");
  29. assert.doesNotThrow(function () { eval("[[[] = [function () { }] ] = []]"); }, "Nested array has array pattern which has function expression is a valid syntax");
  30. assert.doesNotThrow(function () { eval("var a = ({x = 1}) => x;"); }, "Lambda has Object destructuring as parameter which has initializer on shorthand is a valid syntax");
  31. }
  32. },
  33. {
  34. name: "Destructuring bug fix - function expression under destructuring pattern does not crash",
  35. body: function () {
  36. var a = [];
  37. var b = 2;
  38. function f() {
  39. [ a [ function () { }, b ] ] = [2] ;
  40. }
  41. f();
  42. }
  43. },
  44. {
  45. name: "Destructuring bug fix - rest operator in for loop",
  46. body: function () {
  47. assert.throws(function () { eval("for (var {a: ...a1} = {}; ; ) { } "); }, SyntaxError, "Native for loop - usage of '...' in object destructuring pattern in not valid", "Unexpected ... operator");
  48. assert.throws(function () { eval("for (var {a: ...[]} = {}; ; ) { } "); }, SyntaxError, "Native for loop - usage of '...' before an array in object destructuring pattern in not valid", "Unexpected ... operator");
  49. assert.throws(function () { eval("for (var {a: ...[]} of '' ) { } "); }, SyntaxError, "for.of loop - usage of '...' before an array in object destructuring pattern in not valid", "Unexpected ... operator");
  50. }
  51. },
  52. {
  53. name: "Destructuring bug fix - call expression instead of reference node",
  54. body: function () {
  55. assert.throws(function () { eval("for (var a of {b: foo()} = {}) { }"); }, SyntaxError, "for.of loop has destructuring pattern which has call expression instead of a reference node", "Invalid destructuring assignment target");
  56. assert.throws(function () { eval("for ([{b: foo()} = {}] of {}) { }"); }, SyntaxError, "for.of loop has destructuring pattern on head which has call expression instead of a reference node", "Invalid destructuring assignment target");
  57. function foo() {
  58. return { bar : function() {} };
  59. }
  60. assert.throws(function () { eval("for (var a in {b: foo().bar()} = {}) { }"); }, SyntaxError, "for.in loop has destructuring pattern which has linked call expression instead of a reference node", "Invalid destructuring assignment target");
  61. assert.doesNotThrow(function () { eval("for (var a in {b: foo().bar} = {}) { }"); }, "for.in loop has destructuring pattern which has a reference node is valid syntax", );
  62. }
  63. },
  64. {
  65. name: "Destructuring bug fix - object coercible",
  66. body: function () {
  67. assert.throws(function () { eval("var {} = undefined"); }, TypeError, "Object declaration - RHS cannot be be undefined", "Cannot convert null or undefined to object");
  68. assert.throws(function () { eval("var {} = null"); }, TypeError, "Object declaration - RHS cannot be be null", "Cannot convert null or undefined to object");
  69. assert.throws(function () { eval("({} = undefined);"); }, TypeError, "Object assignment pattern - RHS cannot be be undefined", "Cannot convert null or undefined to object");
  70. assert.throws(function () { eval("([{}] = []);"); }, TypeError, "Object assignment pattern nested with array pattern has evaluated to have undefined as RHS", "Cannot convert null or undefined to object");
  71. assert.throws(function () { eval("function f({}){}; f();"); }, TypeError, "Object pattern on function - evaluated to have undefined from assignment expression", "Cannot convert null or undefined to object");
  72. assert.throws(function () { eval("function f({}){}; f(null);"); }, TypeError, "Object pattern on function - evaluated to have null from assignment expression", "Cannot convert null or undefined to object");
  73. }
  74. },
  75. {
  76. name: "Destructuring bug fix - a variable in body has the same name as param should not throw in the defer parse mode",
  77. body: function () {
  78. assert.doesNotThrow(function () { eval("function foo() { function bar([a]) { var a = 1; } }"); }, "variable 'a' is not a re-declaration" );
  79. assert.doesNotThrow(function () { eval("function foo() { function bar([a], {b, b1}, [c]) { var b1 = 1; } }"); }, "variable 'b1' is not a re-declaration" );
  80. assert.doesNotThrow(function () { eval("function foo() { ({c}) => { var c = 1; } }"); }, "variable 'c' is not a re-declaration" );
  81. }
  82. },
  83. {
  84. name: "Destructuring bug fix - assign to const",
  85. body: function () {
  86. assert.throws(function () { const c = 10; ({c} = {c:11}); }, TypeError, "Cannot assign to const", "Assignment to const");
  87. assert.throws(function () { eval("const c = 10; ({c} = {c:11});"); }, TypeError, "Cannot assign to const in eval", "Assignment to const");
  88. assert.throws(function () { const c = 10; eval("({c} = {c:11});"); }, TypeError, "Cannot assign to const in eval, where const is defined outsdie of eval", "Assignment to const");
  89. }
  90. },
  91. {
  92. name: "Destructuring bug fix - pattern with rest parameter",
  93. body: function () {
  94. assert.doesNotThrow(function () { eval("function foo({a}, ...b) { if (b) { } }; foo({});"); } );
  95. assert.doesNotThrow(function () { eval("function foo([], ...b) { if (b) { } }; foo([]);"); });
  96. }
  97. },
  98. {
  99. name: "Object Destructuring with empty identifier/reference",
  100. body: function () {
  101. assert.throws(function () { eval("var {x : } = {};"); }, SyntaxError);
  102. assert.throws(function () { eval("var {x : , } = {};"); }, SyntaxError);
  103. assert.throws(function () { eval("var {x : , y} = {};"); }, SyntaxError);
  104. assert.throws(function () { eval("({x : , y} = {});"); }, SyntaxError);
  105. }
  106. },
  107. {
  108. name: "Destructuring pattern at param has arguments as declaration",
  109. body: function () {
  110. assert.doesNotThrow(function () { eval("function foo([arguments]) { arguments; }; foo([1]);"); });
  111. assert.doesNotThrow(function () { eval("function foo({arguments}) { arguments; }; foo({arguments:1});"); });
  112. assert.doesNotThrow(function () { eval("function foo({x:{arguments}}) { arguments; }; foo({x:{arguments:1}});"); });
  113. }
  114. },
  115. {
  116. name: "Destructuring pattern at param has function as default value",
  117. body: function () {
  118. assert.doesNotThrow(function () { eval("function foo(x = ({y = function (p) {}} = 'bar')) {}; foo();"); });
  119. assert.doesNotThrow(function () { eval("var foo = (x = ({y = function (p) {}} = 'bar')) => {}; foo();"); });
  120. }
  121. },
  122. {
  123. name: "Destructuring empty patterns at param with arguments/eval at function body",
  124. body: function () {
  125. (function ({}, {}, {}, {}, {}, a) {
  126. eval("");
  127. assert.areEqual(arguments[1].x, 1);
  128. assert.areEqual(a, 2);
  129. })({}, {x:1}, {}, {}, {}, 2);
  130. (function ({}, {}, {}, {}, {}, a) {
  131. (function () {
  132. eval("");
  133. })();
  134. assert.areEqual(arguments[1].x, 1);
  135. assert.areEqual(a, 2);
  136. })({}, {x:1}, {}, {}, {}, 2);
  137. (function ({}, {}, {}, {}, {}, a) {
  138. (function () {
  139. a;
  140. })();
  141. eval("");
  142. assert.areEqual(arguments[1].x, 1);
  143. assert.areEqual(a, 2);
  144. })({}, {x:1}, {}, {}, {}, 2);
  145. }
  146. },
  147. {
  148. name: "Destructuring patterns (multiple identifiers in the pattern) at param with arguments/eval at function body",
  149. body: function () {
  150. (function (x1, {x2, x3}, [x4, x5], x6) {
  151. eval("");
  152. assert.areEqual(arguments[2], [4, 5]);
  153. })(1, {x2:2, x3:3}, [4, 5], 6);
  154. (function (x1, {x2, x3}, [x4, x5], x6) {
  155. var k = x1 + x2 + x3 + x4 + x5 + x6;
  156. eval("");
  157. assert.areEqual(arguments[2], [4, 5]);
  158. assert.areEqual(k, 21);
  159. })(1, {x2:2, x3:3}, [4, 5], 6);
  160. (function (x1, {x2, x3}, [x4, x5], x6) {
  161. (function() {
  162. eval("");
  163. });
  164. assert.areEqual(arguments[3], 6);
  165. var k = x1 + x2 + x3 + x4 + x5 + x6;
  166. assert.areEqual(k, 21);
  167. })(1, {x2:2, x3:3}, [4, 5], 6);
  168. (function (x1, {x2, x3}, [x4, x5], x6) {
  169. (function() {
  170. x3; x5; x6;
  171. })();
  172. var k = x1 + x2 + x3 + x4 + x5 + x6;
  173. assert.areEqual(k, 21);
  174. })(1, {x2:2, x3:3}, [4, 5], 6);
  175. (function (x1, {x2, x3}, [x4, x5], x6) {
  176. (function() {
  177. x3; x5; x6;
  178. })();
  179. var k = x1 + x2 + x3 + x4 + x5 + x6;
  180. assert.areEqual(arguments[3], 6);
  181. assert.areEqual(k, 21);
  182. })(1, {x2:2, x3:3}, [4, 5], 6);
  183. (function (x1, {x2, x3}, [x4, x5], x6) {
  184. (function() {
  185. assert.areEqual(x1 + x2 + x3 + x4 + x5 + x6, 21);
  186. })();
  187. })(1, {x2:2, x3:3}, [4, 5], 6);
  188. }
  189. },
  190. {
  191. name: "Destructuring patterns (multiple identifiers in the pattern) at param with lambdas, arguments and eval at function body",
  192. body: function () {
  193. (function (x1, {x2, x3}, [x4, x5], x6) {
  194. (() => {
  195. assert.areEqual(arguments[2], [4, 5]);
  196. })();
  197. eval("");
  198. })(1, {x2:2, x3:3}, [4, 5], 6);
  199. (function (x1, {x2, x3}, [x4, x5], x6) {
  200. (() => {
  201. var k = x1 + x2 + x3 + x4 + x5 + x6;
  202. eval("");
  203. assert.areEqual(arguments[2], [4, 5]);
  204. assert.areEqual(k, 21);
  205. })();
  206. })(1, {x2:2, x3:3}, [4, 5], 6);
  207. }
  208. },
  209. {
  210. name: "Destructuring patterns with rest at param with arguments/eval at function body",
  211. body: function () {
  212. (function (a, {b}, ...rest) {
  213. eval("");
  214. assert.areEqual(b, 2);
  215. assert.areEqual(arguments[2], 3);
  216. })(1, {b:2}, 3);
  217. (function (a, {b}, ...rest) {
  218. (function () {
  219. eval("");
  220. })();
  221. assert.areEqual(rest, [3]);
  222. assert.areEqual(arguments[2], 3);
  223. })(1, {b:2}, 3);
  224. (function (a, {b}, ...rest) {
  225. (function () {
  226. assert.areEqual(b, 2);
  227. assert.areEqual(rest, [3]);
  228. })();
  229. assert.areEqual(rest, [3]);
  230. assert.areEqual(arguments[2], 3);
  231. })(1, {b:2}, 3);
  232. }
  233. },
  234. {
  235. name: "Rest as pattern at param with arguments/eval at function body",
  236. body: function () {
  237. (function ([a, b], c, ...{rest1, rest2}) {
  238. eval("");
  239. assert.areEqual(rest1, 4);
  240. assert.areEqual(rest2, 5);
  241. assert.areEqual(c, 3);
  242. assert.areEqual(arguments[1], 3);
  243. })([1, 2], 3, {rest1:4, rest2:5});
  244. (function ([a, b], c, ...{rest1, rest2}) {
  245. (function () {
  246. assert.areEqual(rest1, 4);
  247. assert.areEqual(rest2, 5);
  248. assert.areEqual(a+b, 3);
  249. })();
  250. eval("");
  251. assert.areEqual(arguments[0], [1, 2]);
  252. })([1, 2], 3, {rest1:4, rest2:5});
  253. }
  254. },
  255. {
  256. name: "Accessing arguments at the params",
  257. body: function () {
  258. (function (x1, {x2, x3}, [x4, x5], x6 = arguments[0]) {
  259. eval("");
  260. assert.areEqual(arguments[2], [4, 5]);
  261. assert.areEqual(x6, 1);
  262. })(1, {x2:2, x3:3}, [4, 5], undefined);
  263. (function (x1, {x2, x3}, [x4, x5], x6 = arguments[0] = 11) {
  264. eval("");
  265. assert.areEqual(arguments[0], 11);
  266. assert.areEqual(x6, 11);
  267. })(1, {x2:2, x3:3}, [4, 5], undefined);
  268. }
  269. },
  270. {
  271. name: "Object destructuring - changing the RHS when emitting",
  272. body: function () {
  273. var a = {}, b;
  274. ({x:a, y:b = 1} = a);
  275. assert.areEqual(a, undefined);
  276. assert.areEqual(b, 1);
  277. }
  278. },
  279. {
  280. name: "Destructuring - Empty object pattern inside call node is a valid syntax",
  281. body: function () {
  282. function f() {
  283. ({} = []).Symbol.interator();
  284. eval("");
  285. };
  286. }
  287. },
  288. {
  289. name: "Destructuring - Place holder slots for pattern are accounted when undeferred.",
  290. body: function () {
  291. function foo({a}) {
  292. function x() {}
  293. eval('');
  294. }
  295. foo({});
  296. function foo1(...[b]) {
  297. function x() {}
  298. eval('');
  299. }
  300. foo1([]);
  301. }
  302. },
  303. {
  304. name: "Destructuring - array pattern at call expression (which causes iterator close)",
  305. body: function () {
  306. function foo(x1, x2, x3, x4) {
  307. assert.areEqual(x1, 'first');
  308. assert.areEqual(x2, 'second');
  309. assert.areEqual(x3[0][0], 'third');
  310. assert.areEqual(x4[0][0][0][0], 'fourth');
  311. }
  312. var a1;
  313. var a2;
  314. foo([{}] = 'first', 'second', [[a1]] = [['third']], [[[[a2]]]] = [[[['fourth']]]]);
  315. assert.areEqual(a1, 'third');
  316. assert.areEqual(a2, 'fourth');
  317. }
  318. },
  319. {
  320. name: "Destructuring - array patten at call expression - validating the next/return functions are called.",
  321. body: function () {
  322. var nextCount = 0;
  323. var returnCount = 0;
  324. var iterable = {};
  325. iterable[Symbol.iterator] = function () {
  326. return {
  327. next: function() {
  328. nextCount++;
  329. return {value : 1, done:false};
  330. },
  331. return: function (value) {
  332. returnCount++;
  333. return {done:true};
  334. }
  335. };
  336. };
  337. var obj = {
  338. set prop(val) {
  339. throw new Error('From setter');
  340. }
  341. };
  342. var foo = function () { };
  343. assert.throws(function () { foo([[obj.prop]] = [iterable]); }, Error, 'pattern at call expression throws and return function is called', 'From setter');
  344. assert.areEqual(nextCount, 1);
  345. assert.areEqual(returnCount, 1);
  346. }
  347. },
  348. {
  349. name: "Destructuring pattern at param has nested blocks.",
  350. body: function () {
  351. assert.doesNotThrow(function () { eval("var e = 1; ( {abcdef = ((((({})) = (1))))} = (e)) => { try{ } catch(e) {}}") }, "Should not throw when the default value and it's initializer both have extra parentheses.");
  352. assert.doesNotThrow(function () { eval("var e = 1; ( {ghijkl = ((((({})) = 1 )))} = (e)) => { try{ } catch(e) {}}") }, "Should not throw when the default value has extra parentheses.");
  353. assert.doesNotThrow(function () { eval("var e = 1; ( {mnopqrs = ((( {} = (1))))} = (e)) => { try{ } catch(e) {}}") }, "Should not throw when the default value initializer has extra parentheses.");
  354. assert.doesNotThrow(function () { eval("var e = 1; ( {tuvwxy = ((( {} = 1 )))} = (e)) => { try{ } catch(e) {}}") }, "Should not throw when the default value and initializer are wrapped in extra parentheses.");
  355. assert.doesNotThrow(function () { eval("var e = 1; ( {abcdef = (((((foo)) = (1))))} = (e)) => { try{ } catch(e) {}}") }, "Should not throw when the default value and it's initializer both have extra parentheses.");
  356. assert.doesNotThrow(function () { eval("var e = 1; ( {ghijkl = (((((foo)) = 1 )))} = (e)) => { try{ } catch(e) {}}") }, "Should not throw when the default value has extra parentheses.");
  357. assert.doesNotThrow(function () { eval("var e = 1; ( {mnopqrs = ((( foo = (1))))} = (e)) => { try{ } catch(e) {}}") }, "Should not throw when the default value initializer has extra parentheses.");
  358. assert.doesNotThrow(function () { eval("var e = 1; ( {tuvwxy = ((( foo = 1 )))} = (e)) => { try{ } catch(e) {}}") }, "Should not throw when the default value and initializer are wrapped in extra parentheses.");
  359. assert.doesNotThrow(function () { eval("var a; ({ tyssjh = ((cspagh = 4) => a) } = 1) => { /*jjj*/ }; (function(a) { })()") }, "Should not throw when there is a nested lambda expression within destructured variable declaration.");
  360. assert.doesNotThrow(function () { eval("var a; [a = class aClass {}] = [];") }, "Should not throw when class declaration exists in destructured variable initializer.");
  361. assert.throws(function () { eval("function test5(){ var ggnzrk=function(){ }; ({ggnzrk, namespace: {}, w: [(inmgdv)]}) => { };};") }, SyntaxError, "Should throw if nested destructured declaration has a variable name in parenthesis.");
  362. assert.throws(function () { eval("function test5(){ var ggnzrk=function(){ }; ({ggnzrk, namespace: {}, w: ([inmgdv])}) => { };};") }, SyntaxError, "Should throw if nested destructured declaration is wrapped in extra parenthesis.");
  363. assert.throws(function () { eval("{ ([((iydvhw)), gpvpgk]) => { }; } var iydvhw=function(){return this};") }, SyntaxError, "Should throw if variable names in destructured declaration have extra parentheses.");
  364. assert.throws(function () { eval("(nmlwii, [((yycokb) = (1))] = (nmlwii)) => { };") }, SyntaxError, "Should throw if one or more of the lambda parameters have destructured variable declaration with initializer capturing another parameter.");
  365. assert.throws(function () { eval("({ggnzrk, w: (ggnzrk)}) => { };") }, SyntaxError, "Should throw if reused symbol in destructured variable declaration.");
  366. assert.throws(function () { eval("([x, ...((yArgs))]) => {}") }, SyntaxError, "Should throw if rest parameter name in a declaration is enclosed in parenthesis.");
  367. assert.throws(function () { eval("([x, ...(([yArgs, zArgs]))]) => {}") }, SyntaxError, "Should throw if rest parameter name in a declaration is enclosed in parenthesis.");
  368. assert.doesNotThrow(function () { eval("( {abcdef = ((((([...((abcdef))] = [1, 2, 3])) = (1))))} = (e)) => { try{ } catch(abcdef) {}}") }, "Should not throw when rest parameter name is enclosed in extra parentheses.");
  369. var a1 = 10;
  370. var b1 = 20;
  371. bar1 = ( {abcdef = (((((a1)) = (30))))} = (b1 = 40) ) => { try { throw a1; } catch(a1) { } };
  372. bar1();
  373. assert.areEqual(a1, 30, "The default value initializer should have fired for parameter in destructuring pattern.");
  374. assert.areEqual(b1, 40, "The default value block initializer should have fired for parameter in destructuring pattern.");
  375. var a2 = 10;
  376. var b2 = 20;
  377. bar2 = ( {abcdef = (((((a2)) = (30))))} = (b2 = 40) ) => { try { throw a2; } catch(a2) { } };
  378. bar2({abcdef : 50});
  379. assert.areEqual(a2, 10, "The default value initializer should NOT have fired for parameter in destructuring pattern.");
  380. assert.areEqual(b2, 20, "The default value block initializer should NOT have fired for parameter in destructuring pattern.");
  381. var a3 = 10;
  382. var b3 = 20;
  383. bar3 = ( {aa3 = a3, bb3 = b3, abcdef = (((((a3)) = (30))))} = (b3 = 40) ) =>
  384. {
  385. try
  386. {
  387. assert.areEqual(a3, undefined, "The variable a3 in the current scope should not have been assigned yet.");
  388. assert.areEqual(b3, undefined, "The variable b3 in the current scope should not have been assigned yet.");
  389. assert.areEqual(aa3, 10, "The parameter aa3 should get initialized correctly.");
  390. assert.areEqual(bb3, 20, "The parameter bb3 should get initialized correctly.");
  391. var a3 = 60;
  392. var b3 = 70;
  393. throw a3;
  394. } catch(a3) { }
  395. };
  396. bar3({abcdef : 50});
  397. assert.areEqual(a3, 10, "The variable a3 in the enclosing scope should not have been changed.");
  398. assert.areEqual(b3, 20, "The variable a3 in the enclosing scope should not have been changed.");
  399. var a4 = 10;
  400. var b4 = 15;
  401. bar4 = ( { b4 = ((xyz = 4) => a4) } = 1) => { b4 = 35; return b4; };
  402. var c4 = bar4();
  403. var d4 = (function( { a4, b4 } = { a4 : 20, b4 : 25 }) { return a4;})();
  404. assert.areEqual(a4, 10, "The variable in the enclosing scope should not have been changed.");
  405. assert.areEqual(b4, 15, "The variable in the enclosing scope should not have been changed.");
  406. assert.areEqual(c4, 35, "The variable in the enclosing scope should not have been changed.");
  407. assert.areEqual(d4, 20, "The variable in the enclosing scope should not have been changed.");
  408. }
  409. },
  410. {
  411. name: "Destructuring pattern with rest parameter has nested blocks.",
  412. body: function () {
  413. [...((a5))] = [1, 2, 3];
  414. assert.areEqual(a5, [1, 2, 3], "The rest parameter with extra parentheses gets assigned correctly.");
  415. assert.doesNotThrow(function () { eval("[...((a))] = [1, 2, 3]") }, "Should not throw when rest parameter name is enclosed in extra parentheses.");
  416. }
  417. },
  418. {
  419. name: "Destructuring bug fix - a reference from a 'this' object should be valid statement",
  420. body: function () {
  421. assert.doesNotThrow(function () { [this.x] = []; }, "array destructuring - referencing x from this in pattern is a correct syntax" );
  422. assert.doesNotThrow(function () { ({x:this.x} = {}); }, "object destructuring - referencing x from this in pattern is a correct syntax" );
  423. (function () {
  424. this.x = 1, this.y = 2;
  425. [this.x, this.y] = [this.y, this.x];
  426. assert.areEqual(this.x, 2);
  427. assert.areEqual(this.y, 1);
  428. })();
  429. assert.doesNotThrow(function () { [...this.x] = [1]; }, "array destructuring rest - referencing x from this in pattern is a correct syntax" );
  430. }
  431. },
  432. {
  433. name: "array destructuring as catch parameter can yield properly",
  434. body: function () {
  435. function * gn() {
  436. try {
  437. throw [];
  438. } catch ([c = (yield 2)]) {
  439. }
  440. };
  441. var it = gn();
  442. var k = it.next();
  443. assert.areEqual(2, k.value, "next should invoke the yield in the generator and which yields 2");
  444. }
  445. },
  446. {
  447. name: "array destructuring nested as catch parameter can yield properly",
  448. body: function () {
  449. function * gn() {
  450. try {
  451. throw [{x:[]}];
  452. } catch ([{x:[c = (yield 2)]}]) {
  453. }
  454. };
  455. var it = gn();
  456. var k = it.next();
  457. assert.areEqual(2, k.value, "next should invoke the yield in the generator and which yields 2");
  458. }
  459. }
  460. ];
  461. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });