Index

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(默认):除非被括号包住,否则不允许赋值。
  • always:禁止所有分配。

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

资源