no-return-assign

禁止在 return 语句中使用赋值运算符

JavaScript 的一个有趣且有时令人困惑的方面是赋值几乎可以在任何时候发生。因此,当真正的意图是进行比较时,错误的等号最终可能会导致赋值。在使用 return 语句时尤其如此。例如:

¥One of the interesting, and sometimes confusing, aspects of JavaScript is that assignment can happen at almost any point. Because of this, an errant equals sign can end up causing assignment when the true intent was to do a comparison. This is especially true when using a return statement. For example:

function doSomething() {
    return foo = bar + 2;
}

在这里很难说出 return 声明的意图。有可能这个函数是为了返回 bar + 2 的结果,但为什么要赋值给 foo 呢?也有可能是为了使用 == 之类的比较运算符,而此代码是错误的。

¥It is difficult to tell the intent of the return statement here. It’s possible that the function is meant to return the result of bar + 2, but then why is it assigning to foo? It’s also possible that the intent was to use a comparison operator such as == and that this code is an error.

由于这种歧义,最好不要在 return 语句中使用赋值。

¥Because of this ambiguity, it’s considered a best practice to not use assignment in return statements.

规则详情

¥Rule Details

该规则旨在消除 return 语句中的赋值。因此,只要发现分配是 return 的一部分,它就会触发警告。

¥This rule aims to eliminate assignments from return statements. As such, it will warn whenever an assignment is found as part of return.

选项

¥Options

该规则采用一个选项,一个字符串,它必须包含以下值之一:

¥The rule takes one option, a string, which must contain one of the following values:

  • except-parens(默认):除非它们被括在括号中,否则不允许赋值。

    ¥except-parens (default): Disallow assignments unless they are enclosed in parentheses.

  • always:禁止所有分配。

    ¥always: Disallow all assignments.

except-parens

这是默认选项。它不允许赋值,除非它们被括在括号中。

¥This is the default option. It disallows assignments unless they are enclosed in parentheses.

默认 "except-parens" 选项的错误代码示例:

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

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

function doSomething() {
    return foo = bar + 2;
}

function doSomethingElse() {
    return foo += 2;
}

const foo = (a, b) => a = b

const bar = (a, b, c) => (a = b, c == b)

function doSomethingMore() {
    return foo = bar && foo > 0;
}

默认 "except-parens" 选项的正确代码示例:

¥Examples of correct code for the default "except-parens" option:

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

function doSomething() {
    return foo == bar + 2;
}

function doSomethingElse() {
    return foo === bar + 2;
}

function doSomethingMore() {
    return (foo = bar + 2);
}

const foo = (a, b) => (a = b)

const bar = (a, b, c) => ((a = b), c == b)

function doAnotherThing() {
    return (foo = bar) && foo > 0;
}

always

此选项不允许 return 语句中的所有赋值。所有作业都被视为问题。

¥This option disallows all assignments in return statements. All assignments are treated as problems.

"always" 选项的错误代码示例:

¥Examples of incorrect code for the "always" option:

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

function doSomething() {
    return foo = bar + 2;
}

function doSomethingElse() {
    return foo += 2;
}

function doSomethingMore() {
    return (foo = bar + 2);
}

"always" 选项的正确代码示例:

¥Examples of correct code for the "always" option:

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

function doSomething() {
    return foo == bar + 2;
}

function doSomethingElse() {
    return foo === bar + 2;
}

何时不使用

¥When Not To Use It

如果你想允许在 return 语句中使用赋值运算符,那么你可以安全地禁用此规则。

¥If you want to allow the use of assignment operators in a return statement, then you can safely disable this rule.

版本

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

资源

ESLint 中文网
粤ICP备13048890号