跳转到内容

noClassAssign

Diagnostic Category: lint/suspicious/noClassAssign

Since: v1.0.0

Sources:

Disallow reassigning class members.

A class declaration creates a variable that we can modify, however, the modification is a mistake in most cases.

class A {}
A = 0;
code-block.js:2:1 lint/suspicious/noClassAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

‘A’ is a class.

1 │ class A {}
> 2 │ A = 0;
^
3 │

‘A’ is defined here.

> 1 │ class A {}
^
2 │ A = 0;
3 │

A = 0;
class A {}
code-block.js:1:1 lint/suspicious/noClassAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

‘A’ is a class.

> 1 │ A = 0;
^
2 │ class A {}
3 │

‘A’ is defined here.

1 │ A = 0;
> 2 │ class A {}
^
3 │

class A {
b() {
A = 0;
}
}
code-block.js:3:3 lint/suspicious/noClassAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

‘A’ is a class.

1 │ class A {
2 │ b() {
> 3 │ A = 0;
^
4 │ }
5 │ }

‘A’ is defined here.

> 1 │ class A {
^
2 │ b() {
3 │ A = 0;

let A = class A {
b() {
A = 0;
// `let A` is shadowed by the class name.
}
}
code-block.js:3:3 lint/suspicious/noClassAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

‘A’ is a class.

1 │ let A = class A {
2 │ b() {
> 3 │ A = 0;
^
4 │ // let A is shadowed by the class name.
5 │ }

‘A’ is defined here.

> 1 │ let A = class A {
^
2 │ b() {
3 │ A = 0;

let A = class A {}
A = 0; // A is a variable.
let A = class {
b() {
A = 0; // A is a variable.
}
}
class A {
b(A) {
A = 0; // A is a parameter.
}
}