Index

logical-assignment-operators

要求或禁止逻辑赋值运算符速记

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

💡 hasSuggestions

此规则报告的一些问题可通过编辑器 建议 手动修复

❄️ Frozen

此规则目前已 冻结,不接受功能请求。

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"(默认)
  • "never"

对象选项(仅在字符串选项设置为 "always" 时可用):

🌐 Object option (only available if string option is set to "always"):

  • "enforceForIfStatements": false(默认)不要检查等效的 if 语句。
  • "enforceForIfStatements": true 检查是否存在等效的 if 语句。

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 中引入。

资源