logical-assignment-operators
要求或禁止逻辑赋值运算符速记
ES2021 引入了逻辑运算符 ||
、&&
和 ??
的赋值运算符简写。以前,这仅允许用于数学运算,例如 +
或 *
(参见规则 operator-assignment)。如果赋值目标和逻辑表达式的左表达式相同,则可以使用简写。例如 a = a || b
可以缩短为 a ||= b
。
¥ES2021 introduces the assignment operator shorthand for the logical operators ||
, &&
and ??
.
Before, this was only allowed for mathematical operations such as +
or *
(see the rule operator-assignment).
The shorthand can be used if the assignment target and the left expression of a logical expression are the same.
For example a = a || b
can be shortened to a ||= b
.
规则详情
¥Rule Details
此规则要求或禁止逻辑赋值运算符简写。
¥This rule requires or disallows logical assignment operator shorthand.
选项
¥Options
该规则有一个字符串和一个对象选项。字符串选项:
¥This rule has a string and an object option. String option:
-
"always"
(默认)¥
"always"
(default) -
"never"
对象选项(仅当字符串选项设置为 "always"
时可用):
¥Object option (only available if string option is set to "always"
):
-
"enforceForIfStatements": false
(默认)不检查等效的if
语句¥
"enforceForIfStatements": false
(default) Do not check for equivalentif
statements -
"enforceForIfStatements": true
检查等效的if
语句¥
"enforceForIfStatements": true
Check for equivalentif
statements
always
此选项检查可以使用逻辑赋值运算符缩短的表达式。例如,a = a || b
可以缩短为 a ||= b
。据报告,具有结合性的表达式(例如 a = a || b || c
)可以缩短为 a ||= b || c
,除非使用括号明确定义计算顺序(例如 a = (a || b) || c
)。
¥This option checks for expressions that can be shortened using logical assignment operator. For example, a = a || b
can be shortened to a ||= b
.
Expressions with associativity such as a = a || b || c
are reported as being able to be shortened to a ||= b || c
unless the evaluation order is explicitly defined using parentheses, such as a = (a || b) || c
.
使用默认 "always"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default "always"
option:
/*eslint logical-assignment-operators: ["error", "always"]*/
a = a || b
a = a && b
a = a ?? b
a || (a = b)
a && (a = b)
a ?? (a = b)
a = a || b || c
a = a && b && c
a = a ?? b ?? c
使用默认 "always"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default "always"
option:
/*eslint logical-assignment-operators: ["error", "always"]*/
a = b
a += b
a ||= b
a = b || c
a || (b = c)
if (a) a = b
a = (a || b) || c
never
使用 "never"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "never"
option:
/*eslint logical-assignment-operators: ["error", "never"]*/
a ||= b
a &&= b
a ??= b
使用 "never"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "never"
option:
/*eslint logical-assignment-operators: ["error", "never"]*/
a = a || b
a = a && b
a = a ?? b
enforceForIfStatements
此选项使用可以用逻辑赋值运算符表示的 if 语句检查其他模式。
¥This option checks for additional patterns with if statements which could be expressed with the logical assignment operator.
使用 ["always", { enforceForIfStatements: true }]
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the ["always", { enforceForIfStatements: true }]
option:
/*eslint logical-assignment-operators: ["error", "always", { enforceForIfStatements: true }]*/
if (a) a = b // <=> a &&= b
if (!a) a = b // <=> a ||= b
if (a == null) a = b // <=> a ??= b
if (a === null || a === undefined) a = b // <=> a ??= b
使用 ["always", { enforceForIfStatements: true }]
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the ["always", { enforceForIfStatements: true }]
option:
/*eslint logical-assignment-operators: ["error", "always", { enforceForIfStatements: true }]*/
if (a) b = c
if (a === 0) a = b
何时不使用
¥When Not To Use It
使用逻辑运算符赋值速记是一种风格选择。关闭此规则将允许开发者根据具体情况选择哪种样式更具可读性。
¥Use of logical operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis.
版本
此规则是在 ESLint v8.24.0 中引入。