DeclOutofBlock.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // Tests for ERRDeclOutOfBlock "Const and let must be declared inside of block"
  6. var a = 1;
  7. (function () {
  8. try { eval(
  9. "if (a) \
  10. let b = 5;" // error
  11. );} catch (e) { WScript.Echo(e); }
  12. try { eval(
  13. "if (a) \
  14. const b = 5;" // error
  15. );} catch (e) { WScript.Echo(e); }
  16. })();
  17. (function () {
  18. try { eval(
  19. "if (a) { \
  20. let c = 3; /* no error */ \
  21. const x = 42; /* no error */ \
  22. }"
  23. );} catch (e) { WScript.Echo(e); }
  24. })();
  25. (function () {
  26. try { eval(
  27. "while (a) \
  28. let d = 5;" // error
  29. );} catch (e) { WScript.Echo(e); }
  30. try { eval(
  31. "while (a) \
  32. let d = 5;" // error
  33. );} catch (e) { WScript.Echo(e); }
  34. })();
  35. (function () {
  36. try { eval(
  37. "while (a) { \
  38. let e = 10; /* no error */ \
  39. const y = 10; /* no error */ \
  40. break; \
  41. }"
  42. );} catch (e) { WScript.Echo(e); }
  43. })();
  44. (function () {
  45. try { eval(
  46. "if (a) \
  47. while (a) \
  48. if (a) { \
  49. let x = 3; /* no error */ \
  50. const z = x; /* no error */ \
  51. while (a) \
  52. let f = 5; /* error */ \
  53. }"
  54. );} catch (e) { WScript.Echo(e); }
  55. })();
  56. function test() {
  57. if (a)
  58. for (let x in [1]) { /* no error */
  59. break;
  60. };
  61. for (var y in [1])
  62. for (let x in [1]) { /* no error */
  63. break;
  64. };
  65. do
  66. for (let x in [1]) { /* no error */
  67. break;
  68. }
  69. while (!a);
  70. if (a)
  71. for (let x = 0; x < 1; x++) { /* no error */
  72. break;
  73. };
  74. for (var y in [1])
  75. for (let x = 0; x < 1; x++) { /* no error */
  76. break;
  77. };
  78. do
  79. for (let x = 0; x < 1; x++) { /* no error */
  80. break;
  81. }
  82. while (!a);
  83. WScript.Echo('success');
  84. };
  85. test();