Skip to content

Commit 41baf41

Browse files
committed
Add tests
1 parent 76b9a63 commit 41baf41

File tree

5 files changed

+877
-0
lines changed

5 files changed

+877
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(7,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
2+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(8,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
3+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(9,18): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
4+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(20,22): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
5+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(21,22): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
6+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(22,22): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
7+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(30,30): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
8+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(39,22): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
9+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(43,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
10+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(44,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
11+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(45,18): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
12+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(59,27): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
13+
tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(75,27): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
14+
15+
16+
==== tests/cases/compiler/checkSuperCallBeforeThisAccess.ts (13 errors) ====
17+
class A {
18+
x = 1;
19+
}
20+
21+
class C1 extends A {
22+
constructor(n: number) {
23+
let a1 = this; // Error
24+
~~~~
25+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
26+
let a2 = this.x; // Error
27+
~~~~
28+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
29+
let a3 = super.x; // Error
30+
~~~~~
31+
!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
32+
let a4 = () => this;
33+
let a5 = () => this.x;
34+
let a6 = () => super.x;
35+
if (!!true) {
36+
super();
37+
let b1 = this;
38+
let b2 = this.x;
39+
let b3 = super.x;
40+
}
41+
else {
42+
let c1 = this; // Error
43+
~~~~
44+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
45+
let c2 = this.x; // Error
46+
~~~~
47+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
48+
let c3 = super.x; // Error
49+
~~~~~
50+
!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
51+
}
52+
if (!!true) {
53+
switch (n) {
54+
case 1:
55+
super();
56+
let d1 = this.x;
57+
case 2:
58+
let d2 = this.x; // Error
59+
~~~~
60+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
61+
default:
62+
super();
63+
let d3 = this.x;
64+
}
65+
let d4 = this.x;
66+
}
67+
if (!!true) {
68+
let e1 = { w: !!true ? super() : 0 };
69+
let e2 = this.x; // Error
70+
~~~~
71+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
72+
let e3 = { w: !!true ? super() : super() };
73+
let e4 = this.x;
74+
}
75+
let f1 = this; // Error
76+
~~~~
77+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
78+
let f2 = this.x; // Error
79+
~~~~
80+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
81+
let f3 = super.x; // Error
82+
~~~~~
83+
!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
84+
}
85+
}
86+
87+
// Repro from #38512
88+
89+
export class Foo {
90+
constructor(value: number) {
91+
}
92+
}
93+
94+
export class BarCorrectlyFails extends Foo {
95+
constructor(something: boolean) {
96+
if (!something) {
97+
const value = this.bar(); // Error
98+
~~~~
99+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
100+
super(value);
101+
}
102+
else {
103+
super(1337);
104+
}
105+
}
106+
bar(): number { return 4; }
107+
}
108+
109+
export class BarIncorrectlyWorks extends Foo {
110+
constructor(something: boolean) {
111+
if (something) {
112+
super(1337);
113+
}
114+
else {
115+
const value = this.bar(); // Error
116+
~~~~
117+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
118+
super(value);
119+
}
120+
}
121+
bar(): number { return 4; }
122+
}
123+
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
//// [checkSuperCallBeforeThisAccess.ts]
2+
class A {
3+
x = 1;
4+
}
5+
6+
class C1 extends A {
7+
constructor(n: number) {
8+
let a1 = this; // Error
9+
let a2 = this.x; // Error
10+
let a3 = super.x; // Error
11+
let a4 = () => this;
12+
let a5 = () => this.x;
13+
let a6 = () => super.x;
14+
if (!!true) {
15+
super();
16+
let b1 = this;
17+
let b2 = this.x;
18+
let b3 = super.x;
19+
}
20+
else {
21+
let c1 = this; // Error
22+
let c2 = this.x; // Error
23+
let c3 = super.x; // Error
24+
}
25+
if (!!true) {
26+
switch (n) {
27+
case 1:
28+
super();
29+
let d1 = this.x;
30+
case 2:
31+
let d2 = this.x; // Error
32+
default:
33+
super();
34+
let d3 = this.x;
35+
}
36+
let d4 = this.x;
37+
}
38+
if (!!true) {
39+
let e1 = { w: !!true ? super() : 0 };
40+
let e2 = this.x; // Error
41+
let e3 = { w: !!true ? super() : super() };
42+
let e4 = this.x;
43+
}
44+
let f1 = this; // Error
45+
let f2 = this.x; // Error
46+
let f3 = super.x; // Error
47+
}
48+
}
49+
50+
// Repro from #38512
51+
52+
export class Foo {
53+
constructor(value: number) {
54+
}
55+
}
56+
57+
export class BarCorrectlyFails extends Foo {
58+
constructor(something: boolean) {
59+
if (!something) {
60+
const value = this.bar(); // Error
61+
super(value);
62+
}
63+
else {
64+
super(1337);
65+
}
66+
}
67+
bar(): number { return 4; }
68+
}
69+
70+
export class BarIncorrectlyWorks extends Foo {
71+
constructor(something: boolean) {
72+
if (something) {
73+
super(1337);
74+
}
75+
else {
76+
const value = this.bar(); // Error
77+
super(value);
78+
}
79+
}
80+
bar(): number { return 4; }
81+
}
82+
83+
84+
//// [checkSuperCallBeforeThisAccess.js]
85+
class A {
86+
constructor() {
87+
this.x = 1;
88+
}
89+
}
90+
class C1 extends A {
91+
constructor(n) {
92+
let a1 = this; // Error
93+
let a2 = this.x; // Error
94+
let a3 = super.x; // Error
95+
let a4 = () => this;
96+
let a5 = () => this.x;
97+
let a6 = () => super.x;
98+
if (!!true) {
99+
super();
100+
let b1 = this;
101+
let b2 = this.x;
102+
let b3 = super.x;
103+
}
104+
else {
105+
let c1 = this; // Error
106+
let c2 = this.x; // Error
107+
let c3 = super.x; // Error
108+
}
109+
if (!!true) {
110+
switch (n) {
111+
case 1:
112+
super();
113+
let d1 = this.x;
114+
case 2:
115+
let d2 = this.x; // Error
116+
default:
117+
super();
118+
let d3 = this.x;
119+
}
120+
let d4 = this.x;
121+
}
122+
if (!!true) {
123+
let e1 = { w: !!true ? super() : 0 };
124+
let e2 = this.x; // Error
125+
let e3 = { w: !!true ? super() : super() };
126+
let e4 = this.x;
127+
}
128+
let f1 = this; // Error
129+
let f2 = this.x; // Error
130+
let f3 = super.x; // Error
131+
}
132+
}
133+
// Repro from #38512
134+
export class Foo {
135+
constructor(value) {
136+
}
137+
}
138+
export class BarCorrectlyFails extends Foo {
139+
constructor(something) {
140+
if (!something) {
141+
const value = this.bar(); // Error
142+
super(value);
143+
}
144+
else {
145+
super(1337);
146+
}
147+
}
148+
bar() { return 4; }
149+
}
150+
export class BarIncorrectlyWorks extends Foo {
151+
constructor(something) {
152+
if (something) {
153+
super(1337);
154+
}
155+
else {
156+
const value = this.bar(); // Error
157+
super(value);
158+
}
159+
}
160+
bar() { return 4; }
161+
}

0 commit comments

Comments
 (0)