noUnreachableSuper (since v12.0.0)
Ensures the super()
constructor is called exactly once on every code path in a class constructor before this
is accessed if the class has a superclass
Examples
Invalid
class A extends B {
constructor() {}
}
nursery/noUnreachableSuper.js:2:5 lint/nursery/noUnreachableSuper ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This constructor has code paths that return without calling `super()`.
1 │ class A extends B {
> 2 │ constructor() {}
│ ^^^^^^^^^^^^^^^^
3 │ }
4 │
ℹ If this is intentional, add an explicit throw statement in unsupported paths.
class A extends B {
constructor(value) {
this.prop = value;
super();
}
}
nursery/noUnreachableSuper.js:2:5 lint/nursery/noUnreachableSuper ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This constructor has code paths accessing `this` before `super()` is called.
1 │ class A extends B {
> 2 │ constructor(value) {
│ ^^^^^^^^^^^^^^^^^^^^
> 3 │ this.prop = value;
> 4 │ super();
> 5 │ }
│ ^
6 │ }
7 │
ℹ `this` is accessed here:
1 │ class A extends B {
2 │ constructor(value) {
> 3 │ this.prop = value;
│ ^^^^
4 │ super();
5 │ }
ℹ `super()` is only called here:
2 │ constructor(value) {
3 │ this.prop = value;
> 4 │ super();
│ ^^^^^
5 │ }
6 │ }
ℹ If this is intentional, add an explicit throw statement in unsupported paths.
class A extends B {
constructor(cond) {
if(cond) {
super();
}
}
}
nursery/noUnreachableSuper.js:2:5 lint/nursery/noUnreachableSuper ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This constructor has code paths that return without calling `super()`.
1 │ class A extends B {
> 2 │ constructor(cond) {
│ ^^^^^^^^^^^^^^^^^^^
> 3 │ if(cond) {
> 4 │ super();
> 5 │ }
> 6 │ }
│ ^
7 │ }
8 │
ℹ If this is intentional, add an explicit throw statement in unsupported paths.
Valid
export default class A extends B {
constructor() {
super();
}
}
export class A {
constructor() {}
}