Skip to content

noSetterReturn

诊断类别:lint/correctness/noSetterReturn

¥Diagnostic Category: lint/correctness/noSetterReturn

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止从 setter 返回值

¥Disallow returning a value from a setter

虽然从 setter 返回值不会产生错误,但返回的值将被忽略。因此,从 setter 返回值要么是不必要的,要么是可能的错误。

¥While returning a value from a setter does not produce an error, the returned value is being ignored. Therefore, returning a value from a setter is either unnecessary or a possible error.

仅允许返回没有值,因为它是控制流语句。

¥Only returning without a value is allowed, as it’s a control flow statement.

¥Examples

¥Invalid

class A {
set foo(x) {
return x;
}
}
code-block.js:3:9 lint/correctness/noSetterReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The setter should not return a value.

1 │ class A {
2 │ set foo(x) {
> 3 │ return x;
^^^^^^^^^
4 │ }
5 │ }

The setter is here:

1 │ class A {
> 2 │ set foo(x) {
^^^^^^^^^^^^
> 3 │ return x;
> 4 │ }
^
5 │ }
6 │

Returning a value from a setter is ignored.

const b = {
set foo(x) {
return x;
},
};
code-block.js:3:9 lint/correctness/noSetterReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The setter should not return a value.

1 │ const b = {
2 │ set foo(x) {
> 3 │ return x;
^^^^^^^^^
4 │ },
5 │ };

The setter is here:

1 │ const b = {
> 2 │ set foo(x) {
^^^^^^^^^^^^
> 3 │ return x;
> 4 │ },
^
5 │ };
6 │

Returning a value from a setter is ignored.

const c = {
set foo(x) {
if (x) {
return x;
}
},
};
code-block.js:4:13 lint/correctness/noSetterReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The setter should not return a value.

2 │ set foo(x) {
3 │ if (x) {
> 4 │ return x;
^^^^^^^^^
5 │ }
6 │ },

The setter is here:

1 │ const c = {
> 2 │ set foo(x) {
^^^^^^^^^^^^
> 3 │ if (x) {
> 4 │ return x;
> 5 │ }
> 6 │ },
^
7 │ };
8 │

Returning a value from a setter is ignored.

¥Valid

// early-return
class A {
set foo(x) {
if (x) {
return;
}
}
}
// not a setter
class B {
set(x) {
return x;
}
}

¥Related links