class-public-fields.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. test("basic functionality", () => {
  2. class A {
  3. number = 3;
  4. string = "foo";
  5. uninitialized;
  6. }
  7. const a = new A();
  8. expect(a.number).toBe(3);
  9. expect(a.string).toBe("foo");
  10. expect(a.uninitialized).toBeUndefined();
  11. });
  12. test("extended name syntax", () => {
  13. class A {
  14. "field with space" = 1;
  15. 12 = "twelve";
  16. [`he${"llo"}`] = 3;
  17. }
  18. const a = new A();
  19. expect(a["field with space"]).toBe(1);
  20. expect(a[12]).toBe("twelve");
  21. expect(a.hello).toBe(3);
  22. });
  23. test("initializer has correct this value", () => {
  24. class A {
  25. this_val = this;
  26. this_name = this.this_val;
  27. }
  28. const a = new A();
  29. expect(a.this_val).toBe(a);
  30. expect(a.this_name).toBe(a);
  31. });
  32. test("static fields", () => {
  33. class A {
  34. static simple = 1;
  35. simple = 2;
  36. static "with space" = 3;
  37. static 24 = "two dozen";
  38. static [`he${"llo"}`] = "friends";
  39. static this_val = this;
  40. static this_name = this.name;
  41. static this_val2 = this.this_val;
  42. }
  43. expect(A.simple).toBe(1);
  44. expect(A["with space"]).toBe(3);
  45. expect(A[24]).toBe("two dozen");
  46. expect(A.hello).toBe("friends");
  47. expect(A.this_val).toBe(A);
  48. expect(A.this_name).toBe("A");
  49. expect(A.this_val2).toBe(A);
  50. const a = new A();
  51. expect(a.simple).toBe(2);
  52. });
  53. test("with super class", () => {
  54. class A {
  55. super_field = 3;
  56. }
  57. class B extends A {
  58. references_super_field = super.super_field;
  59. arrow_ref_super = () => (super.super_field = 4);
  60. }
  61. const b = new B();
  62. expect(b.super_field).toBe(3);
  63. expect(b.references_super_field).toBeUndefined();
  64. b.arrow_ref_super();
  65. expect(b.super_field).toBe(4);
  66. });
  67. test("'arguments' is not allowed in class field initializer", () => {
  68. expect("class A { a = arguments; }").not.toEval();
  69. expect("class B { static b = arguments; }").not.toEval();
  70. class C {
  71. c = eval("arguments");
  72. }
  73. expect(() => {
  74. new C();
  75. }).toThrowWithMessage(SyntaxError, "'arguments' is not allowed in class field initializer");
  76. expect(() => {
  77. class D {
  78. static d = eval("arguments");
  79. }
  80. }).toThrowWithMessage(SyntaxError, "'arguments' is not allowed in class field initializer");
  81. });
  82. test("using 'arguments' via indirect eval throws at runtime instead of parse time", () => {
  83. const indirect = eval;
  84. class A {
  85. a = indirect("arguments");
  86. }
  87. expect(() => {
  88. new A();
  89. }).toThrowWithMessage(ReferenceError, "'arguments' is not defined");
  90. expect(() => {
  91. class B {
  92. static b = indirect("arguments");
  93. }
  94. }).toThrowWithMessage(ReferenceError, "'arguments' is not defined");
  95. });
  96. describe("class fields with a 'special' name", () => {
  97. test("static", () => {
  98. class A {
  99. static;
  100. }
  101. expect("static" in new A()).toBeTrue();
  102. class B {
  103. static;
  104. }
  105. expect("static" in new B()).toBeTrue();
  106. class C {
  107. static a;
  108. }
  109. expect("static" in new C()).toBeFalse();
  110. expect("a" in new C()).toBeFalse();
  111. expect("a" in C).toBeTrue();
  112. expect("static" in C).toBeFalse();
  113. });
  114. test("async", () => {
  115. class A {
  116. async;
  117. }
  118. expect("async" in new A()).toBeTrue();
  119. class B {
  120. async;
  121. }
  122. expect("async" in new B()).toBeTrue();
  123. class C {
  124. async;
  125. a;
  126. }
  127. expect("async" in new C()).toBeTrue();
  128. expect("a" in new C()).toBeTrue();
  129. });
  130. });