Skip to content

noUnreachableSuper

诊断类别:lint/correctness/noUnreachableSuper

¥Diagnostic Category: lint/correctness/noUnreachableSuper

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

如果类具有超类,则确保在访问 this 之前,在类构造函数中的每个代码路径上都只调用一次 super() 构造函数

¥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() {}
}
code-block.js:2:5 lint/correctness/noUnreachableSuper ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This constructor has code paths that return without calling </span><span style="color: Tomato;"><strong>super()</strong></span><span style="color: Tomato;">.

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();
}
}
code-block.js:2:5 lint/correctness/noUnreachableSuper ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This constructor has code paths accessing </span><span style="color: Tomato;"><strong>this</strong></span><span style="color: Tomato;"> without calling </span><span style="color: Tomato;"><strong>super()</strong></span><span style="color: Tomato;"> first.

1 │ class A extends B {
> 2 │ constructor(value) {
^^^^^^^^^^^^^^^^^^^^
> 3 │ this.prop = value;
> 4 │ super();
> 5 │ }
^
6 │ }
7 │

</span><span style="color: lightgreen;"><strong>this</strong></span><span style="color: lightgreen;"> is accessed here:

1 │ class A extends B {
2 │ constructor(value) {
> 3 │ this.prop = value;
^^^^
4 │ super();
5 │ }

If this is intentional, add an explicit throw statement in unsupported paths.

class A extends B {
constructor(cond) {
if(cond) {
super();
}
}
}
code-block.js:2:5 lint/correctness/noUnreachableSuper ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This constructor has code paths that return without calling </span><span style="color: Tomato;"><strong>super()</strong></span><span style="color: Tomato;">.

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() {}
}

¥Related links