noUselessConstructor
诊断类别:lint/complexity/noUselessConstructor
¥Diagnostic Category: lint/complexity/noUselessConstructor
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
-
与以下相同:
no-useless-constructor
¥Same as:
no-useless-constructor
禁止不必要的构造函数。
¥Disallow unnecessary constructors.
如果未指定,ES2015 提供默认类构造函数。因此,提供空构造函数或委托给其父级的构造函数是不必要的。
¥ES2015 provides a default class constructor if one is not specified. As such, providing an empty constructor or one that delegates into its parent is unnecessary.
规则忽略:
¥The rule ignores:
-
装饰类;
¥decorated classes;
-
至少有一个 参数属性 的构造函数;
¥constructors with at least one parameter property;
-
private
和protected
构造函数。¥
private
andprotected
constructors.
¥Caveat
此规则报告唯一目的是使父构造函数公开的构造函数。参见最后一个无效示例。
¥This rule reports on constructors whose sole purpose is to make a parent constructor public. See the last invalid example.
¥Examples
¥Invalid
code-block.js:2:5 lint/complexity/noUselessConstructor FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This constructor is unnecessary.
1 │ class A {
> 2 │ constructor (a) {}
│ ^^^^^^^^^^^^^^^^^^
3 │ }
4 │
ℹ Unsafe fix: Remove the unnecessary constructor.
1 1 │ class A {
2 │ - ····constructor·(a)·{}
3 2 │ }
4 3 │
code-block.ts:2:5 lint/complexity/noUselessConstructor FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This constructor is unnecessary.
1 │ class B extends A {
> 2 │ constructor (a) {
│ ^^^^^^^^^^^^^^^^^
> 3 │ super(a);
> 4 │ }
│ ^
5 │ }
6 │
ℹ Unsafe fix: Remove the unnecessary constructor.
1 1 │ class B extends A {
2 │ - ····constructor·(a)·{
3 │ - ········super(a);
4 │ - ····}
5 2 │ }
6 3 │
code-block.js:5:5 lint/complexity/noUselessConstructor FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This constructor is unnecessary.
3 │ * Documented constructor.
4 │ */
> 5 │ constructor () {}
│ ^^^^^^^^^^^^^^^^^
6 │ }
7 │
ℹ Unsafe fix: Remove the unnecessary constructor.
1 1 │ class C {
2 │ - ····/**
3 │ - ·····*·Documented·constructor.
4 │ - ·····*/
5 │ - ····constructor·()·{}
6 2 │ }
7 3 │
code-block.js:2:5 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ ‘protected’ modifier can only be used in TypeScript files
1 │ class A {
> 2 │ protected constructor() {
│ ^^^^^^^^^
3 │ this.prop = 1;
4 │ }
¥Valid
¥Related links