destructuring_params.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 destructuring syntax as params",
  9. body: function () {
  10. assert.doesNotThrow(function () { eval("function foo({x:x}) {}"); }, "Object destructuring pattern as a formal is valid syntax");
  11. assert.doesNotThrow(function () { eval("function foo({x}) {}"); }, "Object destructuring pattern (shorthand) as a formal is valid syntax");
  12. assert.doesNotThrow(function () { eval("function foo([x]) {}"); }, "Array destructuring pattern as a formal is valid syntax");
  13. assert.doesNotThrow(function () { eval("function foo({x:x}, y) {}"); }, "Object destructuring pattern as a first formal is valid syntax");
  14. assert.doesNotThrow(function () { eval("function foo([x], y) {}"); }, "Array destructuring pattern as a first formal is valid syntax");
  15. assert.doesNotThrow(function () { eval("function foo(y, {x:x}) {}"); }, "Object destructuring pattern as a second formal is valid syntax");
  16. assert.doesNotThrow(function () { eval("function foo(y, [x]) {}"); }, "Array destructuring pattern as a second formal is valid syntax");
  17. assert.doesNotThrow(function () { eval("function foo({}) {}"); }, "Object destructuring pattern (with empty syntax) as a formal is valid syntax");
  18. assert.doesNotThrow(function () { eval("function foo([]) {}"); }, "Array destructuring pattern (with empty syntax) as a formal is valid syntax");
  19. assert.doesNotThrow(function () { eval("function foo([,]) {}"); }, "Array destructuring pattern (with empty syntax and comma) as a formal is valid syntax");
  20. }
  21. },
  22. {
  23. name: "Destructuring syntax as params - invalid syntax",
  24. body: function () {
  25. assert.throws(function () { eval("function foo({,}) {}"); }, SyntaxError, "Object destructuring pattern as a formal with empty names is not valid syntax", "Expected identifier, string or number");
  26. assert.throws(function () { eval("function foo(({})) {}"); }, SyntaxError, "Object destructuring pattern as a formal surrounded with parens is not valid syntax", "Expected identifier");
  27. assert.throws(function () { eval("function foo(([])) {}"); }, SyntaxError, "Array destructuring pattern as a formal surrounded with parens is not valid syntax", "Expected identifier");
  28. assert.throws(function () { eval("function foo([x}) {}"); }, SyntaxError, "Array destructuring pattern without right bracket is not valid syntax", "Unexpected operator in destructuring expression");
  29. assert.throws(function () { eval("function foo({x:abc+1}) {}"); }, SyntaxError, "Object destructuring pattern with operator is not valid syntax", "Unexpected operator in destructuring expression");
  30. assert.throws(function () { eval("function foo({x:abc.d}) {}"); }, SyntaxError, "Object destructuring pattern as a formal with property reference is not valid syntax", "Syntax error");
  31. assert.throws(function () { eval("function foo([abc.d]) {}"); }, SyntaxError, "Array destructuring pattern as a formal with property reference is not valid syntax", "Syntax error");
  32. assert.throws(function () { eval("function foo([super]) {}"); }, SyntaxError, "Array destructuring pattern as a formal identifier as super is not valid syntax", "The use of a keyword for an identifier is invalid");
  33. assert.throws(function () { eval("function foo({x:super}) {}"); }, SyntaxError, "Object destructuring pattern as a formal identifier as super is not valid syntax", "The use of a keyword for an identifier is invalid");
  34. assert.throws(function () { eval("function foo({x:super()}) {}"); }, SyntaxError, "Object destructuring pattern as a formal identifier as super call is not valid syntax", "The use of a keyword for an identifier is invalid");
  35. assert.throws(function () { eval("function foo([super()]) {}"); }, SyntaxError, "Array destructuring pattern as a formal identifier as super call is not valid syntax", "The use of a keyword for an identifier is invalid");
  36. }
  37. },
  38. {
  39. name: "Destructuring syntax as params - Multiple and mixed patterns",
  40. body: function () {
  41. assert.doesNotThrow(function () { eval("function foo({x:x}, {y:y}, {z:z}) {}"); }, "Three params as object pattern is valid syntax");
  42. assert.doesNotThrow(function () { eval("function foo([x], [y], [z]) {}"); }, "Three params as array pattern is valid syntax");
  43. assert.doesNotThrow(function () { eval("function foo({x1:x1, x2:x2, x3:x3}, {y1:y1, y1:y2}) {}"); }, "Two params as object pattern, and each pattern has more than one matching name is valid syntax");
  44. assert.doesNotThrow(function () { eval("function foo([x1, x2, x3], [y1, y2, y3]) {}"); }, "Two params as array pattern, and each pattern has more than one matching name is valid syntax");
  45. assert.doesNotThrow(function () { eval("function foo({x1:x1}, [y1]) {}"); }, "Two params with first object pattern and second array pattern is valid syntax");
  46. assert.doesNotThrow(function () { eval("function foo([x1], {y1:y1}) {}"); }, "Two params with first array pattern and second object pattern is valid syntax");
  47. assert.doesNotThrow(function () { eval("function foo(x1, {x2, x3}, [x4, x5], x6) {}"); }, "Multiple params with mixed of binding identifier and binding pattern is valid syntax");
  48. assert.doesNotThrow(function () { eval("function foo({x1:[y1]}) {}"); }, "Object destructuring pattern nesting array pattern as a formal is valid syntax");
  49. assert.doesNotThrow(function () { eval("function foo([x1, {y1:y1}]) {}"); }, "Array destructuring pattern nesting object pattern as a formal is valid syntax");
  50. }
  51. },
  52. {
  53. name: "Destructuring syntax as params - class/function expression/function expression with names/lambda",
  54. body: function () {
  55. assert.doesNotThrow(function () { eval("({x}) => x;"); }, "Object destructuring pattern as a formal on lambda is valid syntax");
  56. assert.doesNotThrow(function () { eval("([x]) => x;"); }, "Array destructuring pattern as a formal on lambda is valid syntax");
  57. assert.doesNotThrow(function () { eval("class foo { constructor({x1}){} }"); }, "Object destructuring pattern as a formal on constructor of class is valid syntax");
  58. assert.doesNotThrow(function () { eval("class foo { constructor([x1]){} }"); }, "Array destructuring pattern as a formal on constructor of class is valid syntax");
  59. assert.doesNotThrow(function () { eval("class foo { method({x1}){ }; set prop({x1}){} }"); }, "Object destructuring pattern on method and setter of class is valid syntax");
  60. assert.doesNotThrow(function () { eval("class foo { method([x1]){ }; set prop([x1]){} }"); }, "Array destructuring pattern on method and setter of class is valid syntax");
  61. assert.doesNotThrow(function () { eval("let foo = function ({x1}, [x2]){};"); }, "Destructuring patterns as formals on function expression is valid syntax");
  62. assert.doesNotThrow(function () { eval("(function({x1}, [x2]){})"); }, "Destructuring patterns as formals on anonymous function is valid syntax");
  63. assert.doesNotThrow(function () { eval("let bar = function foo({x1}, [x2]){};"); }, "Destructuring patterns as formals on named function expression is valid syntax");
  64. assert.doesNotThrow(function () { eval("new Function('{x}', '[y]', 'return x + y');"); }, "Destructuring patterns as formals on function constructor is valid syntax");
  65. assert.doesNotThrow(function () { eval("let obj = { foo({x}) {}, set prop([x]) {} }"); }, "Destructuring pattern as a formal on functions of object literal is valid syntax");
  66. }
  67. },
  68. {
  69. name: "Destructuring syntax as params - Initializer",
  70. body: function () {
  71. assert.doesNotThrow(function () { eval("function foo({x:x = 10}) {}"); }, "One param as object destructuring pattern with initializer is valid syntax");
  72. assert.doesNotThrow(function () { eval("function foo([x = 20]) {}"); }, "One param as array destructuring pattern with initializer is valid syntax");
  73. assert.doesNotThrow(function () { eval("function foo({x1:x1 = 1}, {y1:y1 = 2}) {}"); }, "Two params as object destructuring pattern with initializer is valid syntax");
  74. assert.doesNotThrow(function () { eval("function foo([x1 = 1], [y1 = 2]) {}"); }, "Two params as array destructuring pattern with initializer is valid syntax");
  75. assert.doesNotThrow(function () { eval("function foo({x1:x1 = 1, x2:x2 = 2, x3:x3 = 3}) {}"); }, "One param as object destructuring pattern has three names with initializer is valid syntax");
  76. assert.doesNotThrow(function () { eval("function foo([x1 = 1, x2 = 2, x3 = 3]) {}"); }, "One param as array destructuring pattern has three names with initializer is valid syntax");
  77. assert.doesNotThrow(function () { eval("function foo({x1:x1 = 1}, [y1 = 2]) {}"); }, "First param as object pattern and second as array pattern with initializer is valid syntax");
  78. assert.doesNotThrow(function () { eval("function foo([x1 = 1], {y1:y1 = 2}) {}"); }, "First param as array pattern and second as object pattern with initializer is valid syntax");
  79. assert.doesNotThrow(function () { eval("function foo({x:x} = {x:1}) {}"); }, "Object destructuring pattern has default value is valid syntax");
  80. assert.doesNotThrow(function () { eval("function foo([x] = [1]) {}"); }, "Array destructuring pattern has default value is valid syntax");
  81. assert.doesNotThrow(function () { eval("function foo({x:x = 1} = {x:2}) {}"); }, "Object destructuring pattern has default and identifier has initializer is valid syntax");
  82. assert.doesNotThrow(function () { eval("function foo([x = 1] = [2]) {}"); }, "Array destructuring pattern has default and identifier has initializer is valid syntax");
  83. assert.doesNotThrow(function () { eval("function foo({x1:[y1 = 1]}) {}"); }, "Object destructuring pattern nesting array pattern which has initializer is valid syntax");
  84. assert.doesNotThrow(function () { eval("function foo([x1, {y1:y1 = 1}]) {}"); }, "Array destructuring pattern nesting object pattern which has initializer is valid syntax");
  85. assert.doesNotThrow(function () { eval("function foo({x1:[y1 = 1] = [2]} = {x1:[3]}) {}"); }, "Object destructuring pattern has default and nesting array pattern which has initializer is valid syntax");
  86. assert.doesNotThrow(function () { eval("function foo([{y1:y1 = 1} = {y1:2}] = [{y1:3}]) {}"); }, "Array destructuring pattern has default and nesting object pattern which has initializer is valid syntax");
  87. }
  88. },
  89. {
  90. name: "Destructuring syntax as params - redeclarations",
  91. body: function () {
  92. assert.throws(function () { eval("function foo({x:x, x:x}) {}"); }, SyntaxError, "One formal as object pattern has duplicate binding identifiers is not valid syntax", "Let/Const redeclaration");
  93. assert.throws(function () { eval("function foo({x:x}, {x:x}) {}"); }, SyntaxError, "Two formals as object patterns have duplicate binding identifiers is not valid syntax", "Let/Const redeclaration");
  94. assert.throws(function () { eval("function foo([x, x]) {}"); }, SyntaxError, "One formal as array pattern has duplicate binding identifiers is not valid syntax", "Let/Const redeclaration");
  95. assert.throws(function () { eval("function foo([x], [x]) {}"); }, SyntaxError, "Two formals as array patterns have duplicate binding identifiers is not valid syntax", "Let/Const redeclaration");
  96. assert.throws(function () { eval("function foo([x], {x:x}) {}"); }, SyntaxError, "Mixed array and object pattern with duplicate identifiers is not valid syntax", "Let/Const redeclaration");
  97. assert.throws(function () { eval("function foo([x], x) {}"); }, SyntaxError, "First formal as array pattern has matching name with the second formal is not valid syntax", "Duplicate formal parameter names not allowed in this context");
  98. assert.throws(function () { eval("function foo(x, [x]) {}"); }, SyntaxError, "First normal formal is matching with the second formal which is array pattern is not valid syntax", "Let/Const redeclaration");
  99. assert.throws(function () { eval("function foo({x:{z:[z1]}}, z1) {}"); }, SyntaxError, "First formal as nesting object pattern has matching name with the second formal is not valid syntax", "Duplicate formal parameter names not allowed in this context");
  100. assert.throws(function () { eval("function foo([x]) { let x = 10;}"); }, SyntaxError, "Object destructuring pattern as a formal is valid syntax", "Let/Const redeclaration");
  101. assert.doesNotThrow(function () { eval("function foo([x]) { var x = 10;}"); }, "var declared names matching with formal (as array pattern) is valid syntax");
  102. }
  103. },
  104. {
  105. name: "Destructuring on param - basic functionality",
  106. body : function () {
  107. function f1({x:x}) {
  108. return x;
  109. }
  110. let ret = f1({x:1});
  111. assert.areEqual(ret, 1, "Object pattern as a formal matches with actual param and initializes the identifier correctly");
  112. function f2([x]) {
  113. return x;
  114. }
  115. ret = f2([2]);
  116. assert.areEqual(ret, 2, "Array pattern as a formal matches with actual param and initializes the identifier correctly");
  117. function f3({x}, [y], z) {
  118. return x + y + z;
  119. }
  120. ret = f3({x:1}, [2], 3);
  121. assert.areEqual(ret, 6, "First formal as object pattern and second formal as array pattern should match and initialize identifiers correctly");
  122. let f4 = function ([x], {y:y}, z) {
  123. return x + y + z;
  124. }
  125. ret = f4([1], {y:2}, 3);
  126. assert.areEqual(ret, 6, "First formal as array pattern and second formal as object pattern should match and initialize identifiers correctly");
  127. }
  128. },
  129. {
  130. name: "Destructuring on param - initializer",
  131. body : function () {
  132. function f1({x:x1 = 11}, [x2 = 22]) {
  133. assert.areEqual(x1, 1, "Identifier from the first pattern has initializer but matches the actual first param and initializes correctly");
  134. assert.areEqual(x2, 2, "Identifier from the second pattern has initializer byt matches the actual second param and initializes correctly");
  135. }
  136. f1({x:1}, [2]);
  137. function f2({x:x1 = 11}, [x2 = 22]) {
  138. assert.areEqual(x1, 11, "Identifier from the first pattern gets undefined from the actual first param so initializes with initializer");
  139. assert.areEqual(x2, 22, "Identifier from the second pattern gets undefined from the actual second param so initializes with initializer");
  140. }
  141. f2({}, []);
  142. (function ({x:x1 = 11} = {x:111}, [x2 = 22] = [222]) {
  143. assert.areEqual(x1, 111, "First pattern matches with default as the actual first param is undefined");
  144. assert.areEqual(x2, 222, "Second pattern matches with default as the actual second param is undefined");
  145. })(undefined, undefined);
  146. }
  147. },
  148. {
  149. name: "Destructuring on param - functionality on lambda/function expression",
  150. body : function () {
  151. (function({x:x1}) {
  152. assert.areEqual(x1, 1, "Anonymous function - object pattern as a formal matches with actual param and initializes identifier correctly");
  153. })({x:1});
  154. let f1 = ([x1]) => x1 * 2;
  155. assert.areEqual(f1([2]), 4, "Lambda - array pattern as a formal matches with actual param and initializes identifier correctly");
  156. let f2 = ({x:x2}) => x2 * 4;
  157. assert.areEqual(f2({x:2}), 8, "Lambda - object pattern as a formal matches with actual param and initializes identifier correctly");
  158. let f3 = function foo({x:x1}) {
  159. assert.areEqual(x1, 1, "Named function expression - object pattern as a formal matches with actual param and initializes identifier correctly");
  160. }
  161. f3({x:1});
  162. let f4 = new Function("{x}", "[y]", "return x + y");
  163. assert.areEqual(f4({x:1}, [2]), 3, "Function constructor - patterns as formals match with actual params and initialize identifiers correctly");
  164. }
  165. },
  166. {
  167. name: "Destructuring on param - captures",
  168. body : function () {
  169. function f1({x:x1}) {
  170. function f1_1() {
  171. assert.areEqual(x1, 1, "Identifier from object pattern is captured and initialized correctly");
  172. }
  173. f1_1();
  174. }
  175. f1({x:1});
  176. function f2([x1]) {
  177. function f2_1() {
  178. assert.areEqual(x1, 2, "Identifier from array pattern is captured and initialized correctly");
  179. }
  180. f2_1();
  181. }
  182. f2([2]);
  183. function f3({x:x1}, [y1], z) {
  184. (function () {
  185. x1++;
  186. })();
  187. (function () {
  188. y1++;
  189. })();
  190. var k = x1 + y1 + z;
  191. assert.areEqual(k, 62, "Identifiers from patterns are captured and initialized correctly");
  192. }
  193. f3({x:10}, [20], 30);
  194. }
  195. },
  196. {
  197. name: "Destructuring on param - capture due to eval",
  198. body : function () {
  199. function f1({x:x1}, [x2]) {
  200. eval('');
  201. assert.areEqual(x1, 1, "Function has eval - identifier from the object pattern is initialized correctly");
  202. assert.areEqual(x2, 2, "Function has eval - identifier from the array pattern is initialized correctly under eval");
  203. }
  204. f1({x:1}, [2]);
  205. function f2({x:x1}, [x2]) {
  206. eval('');
  207. (function () {
  208. x1;
  209. x2;
  210. })();
  211. assert.areEqual(x1, 3, "Function has eval and inner function - identifier from the object pattern is initialized correctly");
  212. assert.areEqual(x2, 4, "Function has eval and inner function - identifier from the array pattern is initialized correctly");
  213. }
  214. f2({x:3}, [4]);
  215. function f3({x:x1}, [x2]) {
  216. (function () {
  217. eval('');
  218. assert.areEqual(x1, 5, "Function's inner function has eval - identifier from the object pattern is initialized correctly");
  219. assert.areEqual(x2, 6, "Function's inner function has eval - identifier from the object pattern is initialized correctly");
  220. })();
  221. }
  222. f3({x:5}, [6]);
  223. }
  224. },
  225. {
  226. name: "Destructuring on param - captures (eval and arguments)",
  227. body : function () {
  228. function f1({x:x1}, [x2]) {
  229. assert.areEqual(arguments[0], {x:1}, "arguments[0] is initialized correctly with first actual argument");
  230. assert.areEqual(arguments[1], [2], "arguments[1] is initialized correctly with second actual argument");
  231. }
  232. f1({x:1}, [2]);
  233. function f2({x:x1}, [x2]) {
  234. (function() {
  235. })();
  236. eval('');
  237. assert.areEqual(arguments[0], {x:1}, "Function has inner function and eval - arguments[0] is initialized correctly with first actual argument");
  238. assert.areEqual(arguments[1], [2], "Function has inner function and eval - arguments[1] is initialized correctly with second actual argument");
  239. }
  240. f2({x:1}, [2]);
  241. function f3({x:x1}, [x2]) {
  242. (function() {
  243. eval('');
  244. })();
  245. assert.areEqual(arguments[0], {x:1}, "Function's inner function has eval - arguments[0] is initialized correctly with first actual argument");
  246. assert.areEqual(arguments[1], [2], "Function's inner function has eval - arguments[1] is initialized correctly with second actual argument");
  247. }
  248. f3({x:1}, [2]);
  249. (function({x:x1}, x2) {
  250. x2 = 3;
  251. assert.areEqual(arguments[1], 2, "arguments object is unmapped - changing second formal does not change arguments[1]");
  252. })({x:1}, 2);
  253. (function ([x1], x2) {
  254. arguments[1] = 2;
  255. assert.areEqual(x2, 1, "arguments object is unmapped - changing arguments[1] does not change second param");
  256. })([], 1);
  257. (function ({x:x1}, x2) {
  258. x2 = 2;
  259. (function() {
  260. eval('');
  261. })();
  262. assert.areEqual(arguments[1], 1, "Function's inner function has eval - arguments object is unmapped - changing second formal does not change arguments[1]");
  263. })({}, 1);
  264. (function ([x1], x2) {
  265. (function() {
  266. eval('');
  267. })();
  268. arguments[1] = 2;
  269. assert.areEqual(x2, 1, "Function's inner function has eval - arguments object is unmapped - changing arguments[1] does not change second param");
  270. })([], 1);
  271. }
  272. },
  273. {
  274. name: "Destructuring on param - multiple/mixed/nested parameters",
  275. body : function () {
  276. (function (x1, {x2, x3}, [x4, x5], x6) {
  277. var k = x1 + x2 + x3 + x4 + x5 + x6;
  278. assert.areEqual(k, 21, "Identifiers under various patterns are matched and initialized correctly");
  279. })(1, {x2:2, x3:3}, [4, 5], 6);
  280. (function (x1, {x2, x3}, [x4, x5], x6) {
  281. var k = x1 + x2 + x3 + x4 + x5 + x6;
  282. eval('');
  283. assert.areEqual(k, 21, "Function has eval - identifiers under various patterns are matched and initialized correctly");
  284. })(1, {x2:2, x3:3}, [4, 5], 6);
  285. (function (x1, {x2, x3}, [x4, x5], x6) {
  286. var k = x1 + x2 + x3 + x4 + x5 + x6;
  287. (function() {
  288. eval('');
  289. });
  290. assert.areEqual(k, 21, "Function's inner function has eval - identifiers under various patterns are matched and initialized correctly");
  291. })(1, {x2:2, x3:3}, [4, 5], 6);
  292. }
  293. },
  294. {
  295. name: "Destructuring on param - misc",
  296. body : function () {
  297. let f1 = function foo(x0, {x:x1}, [x2]) {
  298. assert.areEqual(x1, 1, "Function expression with name - identifier from second formal is matched and initialized correctly");
  299. assert.areEqual(x2, 2, "Function expression with name - identifier from third formal is matched and initialized correctly");
  300. }
  301. f1(0, {x:1}, [2]);
  302. let f2 = function foo1(x0, {x:x1}, [x2]) {
  303. eval('');
  304. assert.areEqual(x1, 1, "Function expression with name has eval - identifier from second formal is matched and initialized correctly");
  305. assert.areEqual(x2, 2, "Function expression with name has eval - identifier from third formal is matched and initialized correctly");
  306. }
  307. f2(0, {x:1}, [2]);
  308. let f3 = function foo2(x0, {x:x1}, [x2]) {
  309. with(x1) {
  310. assert.areEqual(x1_1, 1, "Function expression with name has 'with' - identifier from second formal is matched and initialized correctly");
  311. assert.areEqual(x2, 2, "Function expression with name has 'with' - identifier from third formal is matched and initialized correctly");
  312. }
  313. }
  314. f3(0, {x:{x1_1:1}}, [2]);
  315. let f4 = function foo3(x0, {x:x1}, [x2]) {
  316. try {
  317. throw 'abc';
  318. }
  319. catch(e) {
  320. (function () {
  321. assert.areEqual(x1, 1, "Function expression with name has 'try/catch' - identifier from second formal is matched and initialized correctly");
  322. assert.areEqual(x2, 2, "Function expression with name has 'try/catch' - identifier from third formal is matched and initialized correctly");
  323. })();
  324. }
  325. }
  326. f4(0, {x:1}, [2]);
  327. let f5 = function foo4(x0, {x:x1}, [x2]) {
  328. try {
  329. throw 'abc';
  330. }
  331. catch(e) {
  332. (function () {
  333. eval('');
  334. assert.areEqual(x1, 1, "Function expression with name has 'try/catch and eval' - identifier from second formal is matched and initialized correctly");
  335. assert.areEqual(x2, 2, "Function expression with name has 'try/catch and eval' - identifier from third formal is matched and initialized correctly");
  336. })();
  337. }
  338. }
  339. f5(0, {x:1}, [2]);
  340. }
  341. },
  342. {
  343. name: "Destructuring on param - class",
  344. body : function () {
  345. class Foo {
  346. add([x1]) {
  347. this.x1 += x1;
  348. }
  349. set prop({x1}) {
  350. this.x1 = x1;
  351. }
  352. get prop() {
  353. return this.x1;
  354. }
  355. static Avg({x1}, [x2], x3) {
  356. return (x1+x2+x3)/3;
  357. }
  358. }
  359. assert.areEqual(Foo.Avg({x1:3}, [4], 5), 4, "Class's static function - identifiers from formal patterns are matched and initialized correctly");
  360. let obj = new Foo();
  361. obj.prop = {x1:1};
  362. assert.areEqual(obj.prop, 1, "Class's setter - identifier from the formal object pattern is matched and initialized correctly");
  363. obj.add([2]);
  364. assert.areEqual(obj.prop, 3, "Class's method - identifier from the formal array pattern is matched and initialized correctly");
  365. }
  366. }
  367. ];
  368. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });