class-errors.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. describe("non-syntax errors", () => {
  2. test("super reference inside nested-but-same |this| scope with no base class", () => {
  3. expect(`
  4. class A {
  5. foo() {
  6. () => { super.bar; }
  7. }
  8. }`).toEval();
  9. });
  10. test("super reference property lookup with no base class", () => {
  11. expect(`
  12. class A {
  13. constructor() {
  14. super.foo;
  15. }
  16. }`).toEval();
  17. });
  18. });
  19. describe("reference errors", () => {
  20. test("derived class doesn't call super in constructor before using this", () => {
  21. class Parent {}
  22. class Child extends Parent {
  23. constructor() {
  24. this;
  25. }
  26. }
  27. expect(() => {
  28. new Child();
  29. }).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
  30. });
  31. test("derived class calls super twice in constructor", () => {
  32. class Parent {}
  33. class Child extends Parent {
  34. constructor() {
  35. super();
  36. super();
  37. }
  38. }
  39. expect(() => {
  40. new Child();
  41. }).toThrowWithMessage(ReferenceError, "|this| is already initialized");
  42. });
  43. });
  44. describe("syntax errors", () => {
  45. test("getter with argument", () => {
  46. expect(`
  47. class A {
  48. get foo(v) {
  49. return 0;
  50. }
  51. }`).not.toEval();
  52. });
  53. test("setter with no arguments", () => {
  54. expect(`
  55. class A {
  56. set foo() {
  57. }
  58. }`).not.toEval();
  59. });
  60. test("setter with more than one argument", () => {
  61. expect(`
  62. class A {
  63. set foo(bar, baz) {
  64. }
  65. }`).not.toEval();
  66. expect(`
  67. class A {
  68. set foo(...bar) {
  69. }
  70. }`).not.toEval();
  71. });
  72. test("super reference inside different |this| scope", () => {
  73. expect(`
  74. class Parent {}
  75. class Child extends Parent {
  76. foo() {
  77. function f() { super.foo; }
  78. }
  79. }`).not.toEval();
  80. });
  81. test("super reference call with no base class", () => {
  82. expect(`
  83. class A {
  84. constructor() {
  85. super();
  86. }
  87. }`).not.toEval();
  88. });
  89. });