destructuring_catch.js 16 KB

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