noAssignInExpressions
诊断类别:lint/suspicious/noAssignInExpressions
¥Diagnostic Category: lint/suspicious/noAssignInExpressions
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
-
灵感来自:
no-cond-assign
¥Inspired from:
no-cond-assign
禁止表达式中的赋值。
¥Disallow assignments in expressions.
在表达式中,通常会将比较运算符(例如 ==
)误输入为赋值运算符(例如 =
)。此外,在表达式中使用赋值令人困惑。实际上,表达式通常被认为是无副作用的。
¥In expressions, it is common to mistype a comparison operator (such as ==
) as an assignment operator (such as =
).
Moreover, the use of assignments in expressions is confusing.
Indeed, expressions are often considered as side-effect free.
¥Examples
¥Invalid
let a, b;a = (b = 1) + 1;
code-block.ts:2:6 lint/suspicious/noAssignInExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The assignment should not be in an expression.
1 │ let a, b;
> 2 │ a = (b = 1) + 1;
│ ^^^^^
3 │
ℹ The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.
let a;if (a = 1) {}
code-block.ts:2:5 lint/suspicious/noAssignInExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The assignment should not be in an expression.
1 │ let a;
> 2 │ if (a = 1) {
│ ^^^^^
3 │ }
4 │
ℹ The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.
function f(a) { return a = 1;}
code-block.ts:2:12 lint/suspicious/noAssignInExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The assignment should not be in an expression.
1 │ function f(a) {
> 2 │ return a = 1;
│ ^^^^^
3 │ }
4 │
ℹ The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.
¥Valid
let a;a = 1;
¥Related links