Expected to return a value at the end of function. Open
return function () {
- Read upRead up
- Exclude checks
title: consistent-return
rule_type: suggestion
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.
A confusing aspect of JavaScript is that a function returns undefined
if any of the following are true:
- it does not execute a
return
statement before it exits - it executes
return
which does not specify a value explicitly - it executes
return undefined
- it executes
return void
followed by an expression (for example, a function call) - 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:
- a code path through the function returns a Boolean value
true
- another code path does not return a value explicitly, therefore returns
undefined
implicitly
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
Rule Details
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:
::: incorrect
/*eslint consistent-return: "error"*/
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
function doSomething(condition) {
if (condition) {
return true;
}
}
:::
Examples of correct code for this rule:
::: correct
/*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
(default) always either specify values or returnundefined
implicitly only. -
"treatUndefinedAsUnspecified": true
always either specify values or returnundefined
explicitly or implicitly.
treatUndefinedAsUnspecified
Examples of incorrect code for this rule with the default { "treatUndefinedAsUnspecified": false }
option:
::: incorrect
/*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
}
:::
Examples of incorrect code for this rule with the { "treatUndefinedAsUnspecified": true }
option:
::: incorrect
/*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;
}
:::
Examples of correct code for this rule with the { "treatUndefinedAsUnspecified": true }
option:
::: correct
/*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
If you want to allow functions to have different return
behavior depending on code branching, then it is safe to disable this rule.
Source: http://eslint.org/docs/rules/
Expected to return a value at the end of arrow function. Open
const later = () => {
- Read upRead up
- Exclude checks
title: consistent-return
rule_type: suggestion
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.
A confusing aspect of JavaScript is that a function returns undefined
if any of the following are true:
- it does not execute a
return
statement before it exits - it executes
return
which does not specify a value explicitly - it executes
return undefined
- it executes
return void
followed by an expression (for example, a function call) - 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:
- a code path through the function returns a Boolean value
true
- another code path does not return a value explicitly, therefore returns
undefined
implicitly
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
Rule Details
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:
::: incorrect
/*eslint consistent-return: "error"*/
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
function doSomething(condition) {
if (condition) {
return true;
}
}
:::
Examples of correct code for this rule:
::: correct
/*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
(default) always either specify values or returnundefined
implicitly only. -
"treatUndefinedAsUnspecified": true
always either specify values or returnundefined
explicitly or implicitly.
treatUndefinedAsUnspecified
Examples of incorrect code for this rule with the default { "treatUndefinedAsUnspecified": false }
option:
::: incorrect
/*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
}
:::
Examples of incorrect code for this rule with the { "treatUndefinedAsUnspecified": true }
option:
::: incorrect
/*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;
}
:::
Examples of correct code for this rule with the { "treatUndefinedAsUnspecified": true }
option:
::: correct
/*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
If you want to allow functions to have different return
behavior depending on code branching, then it is safe to disable this rule.
Source: http://eslint.org/docs/rules/
Unexpected unnamed function. Open
return function () {
- Read upRead up
- Exclude checks
title: func-names ruletype: suggestion furtherreading: - https://web.archive.org/web/20201112040809/http://markdaggett.com/blog/2013/02/15/functions-explained/
- https://2ality.com/2015/09/function-names-es6.html
A pattern that's becoming more common is to give function expressions names to aid in debugging. For example:
Foo.prototype.bar = function bar() {};
Adding the second bar
in the above example is optional. If you leave off the function name then when the function throws an exception you are likely to get something similar to anonymous function
in the stack trace. If you provide the optional name for a function expression then you will get the name of the function expression in the stack trace.
Rule Details
This rule can enforce or disallow the use of named function expressions.
Options
This rule has a string option:
-
"always"
(default) requires function expressions to have a name -
"as-needed"
requires function expressions to have a name, if the name isn't assigned automatically per the ECMAScript specification. -
"never"
disallows named function expressions, except in recursive functions, where a name is needed
This rule has an object option:
-
"generators": "always" | "as-needed" | "never"
-
"always"
require named generators -
"as-needed"
require named generators if the name isn't assigned automatically per the ECMAScript specification. -
"never"
disallow named generators where possible.
-
When a value for generators
is not provided the behavior for generator functions falls back to the base option.
Please note that "always"
and "as-needed"
require function expressions and function declarations in export default
declarations to have a name.
always
Examples of incorrect code for this rule with the default "always"
option:
::: incorrect
/*eslint func-names: ["error", "always"]*/
Foo.prototype.bar = function() {};
const cat = {
meow: function() {}
}
(function() {
// ...
}())
export default function() {}
:::
Examples of correct code for this rule with the default "always"
option:
::: correct
/*eslint func-names: ["error", "always"]*/
Foo.prototype.bar = function bar() {};
const cat = {
meow() {}
}
(function bar() {
// ...
}())
export default function foo() {}
:::
as-needed
ECMAScript 6 introduced a name
property on all functions. The value of name
is determined by evaluating the code around the function to see if a name can be inferred. For example, a function assigned to a variable will automatically have a name
property equal to the name of the variable. The value of name
is then used in stack traces for easier debugging.
Examples of incorrect code for this rule with the "as-needed"
option:
::: incorrect
/*eslint func-names: ["error", "as-needed"]*/
Foo.prototype.bar = function() {};
(function() {
// ...
}())
export default function() {}
:::
Examples of correct code for this rule with the "as-needed"
option:
::: correct
/*eslint func-names: ["error", "as-needed"]*/
var bar = function() {};
const cat = {
meow: function() {}
}
class C {
#bar = function() {};
baz = function() {};
}
quux ??= function() {};
(function bar() {
// ...
}())
export default function foo() {}
:::
never
Examples of incorrect code for this rule with the "never"
option:
::: incorrect
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar = function bar() {};
(function bar() {
// ...
}())
:::
Examples of correct code for this rule with the "never"
option:
::: correct
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar = function() {};
(function() {
// ...
}())
:::
generators
Examples of incorrect code for this rule with the "always", { "generators": "as-needed" }
options:
::: incorrect
/*eslint func-names: ["error", "always", { "generators": "as-needed" }]*/
(function*() {
// ...
}())
:::
Examples of correct code for this rule with the "always", { "generators": "as-needed" }
options:
::: correct
/*eslint func-names: ["error", "always", { "generators": "as-needed" }]*/
var foo = function*() {};
:::
Examples of incorrect code for this rule with the "always", { "generators": "never" }
options:
::: incorrect
/*eslint func-names: ["error", "always", { "generators": "never" }]*/
var foo = bar(function *baz() {});
:::
Examples of correct code for this rule with the "always", { "generators": "never" }
options:
::: correct
/*eslint func-names: ["error", "always", { "generators": "never" }]*/
var foo = bar(function *() {});
:::
Examples of incorrect code for this rule with the "as-needed", { "generators": "never" }
options:
::: incorrect
/*eslint func-names: ["error", "as-needed", { "generators": "never" }]*/
var foo = bar(function *baz() {});
:::
Examples of correct code for this rule with the "as-needed", { "generators": "never" }
options:
::: correct
/*eslint func-names: ["error", "as-needed", { "generators": "never" }]*/
var foo = bar(function *() {});
:::
Examples of incorrect code for this rule with the "never", { "generators": "always" }
options:
::: incorrect
/*eslint func-names: ["error", "never", { "generators": "always" }]*/
var foo = bar(function *() {});
:::
Examples of correct code for this rule with the "never", { "generators": "always" }
options:
::: correct
/*eslint func-names: ["error", "never", { "generators": "always" }]*/
var foo = bar(function *baz() {});
:::
Compatibility
- JSCS: requireAnonymousFunctions
- JSCS: disallowAnonymousFunctions Source: http://eslint.org/docs/rules/
Use the rest parameters instead of 'arguments'. Open
const args = arguments;
- Read upRead up
- Exclude checks
title: prefer-rest-params ruletype: suggestion relatedrules:
- prefer-spread
There are rest parameters in ES2015.
We can use that feature for variadic functions instead of the arguments
variable.
arguments
does not have methods of Array.prototype
, so it's a bit of an inconvenience.
Rule Details
This rule is aimed to flag usage of arguments
variables.
Examples
Examples of incorrect code for this rule:
::: incorrect
/*eslint prefer-rest-params: "error"*/
function foo() {
console.log(arguments);
}
function foo(action) {
var args = Array.prototype.slice.call(arguments, 1);
action.apply(null, args);
}
function foo(action) {
var args = [].slice.call(arguments, 1);
action.apply(null, args);
}
:::
Examples of correct code for this rule:
::: correct
/*eslint prefer-rest-params: "error"*/
function foo(...args) {
console.log(args);
}
function foo(action, ...args) {
action.apply(null, args); // or `action(...args)`, related to the `prefer-spread` rule.
}
// Note: the implicit arguments can be overwritten.
function foo(arguments) {
console.log(arguments); // This is the first argument.
}
function foo() {
var arguments = 0;
console.log(arguments); // This is a local variable.
}
:::
When Not To Use It
This rule should not be used in ES3/5 environments.
In ES2015 (ES6) or later, if you don't want to be notified about arguments
variables, then it's safe to disable this rule.
Source: http://eslint.org/docs/rules/
Prefer named exports. Open
export default function debounce(func, wait, immediate) {
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
Definition for rule 'node/no-restricted-import' was not found. Open
export default function debounce(func, wait, immediate) {
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/