Index

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 语句。
  • 它执行 return,该命令没有明确指定值。
  • 它执行 return undefined
  • 它先执行 return void,然后执行一个表达式(例如,函数调用)。
  • 它先执行 return,然后执行任何其他求值为 undefined 的表达式。

如果函数中的某些代码路径显式返回一个值,但有些代码路径没有显式返回值,这可能是一个类型错误,尤其是在大型函数中。在以下示例中:

🌐 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
  • 另一条代码路径没有显式返回值,因此隐式返回 undefined
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": true 总是要么显式或隐式地返回 undefined,要么指定值。

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

资源