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 循环的测试中重新分配变量)

    ¥"except-parens" (default) allows assignments in test conditions only if they are enclosed in parentheses (for example, to allow reassigning a variable in the test of a while or do...while loop)

  • "always" 不允许测试条件下的所有分配

    ¥"always" disallows all assignments in test conditions

except-parens

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

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

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

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

// Practical example that is similar to an error
var 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
var x;
if (x === 0) {
    var b = 1;
}

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

// Practical example that wraps the assignment and tests for 'null'
var setHeight = 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
var x;
if (x = 0) {
    var b = 1;
}

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

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

// Practical example that wraps the assignment and tests for 'null'
var setHeight = 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
var x;
if (x === 0) {
    var b = 1;
}

版本

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

资源

ESLint 中文网
粤ICP备13048890号