Index

no-cond-assign

禁止条件表达式中的赋值运算符

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

在条件语句中,很容易将比较运算符(例如 ==)误输入为赋值运算符(例如 =)。例如:

🌐 In conditional statements, it is very easy to mistype a comparison operator (such as ==) as an assignment operator (such as =). For example:

// Check the user's job title
if (user.jobTitle = "manager") {
    // user.jobTitle is now incorrect
}

在条件语句中使用赋值运算符是有正当理由的。然而,要判断某个特定的赋值是否是有意的,可能很困难。

🌐 There are valid reasons to use assignment operators in conditional statements. However, it can be difficult to tell whether a specific assignment was intentional.

规则详情

🌐 Rule Details

此规则禁止在 ifforwhiledo...while 语句的测试条件中使用模糊的赋值运算符。

🌐 This rule disallows ambiguous assignment operators in test conditions of if, for, while, and do...while statements.

选项

🌐 Options

此规则有一个字符串选项:

🌐 This rule has a string option:

  • "except-parens"(默认)仅在赋值被括号括起来的情况下才允许在测试条件中进行赋值(例如,为了允许在 whiledo...while 循环的测试中重新赋值一个变量)。
  • "always" 不允许在测试条件中进行所有赋值。

except-parens

使用默认 "except-parens" 选项时,该规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the default "except-parens" option:

在线运行
/*eslint no-cond-assign: "error"*/

// Unintentional assignment
let x;
if (x = 0) {
    const b = 1;
}

// Practical example that is similar to an error
const setHeight = function (someNode) {
    do {
        someNode.height = "100px";
    } while (someNode = someNode.parentNode);
}

使用默认 "except-parens" 选项时,该规则的正确代码示例:

🌐 Examples of correct code for this rule with the default "except-parens" option:

在线运行
/*eslint no-cond-assign: "error"*/

// Assignment replaced by comparison
let x;
if (x === 0) {
    const b = 1;
}

// Practical example that wraps the assignment in parentheses
const setHeight = function (someNode) {
    do {
        someNode.height = "100px";
    } while ((someNode = someNode.parentNode));
}

// Practical example that wraps the assignment and tests for 'null'
const set_height = function (someNode) {
    do {
        someNode.height = "100px";
    } while ((someNode = someNode.parentNode) !== null);
}

always

使用 "always" 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the "always" option:

在线运行
/*eslint no-cond-assign: ["error", "always"]*/

// Unintentional assignment
let x;
if (x = 0) {
    const b = 1;
}

// Practical example that is similar to an error
const setHeight = function (someNode) {
    do {
        someNode.height = "100px";
    } while (someNode = someNode.parentNode);
}

// Practical example that wraps the assignment in parentheses
const set_height = function (someNode) {
    do {
        someNode.height = "100px";
    } while ((someNode = someNode.parentNode));
}

// Practical example that wraps the assignment and tests for 'null'
const heightSetter = function (someNode) {
    do {
        someNode.height = "100px";
    } while ((someNode = someNode.parentNode) !== null);
}

使用 "always" 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the "always" option:

在线运行
/*eslint no-cond-assign: ["error", "always"]*/

// Assignment replaced by comparison
let x;
if (x === 0) {
    const b = 1;
}

版本

此规则是在 ESLint v0.0.9 中引入。

资源