consistent-return
要求 return
语句始终或从不指定值
与强制函数返回指定类型值的静态类型语言不同,JavaScript 允许函数中的不同代码路径返回不同类型的值。
¥Unlike statically-typed languages which enforce that a function returns a specified type of value, JavaScript allows different code paths in a function to return different types of values.
JavaScript 的一个令人困惑的方面是,如果以下任何一项为真,则函数会返回 undefined
:
¥A confusing aspect of JavaScript is that a function returns undefined
if any of the following are true:
-
它在退出之前不执行
return
语句¥it does not execute a
return
statement before it exits -
它执行没有明确指定值的
return
¥it executes
return
which does not specify a value explicitly -
它执行
return undefined
¥it executes
return undefined
-
它执行
return void
后跟一个表达式(例如,函数调用)¥it executes
return void
followed by an expression (for example, a function call) -
它执行
return
后跟任何其他计算结果为undefined
的表达式¥it executes
return
followed by any other expression which evaluates toundefined
如果函数中的任何代码路径显式返回值,但某些代码路径没有显式返回值,则可能是输入错误,尤其是在大型函数中。在以下示例中:
¥If any code paths in a function return a value explicitly but some code path do not return a value explicitly, it might be a typing mistake, especially in a large function. In the following example:
-
通过函数的代码路径返回布尔值
true
¥a code path through the function returns a Boolean value
true
-
另一个代码路径没有显式返回值,因此隐式返回
undefined
¥another code path does not return a value explicitly, therefore returns
undefined
implicitly
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
规则详情
¥Rule Details
此规则要求 return
语句始终或从不指定值。此规则忽略名称以大写字母开头的函数定义,因为构造函数(当使用 new
运算符调用时)如果不显式返回另一个对象,则隐式返回实例化对象。
¥This rule requires return
statements to either always or never specify values. This rule ignores function definitions where the name begins with an uppercase letter, because constructors (when invoked with the new
operator) return the instantiated object implicitly if they do not return another object explicitly.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint consistent-return: "error"*/
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
function doSomethingElse(condition) {
if (condition) {
return true;
}
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint consistent-return: "error"*/
function doSomething(condition) {
if (condition) {
return true;
} else {
return false;
}
}
function Foo() {
if (!(this instanceof Foo)) {
return new Foo();
}
this.a = 0;
}
选项
¥Options
此规则有一个对象选项:
¥This rule has an object option:
-
"treatUndefinedAsUnspecified": false
(默认)始终指定值或仅隐式返回undefined
。¥
"treatUndefinedAsUnspecified": false
(default) always either specify values or returnundefined
implicitly only. -
"treatUndefinedAsUnspecified": true
始终显式或隐式指定值或返回undefined
。¥
"treatUndefinedAsUnspecified": true
always either specify values or returnundefined
explicitly or implicitly.
treatUndefinedAsUnspecified
使用默认 { "treatUndefinedAsUnspecified": false }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default { "treatUndefinedAsUnspecified": false }
option:
/*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": false }]*/
function foo(callback) {
if (callback) {
return void callback();
}
// no return statement
}
function bar(condition) {
if (condition) {
return undefined;
}
// no return statement
}
使用 { "treatUndefinedAsUnspecified": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the { "treatUndefinedAsUnspecified": true }
option:
/*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/
function foo(callback) {
if (callback) {
return void callback();
}
return true;
}
function bar(condition) {
if (condition) {
return undefined;
}
return true;
}
使用 { "treatUndefinedAsUnspecified": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "treatUndefinedAsUnspecified": true }
option:
/*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/
function foo(callback) {
if (callback) {
return void callback();
}
// no return statement
}
function bar(condition) {
if (condition) {
return undefined;
}
// no return statement
}
何时不使用
¥When Not To Use It
如果你想允许函数根据代码分支具有不同的 return
行为,那么禁用此规则是安全的。
¥If you want to allow functions to have different return
behavior depending on code branching, then it is safe to disable this rule.
版本
此规则是在 ESLint v0.4.0 中引入。