destructuring_catch.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. }
  58. },
  59. {
  60. name: "Destructuring on catch param - basic functionality",
  61. body: function () {
  62. try {
  63. throw [1];
  64. }
  65. catch ([e1]) {
  66. assert.areEqual(e1, 1, "Array pattern as a catch param matches with actual exception and initializes the identifier correctly");
  67. }
  68. try {
  69. throw {e2:2};
  70. }
  71. catch({e2}) {
  72. assert.areEqual(e2, 2, "Object pattern as a catch param matches with actual exception and initializes the identifier correctly");
  73. }
  74. try {
  75. throw [3, {e4:[4]}];
  76. }
  77. catch([e3, {e4:[e4]}]) {
  78. assert.areEqual(e3, 3, "First identifier in catch param as pattern is matched and initialized correctly");
  79. assert.areEqual(e4, 4, "Second identifier in catch param as pattern is matched and initialized correctly");
  80. }
  81. }
  82. },
  83. {
  84. name: "Destructuring on catch param - initializer",
  85. body: function () {
  86. try {
  87. throw [];
  88. }
  89. catch ([e1 = 11]) {
  90. assert.areEqual(e1, 11, "Array pattern as a catch param has initializer and initializes with initializer value");
  91. }
  92. try {
  93. throw {};
  94. }
  95. catch({e2:e2 = 22}) {
  96. assert.areEqual(e2, 22, "Object pattern as a catch param has initializer and initializes with initializer value");
  97. }
  98. try {
  99. throw [, {e4:[]}];
  100. }
  101. catch([e3 = 11, {e4:[e4 = 22]} = {e4:[]}]) {
  102. assert.areEqual(e3, 11, "First identifier in catch params as a pattern is initialized with initializer value");
  103. assert.areEqual(e4, 22, "Second identifier in catch params as a pattern is initialized with initializer value");
  104. }
  105. }
  106. },
  107. {
  108. name: "Destructuring on catch param - captures",
  109. body : function () {
  110. (function () {
  111. try {
  112. throw {x1:'x1', x2:'x2', x3:'x3'};
  113. }
  114. catch ({x1, x2, x3}) {
  115. (function () {
  116. x1;x2;x3;
  117. })();
  118. let m = x1+x2+x3;
  119. assert.areEqual(m, 'x1x2x3', "Inner Function - capturing all identifiers from object pattern in inner function is working correctly");
  120. }
  121. })();
  122. (function () {
  123. try {
  124. throw ['y1', 'y2', 'y3'];
  125. }
  126. catch ([x1, x2, x3]) {
  127. (function () {
  128. x1;x2;x3;
  129. })();
  130. let m = x1+x2+x3;
  131. assert.areEqual(m, 'y1y2y3', "Inner Function - capturing all identifiers from array pattern in inner function is working correctly");
  132. }
  133. })();
  134. (function () {
  135. try {
  136. throw ['y1', 'y2', 'y3'];
  137. }
  138. catch ([x1, x2, x3]) {
  139. (function () {
  140. x2;
  141. })();
  142. let m = x1+x2+x3;
  143. assert.areEqual(m, 'y1y2y3', "Inner Function - capturing only one identifier from pattern in inner function is working correctly");
  144. }
  145. })();
  146. (function () {
  147. try {
  148. throw ['y1', 'y2', 'y3'];
  149. }
  150. catch ([x1, x2, x3]) {
  151. eval('');
  152. let m = x1+x2+x3;
  153. assert.areEqual(m, 'y1y2y3', "Has eval - identifiers from catch params are initialized correctly");
  154. }
  155. })();
  156. (function () {
  157. try {
  158. throw ['y1', 'y2', 'y3'];
  159. }
  160. catch ([x1, x2, x3]) {
  161. (function () {
  162. x1;x2;x3;
  163. })();
  164. eval('');
  165. let m = x1+x2+x3;
  166. assert.areEqual(m, 'y1y2y3', "Has eval and inner function - identifiers from catch params are initialized correctly");
  167. }
  168. })();
  169. (function () {
  170. try {
  171. throw ['y1', 'y2', 'y3'];
  172. }
  173. catch ([x1, x2, x3]) {
  174. (function () {
  175. eval('');
  176. x1;x2;x3;
  177. })();
  178. let m = x1+x2+x3;
  179. assert.areEqual(m, 'y1y2y3', "Inner function has eval - identifiers from catch params are initialized correctly");
  180. }
  181. })();
  182. }
  183. },
  184. {
  185. name: "Function definitions in catch's parameter",
  186. body: function () {
  187. (function() {
  188. try {
  189. var c = 10;
  190. throw ['inside'];
  191. } catch ([x, y = function() { return c; }]) {
  192. assert.areEqual(y(), 10, "Function should be able to capture symbols from try's body properly");
  193. assert.areEqual(x, 'inside', "Function should be able to capture symbols from try's body properly");
  194. }
  195. })();
  196. (function() {
  197. try {
  198. throw [];
  199. } catch ([x = 10, y = function() { return x; }]) {
  200. assert.areEqual(y(), 10, "Function should be able to capture symbols from catch's param");
  201. }
  202. })();
  203. (function() {
  204. try {
  205. throw [];
  206. } catch ([x = 10, y = function() { return x; }]) {
  207. eval("");
  208. assert.areEqual(y(), 10, "Function should be able to capture symbols from catch's param");
  209. }
  210. })();
  211. (function() {
  212. try {
  213. throw {};
  214. } catch ({x = 10, y = function() { return x; }}) {
  215. assert.areEqual(y(), 10, "Function should be able to capture symbols from catch's param");
  216. }
  217. })();
  218. (function() {
  219. try {
  220. throw ['inside', {}];
  221. } catch ([x = 10, { y = function() { return x; } }]) {
  222. eval("");
  223. assert.areEqual(y(), 'inside', "Function should be able to capture symbols from catch's param");
  224. }
  225. })();
  226. (function() {
  227. try {
  228. throw ['inside', {}];
  229. } catch ([x, { y = () => arguments[0] }]) {
  230. assert.areEqual(y(), 10, "Function should be able to capture the arguments symbol from the parent function");
  231. assert.areEqual(x, 'inside', "Function should be able to capture symbols from try's body properly");
  232. }
  233. })(10);
  234. (function(a = 1, b = () => a) {
  235. try {
  236. throw [];
  237. } catch ([x = 10, y = function() { return b; }]) {
  238. assert.areEqual(y()(), 1, "Function should be able to capture formals from a split scoped function");
  239. }
  240. })();
  241. (function () {
  242. var z = 100;
  243. (function() {
  244. try {
  245. throw [];
  246. } catch ([x = 10, y = () => x + z]) {
  247. assert.areEqual(y(), 110, "Function should be able to capture symbols from outer functions");
  248. }
  249. })();
  250. })();
  251. (function () {
  252. var z = 100;
  253. (function() {
  254. try {
  255. throw [];
  256. } catch ([x = z = 10, y = () => x]) {
  257. assert.areEqual(y(), 10, "Function should be able to capture symbols from outer functions");
  258. assert.areEqual(z, 10, "Variable from the outer function is updated during the param initialization");
  259. }
  260. })();
  261. })();
  262. (function () {
  263. var a = 100;
  264. (function() {
  265. var b = 200;
  266. try {
  267. throw [];
  268. } catch ([x = () => y, y = 10, z = () => a]) {
  269. c = () => x() + z() + b;
  270. assert.areEqual(c(), 310, "Variable from all three levels are accessible");
  271. }
  272. })();
  273. })();
  274. (function () {
  275. var a = 100;
  276. (function() {
  277. var b = 200;
  278. try {
  279. throw [];
  280. } catch ([x = () => y, y = 10, z = () => a]) {
  281. c = () => x() + z() + b;
  282. assert.areEqual(c(), 310, "Variable from all three levels are accessible with eval in catch's body");
  283. eval("");
  284. }
  285. })();
  286. })();
  287. (function () {
  288. try {
  289. var c = 10;
  290. throw [ ];
  291. } catch ([x = 1, y = function() { eval(""); return c + x; }]) {
  292. assert.areEqual(y(), 11, "Function should be able to capture symbols from outer functions even with eval in the body");
  293. }
  294. })();
  295. (function () {
  296. try {
  297. eval("");
  298. var c = 10;
  299. throw [ ];
  300. } catch ([x = 1, y = function() { return c + x; }]) {
  301. assert.areEqual(y(), 11, "Function should be able to capture symbols from outer functions even with eval in the try block");
  302. }
  303. })();
  304. (function () {
  305. try {
  306. var c = 10;
  307. throw {x : 'inside', y: []};
  308. } catch ({x, y: [y = function(a = 10, b = () => a) { return b; }]}) {
  309. assert.areEqual(y()(), 10, "Function should be able to capture symbols from outer functions even if it has split scope");
  310. }
  311. })();
  312. (function () {
  313. var f = function foo(a) {
  314. try {
  315. if (!a) {
  316. return foo(1);
  317. }
  318. var c = 10;
  319. throw [ ];
  320. } catch ([y = function() { return c + a; }]) {
  321. assert.areEqual(y(), 11, "Function should be able to capture symbols from outer functions when inside a named function expression");
  322. }
  323. };
  324. f();
  325. })();
  326. }
  327. }
  328. ];
  329. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });