exportBinding.js 5.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. export class A1 {
  7. attemptBindingChange() { A1 = 1; }
  8. bindingUnmodified() { return A1 !== 1; }
  9. }
  10. var A1Inst = new A1();
  11. assert.throws(function () { A1Inst.attemptBindingChange(); }, TypeError, 'Exported class name captured inside class is const', "Assignment to const");
  12. assert.doesNotThrow(function () { A1 = 1; }, 'Exported class name decl binding is mutable outside class');
  13. assert.throws(function () { A1Inst.attemptBindingChange(); }, TypeError, 'Mutation of class decl did not change constness', "Assignment to const");
  14. assert.areEqual(true, A1Inst.bindingUnmodified(), 'Captured class name does not observe mutation');
  15. assert.throws(function () { A1Inst.attemptBindingChange(); }, TypeError, 'Exported class name captured inside class is still const', "Assignment to const");
  16. class A2 {
  17. attemptBindingChange() { A2 = 1; }
  18. bindingUnmodified() { return A2 !== 1; }
  19. }
  20. var A2Inst = new A2();
  21. export { A2 };
  22. assert.throws(function () { A2Inst.attemptBindingChange(); }, TypeError, 'Delay exported class name captured in class is const', "Assignment to const");
  23. assert.doesNotThrow(function () { A2 = 1; }, 'Delay exported class name decl binding is mutable outside class');
  24. assert.throws(function () { A2Inst.attemptBindingChange(); }, TypeError, 'Mutation of class decl did not change constness', "Assignment to const");
  25. assert.areEqual(true, A2Inst.bindingUnmodified(), 'Captured class name does not observe mutation');
  26. assert.throws(function () { A2Inst.attemptBindingChange(); }, TypeError, 'Delay exported class name captured in class is still const', "Assignment to const");
  27. export let B1 = class B1 {
  28. attemptBindingChange() { B1 = 1; }
  29. bindingUnmodified() { return B1 !== 1; }
  30. }
  31. var B1Inst = new B1();
  32. assert.throws(function () { B1Inst.attemptBindingChange(); }, TypeError, 'Exported class expr name captured inside class is const', "Assignment to const");
  33. assert.doesNotThrow(function () { B1 = 1; }, 'Exported class expr name decl binding is mutable outside class');
  34. assert.throws(function () { B1Inst.attemptBindingChange(); }, TypeError, 'Mutation of class expr decl did not change constness', "Assignment to const");
  35. assert.areEqual(true, B1Inst.bindingUnmodified(), 'Captured class name does not observe mutation');
  36. assert.throws(function () { B1Inst.attemptBindingChange(); }, TypeError, 'Exported class expr name captured inside class is still const', "Assignment to const");
  37. let B2 = class B2 {
  38. attemptBindingChange() { B2 = 1; }
  39. bindingUnmodified() { return B2 !== 1; }
  40. }
  41. var B2Inst = new B2();
  42. export { B2 };
  43. assert.throws(function () { B2Inst.attemptBindingChange(); }, TypeError, 'Delay exported class expr name captured in class is const', "Assignment to const");
  44. assert.doesNotThrow(function () { B2 = 1; }, 'Delay exported class expr name decl binding is mutable outside class');
  45. assert.throws(function () { B2Inst.attemptBindingChange(); }, TypeError, 'Mutation of class expr decl did not change constness', "Assignment to const");
  46. assert.areEqual(true, B2Inst.bindingUnmodified(), 'Captured class name does not observe mutation');
  47. assert.throws(function () { B2Inst.attemptBindingChange(); }, TypeError, 'Delay exported class expr name captured in class is still const', "Assignment to const");
  48. export let C1 = class NotC1 {
  49. attemptOuterBindingChange() { C1 = 1; }
  50. attemptInnerBindingChange() { NotC1 = 1; }
  51. outerbindingUnmodified() { return C1 !== 1; }
  52. innerbindingUnmodified() { return NotC1 !== 1; }
  53. }
  54. var C1Inst = new C1();
  55. assert.throws(function () { C1Inst.attemptInnerBindingChange(); }, TypeError, 'Exported class expr name captured inside class is const', "Assignment to const");
  56. assert.doesNotThrow(function () { C1Inst.attemptOuterBindingChange(); }, 'Exported class expr name decl binding is mutable outside class');
  57. assert.throws(function () { C1Inst.attemptInnerBindingChange(); }, TypeError, 'Mutation of class expr decl did not change constness', "Assignment to const");
  58. assert.areEqual(false, C1Inst.outerbindingUnmodified(), 'Captured class decl observes mutation');
  59. assert.areEqual(true, C1Inst.innerbindingUnmodified(), 'Captured class name does not observe mutation');
  60. assert.throws(function () { C1Inst.attemptInnerBindingChange(); }, TypeError, 'Exported class expr name captured inside class is still const', "Assignment to const");
  61. let C2 = class NotC2 {
  62. attemptOuterBindingChange() { C2 = 1; }
  63. attemptInnerBindingChange() { NotC2 = 1; }
  64. outerbindingUnmodified() { return C2 !== 1; }
  65. innerbindingUnmodified() { return NotC2 !== 1; }
  66. }
  67. var C2Inst = new C2();
  68. export { C2 };
  69. assert.throws(function () { C2Inst.attemptInnerBindingChange(); }, TypeError, 'Exported class expr name captured inside class is const', "Assignment to const");
  70. assert.doesNotThrow(function () { C2Inst.attemptOuterBindingChange(); }, 'Exported class expr name decl binding is mutable outside class');
  71. assert.throws(function () { C2Inst.attemptInnerBindingChange(); }, TypeError, 'Mutation of class expr decl did not change constness', "Assignment to const");
  72. assert.areEqual(false, C2Inst.outerbindingUnmodified(), 'Captured class decl observes mutation');
  73. assert.areEqual(true, C2Inst.innerbindingUnmodified(), 'Captured class name does not observe mutation');
  74. assert.throws(function () { C2Inst.attemptInnerBindingChange(); }, TypeError, 'Exported class expr name captured inside class is still const', "Assignment to const");
  75. console.log("PASS");