destructuring_catch.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 catch param",
  9. body: function () {
  10. assert.doesNotThrow(function () { eval("try {} catch({}) {}"); }, "Object destructuring pattern (empty) as a catch param is valid syntax");
  11. assert.doesNotThrow(function () { eval("try {} catch([]) {}"); }, "Array destructuring pattern (empty) as a catch param is valid syntax");
  12. assert.doesNotThrow(function () { eval("try {} catch({x:x}) {}"); }, "Object destructuring pattern as a catch param is valid syntax");
  13. assert.doesNotThrow(function () { eval("try {} catch([e]) {}"); }, "Object destructuring pattern as a catch param is valid syntax");
  14. assert.doesNotThrow(function () { eval("try {} catch({x}) {}"); }, "Object destructuring pattern (as short-hand) as a catch param is valid syntax");
  15. assert.doesNotThrow(function () { eval("function foo() {try {} catch({x, y:[y]}) {} }"); }, "Object destructuring pattern as a catch param inside a function is valid syntax");
  16. assert.doesNotThrow(function () { eval("function foo() {try {} catch([x, {y:[y]}]) {} }"); }, "Object destructuring pattern as a catch param inside a function is valid syntax");
  17. }
  18. },
  19. {
  20. name: "Destructuring syntax as catch param - invalid syntax",
  21. body: function () {
  22. assert.throws(function () { eval("function foo() {try {} catch({,}) {} }"); }, SyntaxError, "Object destructuring pattern as a catch param with empty names is not valid syntax", "Expected identifier, string or number");
  23. assert.throws(function () { eval("function foo() {try {} catch(([])) {} }"); }, SyntaxError, "Object destructuring pattern as a catch param with empty names is not valid syntax", "Expected identifier");
  24. assert.throws(function () { eval("function foo() {try {} catch({x:abc+1}) {} }"); }, SyntaxError, "Object destructuring pattern as a catch param with operator is not valid syntax", "Unexpected operator in destructuring expression");
  25. assert.throws(function () { eval("function foo() {try {} catch([abc.d]) {} }"); }, SyntaxError, "Array destructuring pattern as a catch param with property reference is not valid syntax", "Syntax error");
  26. assert.throws(function () { eval("function foo() {try {} catch([x], [y]) {} }"); }, SyntaxError, "More than one patterns/identifiers as catch params is not valid syntax", "Expected ')'");
  27. assert.throws(function () { eval("function foo() {'use strict'; try {} catch([arguments]) {} }"); }, SyntaxError, "StrictMode - identifier under pattern named as 'arguments' is not valid syntax", "Invalid usage of 'arguments' in strict mode");
  28. assert.throws(function () { eval("function foo() {'use strict'; try {} catch([eval]) {} }"); }, SyntaxError, "StrictMode - identifier under pattern named as 'eval' is not valid syntax", "Invalid usage of 'eval' in strict mode");
  29. }
  30. },
  31. {
  32. name: "Destructuring syntax as params - Initializer",
  33. body: function () {
  34. assert.doesNotThrow(function () { eval("function foo() {try {} catch({x:x = 20}) {} }"); }, "Catch param as object destructuring pattern with initializer is valid syntax");
  35. assert.doesNotThrow(function () { eval("function foo() {try {} catch([x = 20]) {} }"); }, "Catch param as array destructuring pattern with initializer is valid syntax");
  36. assert.doesNotThrow(function () { eval("function foo() {try {} catch({x1:x1 = 1, x2:x2 = 2, x3:x3 = 3}) {} }"); }, "Catch param as object destructuring pattern has three names with initializer is valid syntax");
  37. assert.doesNotThrow(function () { eval("function foo() {try {} catch([x1 = 1, x2 = 2, x3 = 3]) {} }"); }, "Catch param as array destructuring pattern has three names with initializer is valid syntax");
  38. assert.throws(function () { eval("function foo() {try {} catch({x:x} = {x:1}) {} }"); }, SyntaxError, "Catch param as pattern with default is not valid syntax", "Destructuring declarations cannot have an initializer");
  39. }
  40. },
  41. {
  42. name: "Destructuring syntax as params - redeclarations",
  43. body: function () {
  44. assert.throws(function () { eval("function foo() {try {} catch({x:x, x:x}) {} }"); }, SyntaxError, "Catch param as object pattern has duplicate binding identifiers is not valid syntax", "Let/Const redeclaration");
  45. assert.throws(function () { eval("function foo() {try {} catch([x, x]) {} }"); }, SyntaxError, "Catch param as array pattern has duplicate binding identifiers is not valid syntax", "Let/Const redeclaration");
  46. assert.throws(function () { eval("function foo() {try {} catch({z1, x:{z:[z1]}}) {} }"); }, SyntaxError, "Catch param has nesting pattern has has matching is not valid syntax", "Let/Const redeclaration");
  47. assert.throws(function () { eval("function foo() {try {} catch([x]) { let x = 10;} }"); }, SyntaxError, "Catch param as a pattern and matching name with let/const variable in body is not valid syntax", "Let/Const redeclaration");
  48. assert.throws(function () { eval("function foo() {try {} catch([x]) { function x() {} } }"); }, SyntaxError, "Catch param as a pattern and matching name with function name in body is not valid syntax", "Let/Const redeclaration");
  49. assert.doesNotThrow(function () { eval("function foo() {try {} catch([x]) { var x = 10;} }"); }, "Catch param as a pattern and matching name with var declared name in body is valid syntax");
  50. (function () {
  51. try {
  52. } catch ({x}) {
  53. var x = 1;
  54. }
  55. assert.areEqual(x, undefined, "Assignment inside the catch block should assign the value to the catch param not the body var");
  56. })();
  57. (function () {
  58. let y = 1;
  59. try {
  60. throw { y : 10 };
  61. } catch ({y}) {
  62. assert.areEqual(y, 10, "Catch block refers to the destructured param");
  63. }
  64. assert.areEqual(y, 1, "Function body refers to the let variable");
  65. })();
  66. (function () {
  67. let x = 1;
  68. try {
  69. throw [{ x : 10 }];
  70. } catch ([{
  71. x
  72. }]) {
  73. assert.areEqual(x, 10, "Catch block with nested destructured param");
  74. }
  75. assert.areEqual(x, 1, "Let declaration in the function body is not affected by the destructured params");
  76. })();
  77. }
  78. },
  79. {
  80. name: "Destructuring on catch param - basic functionality",
  81. body: function () {
  82. try {
  83. throw [1];
  84. }
  85. catch ([e1]) {
  86. assert.areEqual(e1, 1, "Array pattern as a catch param matches with actual exception and initializes the identifier correctly");
  87. }
  88. try {
  89. throw {e2:2};
  90. }
  91. catch({e2}) {
  92. assert.areEqual(e2, 2, "Object pattern as a catch param matches with actual exception and initializes the identifier correctly");
  93. }
  94. try {
  95. throw [3, {e4:[4]}];
  96. }
  97. catch([e3, {e4:[e4]}]) {
  98. assert.areEqual(e3, 3, "First identifier in catch param as pattern is matched and initialized correctly");
  99. assert.areEqual(e4, 4, "Second identifier in catch param as pattern is matched and initialized correctly");
  100. }
  101. }
  102. },
  103. {
  104. name: "Destructuring on catch param - initializer",
  105. body: function () {
  106. try {
  107. throw [];
  108. }
  109. catch ([e1 = 11]) {
  110. assert.areEqual(e1, 11, "Array pattern as a catch param has initializer and initializes with initializer value");
  111. }
  112. try {
  113. throw {};
  114. }
  115. catch({e2:e2 = 22}) {
  116. assert.areEqual(e2, 22, "Object pattern as a catch param has initializer and initializes with initializer value");
  117. }
  118. try {
  119. throw [, {e4:[]}];
  120. }
  121. catch([e3 = 11, {e4:[e4 = 22]} = {e4:[]}]) {
  122. assert.areEqual(e3, 11, "First identifier in catch params as a pattern is initialized with initializer value");
  123. assert.areEqual(e4, 22, "Second identifier in catch params as a pattern is initialized with initializer value");
  124. }
  125. }
  126. },
  127. {
  128. name: "Destructuring on catch param - captures",
  129. body : function () {
  130. (function () {
  131. try {
  132. throw {x1:'x1', x2:'x2', x3:'x3'};
  133. }
  134. catch ({x1, x2, x3}) {
  135. (function () {
  136. x1;x2;x3;
  137. })();
  138. let m = x1+x2+x3;
  139. assert.areEqual(m, 'x1x2x3', "Inner Function - capturing all identifiers from object pattern in inner function is working correctly");
  140. }
  141. })();
  142. (function () {
  143. try {
  144. throw ['y1', 'y2', 'y3'];
  145. }
  146. catch ([x1, x2, x3]) {
  147. (function () {
  148. x1;x2;x3;
  149. })();
  150. let m = x1+x2+x3;
  151. assert.areEqual(m, 'y1y2y3', "Inner Function - capturing all identifiers from array pattern in inner function is working correctly");
  152. }
  153. })();
  154. (function () {
  155. try {
  156. throw ['y1', 'y2', 'y3'];
  157. }
  158. catch ([x1, x2, x3]) {
  159. (function () {
  160. x2;
  161. })();
  162. let m = x1+x2+x3;
  163. assert.areEqual(m, 'y1y2y3', "Inner Function - capturing only one identifier from pattern in inner function is working correctly");
  164. }
  165. })();
  166. (function () {
  167. try {
  168. throw ['y1', 'y2', 'y3'];
  169. }
  170. catch ([x1, x2, x3]) {
  171. eval('');
  172. let m = x1+x2+x3;
  173. assert.areEqual(m, 'y1y2y3', "Has eval - identifiers from catch params are initialized correctly");
  174. }
  175. })();
  176. (function () {
  177. try {
  178. throw ['y1', 'y2', 'y3'];
  179. }
  180. catch ([x1, x2, x3]) {
  181. (function () {
  182. x1;x2;x3;
  183. })();
  184. eval('');
  185. let m = x1+x2+x3;
  186. assert.areEqual(m, 'y1y2y3', "Has eval and inner function - identifiers from catch params are initialized correctly");
  187. }
  188. })();
  189. (function () {
  190. try {
  191. throw ['y1', 'y2', 'y3'];
  192. }
  193. catch ([x1, x2, x3]) {
  194. (function () {
  195. eval('');
  196. x1;x2;x3;
  197. })();
  198. let m = x1+x2+x3;
  199. assert.areEqual(m, 'y1y2y3', "Inner function has eval - identifiers from catch params are initialized correctly");
  200. }
  201. })();
  202. }
  203. },
  204. {
  205. name: "Function definitions in catch's parameter",
  206. body: function () {
  207. (function() {
  208. try {
  209. var c = 10;
  210. throw ['inside'];
  211. } catch ([x, y = function() { return c; }]) {
  212. assert.areEqual(y(), 10, "Function should be able to capture symbols from try's body properly");
  213. assert.areEqual(x, 'inside', "Function should be able to capture symbols from try's body properly");
  214. }
  215. })();
  216. (function() {
  217. try {
  218. throw [];
  219. } catch ([x = 10, y = function() { return x; }]) {
  220. assert.areEqual(y(), 10, "Function should be able to capture symbols from catch's param");
  221. }
  222. })();
  223. (function() {
  224. try {
  225. throw [];
  226. } catch ([x = 10, y = function() { return x; }]) {
  227. eval("");
  228. assert.areEqual(y(), 10, "Function should be able to capture symbols from catch's param");
  229. }
  230. })();
  231. (function() {
  232. try {
  233. throw {};
  234. } catch ({x = 10, y = function() { return x; }}) {
  235. assert.areEqual(y(), 10, "Function should be able to capture symbols from catch's param");
  236. }
  237. })();
  238. (function() {
  239. try {
  240. throw ['inside', {}];
  241. } catch ([x = 10, { y = function() { return x; } }]) {
  242. eval("");
  243. assert.areEqual(y(), 'inside', "Function should be able to capture symbols from catch's param");
  244. }
  245. })();
  246. (function() {
  247. try {
  248. throw ['inside', {}];
  249. } catch ([x, { y = () => arguments[0] }]) {
  250. assert.areEqual(y(), 10, "Function should be able to capture the arguments symbol from the parent function");
  251. assert.areEqual(x, 'inside', "Function should be able to capture symbols from try's body properly");
  252. }
  253. })(10);
  254. (function(a = 1, b = () => a) {
  255. try {
  256. throw [];
  257. } catch ([x = 10, y = function() { return b; }]) {
  258. assert.areEqual(y()(), 1, "Function should be able to capture formals from a split scoped function");
  259. }
  260. })();
  261. (function () {
  262. var z = 100;
  263. (function() {
  264. try {
  265. throw [];
  266. } catch ([x = 10, y = () => x + z]) {
  267. assert.areEqual(y(), 110, "Function should be able to capture symbols from outer functions");
  268. }
  269. })();
  270. })();
  271. (function () {
  272. var z = 100;
  273. (function() {
  274. try {
  275. throw [];
  276. } catch ([x = z = 10, y = () => x]) {
  277. assert.areEqual(y(), 10, "Function should be able to capture symbols from outer functions");
  278. assert.areEqual(z, 10, "Variable from the outer function is updated during the param initialization");
  279. }
  280. })();
  281. })();
  282. (function () {
  283. var a = 100;
  284. (function() {
  285. var b = 200;
  286. try {
  287. throw [];
  288. } catch ([x = () => y, y = 10, z = () => a]) {
  289. c = () => x() + z() + b;
  290. assert.areEqual(c(), 310, "Variable from all three levels are accessible");
  291. }
  292. })();
  293. })();
  294. (function () {
  295. var a = 100;
  296. (function() {
  297. var b = 200;
  298. try {
  299. throw [];
  300. } catch ([x = () => y, y = 10, z = () => a]) {
  301. c = () => x() + z() + b;
  302. assert.areEqual(c(), 310, "Variable from all three levels are accessible with eval in catch's body");
  303. eval("");
  304. }
  305. })();
  306. })();
  307. (function () {
  308. try {
  309. var c = 10;
  310. throw [ ];
  311. } catch ([x = 1, y = function() { eval(""); return c + x; }]) {
  312. assert.areEqual(y(), 11, "Function should be able to capture symbols from outer functions even with eval in the body");
  313. }
  314. })();
  315. (function () {
  316. try {
  317. eval("");
  318. var c = 10;
  319. throw [ ];
  320. } catch ([x = 1, y = function() { return c + x; }]) {
  321. assert.areEqual(y(), 11, "Function should be able to capture symbols from outer functions even with eval in the try block");
  322. }
  323. })();
  324. (function () {
  325. try {
  326. var c = 10;
  327. throw {x : 'inside', y: []};
  328. } catch ({x, y: [y = function(a = 10, b = () => a) { return b; }]}) {
  329. assert.areEqual(y()(), 10, "Function should be able to capture symbols from outer functions even if it has split scope");
  330. }
  331. })();
  332. (function () {
  333. var f = function foo(a) {
  334. try {
  335. if (!a) {
  336. return foo(1);
  337. }
  338. var c = 10;
  339. throw [ ];
  340. } catch ([y = function() { return c + a; }]) {
  341. assert.areEqual(y(), 11, "Function should be able to capture symbols from outer functions when inside a named function expression");
  342. }
  343. };
  344. f();
  345. })();
  346. }
  347. }
  348. ];
  349. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });