]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Disallow use of `this`/`super` before calling `super()` in constructors. (no-this-before-super) |
2 | ||
3 | In the constructor of derived classes, if `this`/`super` are used before `super()` calls, it raises a reference error. | |
4 | ||
5 | This rule checks `this`/`super` keywords in constructors, then reports those that are before `super()`. | |
6 | ||
7 | ## Rule Details | |
8 | ||
9 | This rule is aimed to flag `this`/`super` keywords before `super()` callings. | |
10 | ||
11 | ## Examples | |
12 | ||
13 | Examples of **incorrect** code for this rule: | |
14 | ||
15 | ```js | |
16 | /*eslint no-this-before-super: "error"*/ | |
17 | /*eslint-env es6*/ | |
18 | ||
19 | class A extends B { | |
20 | constructor() { | |
21 | this.a = 0; | |
22 | super(); | |
23 | } | |
24 | } | |
25 | ||
26 | class A extends B { | |
27 | constructor() { | |
28 | this.foo(); | |
29 | super(); | |
30 | } | |
31 | } | |
32 | ||
33 | class A extends B { | |
34 | constructor() { | |
35 | super.foo(); | |
36 | super(); | |
37 | } | |
38 | } | |
39 | ||
40 | class A extends B { | |
41 | constructor() { | |
42 | super(this.foo()); | |
43 | } | |
44 | } | |
45 | ``` | |
46 | ||
47 | Examples of **correct** code for this rule: | |
48 | ||
49 | ```js | |
50 | /*eslint no-this-before-super: "error"*/ | |
51 | /*eslint-env es6*/ | |
52 | ||
53 | class A { | |
54 | constructor() { | |
55 | this.a = 0; // OK, this class doesn't have an `extends` clause. | |
56 | } | |
57 | } | |
58 | ||
59 | class A extends B { | |
60 | constructor() { | |
61 | super(); | |
62 | this.a = 0; // OK, this is after `super()`. | |
63 | } | |
64 | } | |
65 | ||
66 | class A extends B { | |
67 | foo() { | |
68 | this.a = 0; // OK. this is not in a constructor. | |
69 | } | |
70 | } | |
71 | ``` | |
72 | ||
73 | ## When Not To Use It | |
74 | ||
75 | If you don't want to be notified about using `this`/`super` before `super()` in constructors, you can safely disable this rule. |