Function has too many statements (24). Maximum allowed is 10. Open
return function(str, format, escapeCode = '') {
- Read upRead up
- Exclude checks
title: max-statements ruletype: suggestion relatedrules: - complexity - max-depth - max-len - max-lines - max-lines-per-function - max-nested-callbacks
- max-params
The max-statements
rule allows you to specify the maximum number of statements allowed in a function.
function foo() {
var bar = 1; // one statement
var baz = 2; // two statements
var qux = 3; // three statements
}
Rule Details
This rule enforces a maximum number of statements allowed in function blocks.
Options
This rule has a number or object option:
-
"max"
(default10
) enforces a maximum number of statements allows in function blocks
Deprecated: The object property maximum
is deprecated; please use the object property max
instead.
This rule has an object option:
-
"ignoreTopLevelFunctions": true
ignores top-level functions
max
Examples of incorrect code for this rule with the default { "max": 10 }
option:
::: incorrect
/*eslint max-statements: ["error", 10]*/
/*eslint-env es6*/
function foo() {
var foo1 = 1;
var foo2 = 2;
var foo3 = 3;
var foo4 = 4;
var foo5 = 5;
var foo6 = 6;
var foo7 = 7;
var foo8 = 8;
var foo9 = 9;
var foo10 = 10;
var foo11 = 11; // Too many.
}
let foo = () => {
var foo1 = 1;
var foo2 = 2;
var foo3 = 3;
var foo4 = 4;
var foo5 = 5;
var foo6 = 6;
var foo7 = 7;
var foo8 = 8;
var foo9 = 9;
var foo10 = 10;
var foo11 = 11; // Too many.
};
:::
Examples of correct code for this rule with the default { "max": 10 }
option:
::: correct
/*eslint max-statements: ["error", 10]*/
/*eslint-env es6*/
function foo() {
var foo1 = 1;
var foo2 = 2;
var foo3 = 3;
var foo4 = 4;
var foo5 = 5;
var foo6 = 6;
var foo7 = 7;
var foo8 = 8;
var foo9 = 9;
var foo10 = 10;
return function () {
// The number of statements in the inner function does not count toward the
// statement maximum.
return 42;
};
}
let foo = () => {
var foo1 = 1;
var foo2 = 2;
var foo3 = 3;
var foo4 = 4;
var foo5 = 5;
var foo6 = 6;
var foo7 = 7;
var foo8 = 8;
var foo9 = 9;
var foo10 = 10;
return function () {
// The number of statements in the inner function does not count toward the
// statement maximum.
return 42;
};
}
:::
Note that this rule does not apply to class static blocks, and that statements in class static blocks do not count as statements in the enclosing function.
Examples of correct code for this rule with { "max": 2 }
option:
::: correct
/*eslint max-statements: ["error", 2]*/
function foo() {
let one;
let two = class {
static {
let three;
let four;
let five;
if (six) {
let seven;
let eight;
let nine;
}
}
};
}
:::
ignoreTopLevelFunctions
Examples of additional correct code for this rule with the { "max": 10 }, { "ignoreTopLevelFunctions": true }
options:
::: correct
/*eslint max-statements: ["error", 10, { "ignoreTopLevelFunctions": true }]*/
function foo() {
var foo1 = 1;
var foo2 = 2;
var foo3 = 3;
var foo4 = 4;
var foo5 = 5;
var foo6 = 6;
var foo7 = 7;
var foo8 = 8;
var foo9 = 9;
var foo10 = 10;
var foo11 = 11;
}
::: Source: http://eslint.org/docs/rules/
Avoid deeply nested control flow statements. Open
if (!openTokens) {
if (
str.slice(
matchStartIndex - escapeCode.length - opener.length,
matchStartIndex - opener.length
Unexpected unnamed function. Open
const escape = function(str) {
- 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/
Unexpected unnamed function. Open
return function(str, format, escapeCode = '') {
- 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/
Unexpected unnamed function. Open
export default (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/
Unexpected assignment within a 'while' statement. Open
while ((match = iterator.exec(str)) !== null) {
- Read upRead up
- Exclude checks
title: no-cond-assign ruletype: problem relatedrules:
- no-extra-parens
In conditional statements, it is very easy to mistype a comparison operator (such as ==
) as an assignment operator (such as =
). For example:
// Check the user's job title
if (user.jobTitle = "manager") {
// user.jobTitle is now incorrect
}
There are valid reasons to use assignment operators in conditional statements. However, it can be difficult to tell whether a specific assignment was intentional.
Rule Details
This rule disallows ambiguous assignment operators in test conditions of if
, for
, while
, and do...while
statements.
Options
This rule has a string option:
-
"except-parens"
(default) allows assignments in test conditions only if they are enclosed in parentheses (for example, to allow reassigning a variable in the test of awhile
ordo...while
loop) -
"always"
disallows all assignments in test conditions
except-parens
Examples of incorrect code for this rule with the default "except-parens"
option:
::: incorrect
/*eslint no-cond-assign: "error"*/
// Unintentional assignment
var x;
if (x = 0) {
var b = 1;
}
// Practical example that is similar to an error
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while (someNode = someNode.parentNode);
}
:::
Examples of correct code for this rule with the default "except-parens"
option:
::: correct
/*eslint no-cond-assign: "error"*/
// Assignment replaced by comparison
var x;
if (x === 0) {
var b = 1;
}
// Practical example that wraps the assignment in parentheses
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode));
}
// Practical example that wraps the assignment and tests for 'null'
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode) !== null);
}
:::
always
Examples of incorrect code for this rule with the "always"
option:
::: incorrect
/*eslint no-cond-assign: ["error", "always"]*/
// Unintentional assignment
var x;
if (x = 0) {
var b = 1;
}
// Practical example that is similar to an error
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while (someNode = someNode.parentNode);
}
// Practical example that wraps the assignment in parentheses
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode));
}
// Practical example that wraps the assignment and tests for 'null'
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode) !== null);
}
:::
Examples of correct code for this rule with the "always"
option:
::: correct
/*eslint no-cond-assign: ["error", "always"]*/
// Assignment replaced by comparison
var x;
if (x === 0) {
var b = 1;
}
::: Source: http://eslint.org/docs/rules/
Unexpected assignment within a 'do...while' statement. Open
} while (openTokens && (iterator.lastIndex = matchStartIndex));
- Read upRead up
- Exclude checks
title: no-cond-assign ruletype: problem relatedrules:
- no-extra-parens
In conditional statements, it is very easy to mistype a comparison operator (such as ==
) as an assignment operator (such as =
). For example:
// Check the user's job title
if (user.jobTitle = "manager") {
// user.jobTitle is now incorrect
}
There are valid reasons to use assignment operators in conditional statements. However, it can be difficult to tell whether a specific assignment was intentional.
Rule Details
This rule disallows ambiguous assignment operators in test conditions of if
, for
, while
, and do...while
statements.
Options
This rule has a string option:
-
"except-parens"
(default) allows assignments in test conditions only if they are enclosed in parentheses (for example, to allow reassigning a variable in the test of awhile
ordo...while
loop) -
"always"
disallows all assignments in test conditions
except-parens
Examples of incorrect code for this rule with the default "except-parens"
option:
::: incorrect
/*eslint no-cond-assign: "error"*/
// Unintentional assignment
var x;
if (x = 0) {
var b = 1;
}
// Practical example that is similar to an error
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while (someNode = someNode.parentNode);
}
:::
Examples of correct code for this rule with the default "except-parens"
option:
::: correct
/*eslint no-cond-assign: "error"*/
// Assignment replaced by comparison
var x;
if (x === 0) {
var b = 1;
}
// Practical example that wraps the assignment in parentheses
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode));
}
// Practical example that wraps the assignment and tests for 'null'
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode) !== null);
}
:::
always
Examples of incorrect code for this rule with the "always"
option:
::: incorrect
/*eslint no-cond-assign: ["error", "always"]*/
// Unintentional assignment
var x;
if (x = 0) {
var b = 1;
}
// Practical example that is similar to an error
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while (someNode = someNode.parentNode);
}
// Practical example that wraps the assignment in parentheses
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode));
}
// Practical example that wraps the assignment and tests for 'null'
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode) !== null);
}
:::
Examples of correct code for this rule with the "always"
option:
::: correct
/*eslint no-cond-assign: ["error", "always"]*/
// Assignment replaced by comparison
var x;
if (x === 0) {
var b = 1;
}
::: Source: http://eslint.org/docs/rules/
Prefer named exports. Open
export default (function() {
- 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
/* eslint-disable max-depth */
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/