Skip to content

useShorthandAssign

诊断类别:lint/style/useShorthandAssign

¥Diagnostic Category: lint/style/useShorthandAssign

自从:v1.3.0

¥Since: v1.3.0

来源:

¥Sources:

尽可能要求赋值运算符简写。

¥Require assignment operator shorthand where possible.

JavaScript 提供结合变量赋值和简单数学运算的简写运算符。

¥JavaScript provides shorthand operators combining a variable assignment and simple mathematical operation.

¥Examples

¥Invalid

a = a + 1;
code-block.js:1:1 lint/style/useShorthandAssign  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Assignment (=) can be replaced with operator assignment +=.

> 1 │ a = a + 1;
^^^^^^^^^
2 │

Unsafe fix: Use += instead.

1 - a·=·a·+·1;
1+ a·+=·1;
2 2

a = a - 1;
code-block.js:1:1 lint/style/useShorthandAssign  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Assignment (=) can be replaced with operator assignment -=.

> 1 │ a = a - 1;
^^^^^^^^^
2 │

Unsafe fix: Use -= instead.

1 - a·=·a·-·1;
1+ a·-=·1;
2 2

a = a * 1;
code-block.js:1:1 lint/style/useShorthandAssign  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Assignment (=) can be replaced with operator assignment *=.

> 1 │ a = a * 1;
^^^^^^^^^
2 │

Unsafe fix: Use *= instead.

1 - a·=·a·*·1;
1+ a·*=·1;
2 2

¥Valid

a += 1;
a -= 1;
a *= 1;

¥Related links