Function render
has 113 lines of code (exceeds 25 allowed). Consider refactoring. Invalid
render() {
const { data, year, until } = this.props
const id = snakeCase(data.ui_text)
const { isCounts } = this.state
Function render
has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring. Open
render() {
const { data, year, until } = this.props
const id = snakeCase(data.ui_text)
const { isCounts } = this.state
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype. Open
for (const i in fitleredDataByYear) {
- Read upRead up
- Exclude checks
Require Guarding for-in (guard-for-in)
Looping over objects with a for in
loop will include properties that are inherited through the prototype chain. This behavior can lead to unexpected items in your for loop.
for (key in foo) {
doSomething(key);
}
Note that simply checking foo.hasOwnProperty(key)
is likely to cause an error in some cases; see [no-prototype-builtins](no-prototype-builtins.md).
Rule Details
This rule is aimed at preventing unexpected behavior that could arise from using a for in
loop without filtering the results in the loop. As such, it will warn when for in
loops do not filter their results with an if
statement.
Examples of incorrect code for this rule:
/*eslint guard-for-in: "error"*/
for (key in foo) {
doSomething(key);
}
Examples of correct code for this rule:
/*eslint guard-for-in: "error"*/
for (key in foo) {
if (Object.prototype.hasOwnProperty.call(foo, key)) {
doSomething(key);
}
if ({}.hasOwnProperty.call(foo, key)) {
doSomething(key);
}
}
Related Rules
- [no-prototype-builtins](no-prototype-builtins.md)
Further Reading
Expected to return a value at the end of arrow function. Invalid
let dataFormattedd = dataFormatted.map(j => {
- Read upRead up
- Exclude checks
require return
statements to either always or never specify values (consistent-return)
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:
/*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:
/*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:
/*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:
/*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:
/*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/
The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype. Open
for (const i in Array.from(keys)) {
- Read upRead up
- Exclude checks
Require Guarding for-in (guard-for-in)
Looping over objects with a for in
loop will include properties that are inherited through the prototype chain. This behavior can lead to unexpected items in your for loop.
for (key in foo) {
doSomething(key);
}
Note that simply checking foo.hasOwnProperty(key)
is likely to cause an error in some cases; see [no-prototype-builtins](no-prototype-builtins.md).
Rule Details
This rule is aimed at preventing unexpected behavior that could arise from using a for in
loop without filtering the results in the loop. As such, it will warn when for in
loops do not filter their results with an if
statement.
Examples of incorrect code for this rule:
/*eslint guard-for-in: "error"*/
for (key in foo) {
doSomething(key);
}
Examples of correct code for this rule:
/*eslint guard-for-in: "error"*/
for (key in foo) {
if (Object.prototype.hasOwnProperty.call(foo, key)) {
doSomething(key);
}
if ({}.hasOwnProperty.call(foo, key)) {
doSomething(key);
}
}
Related Rules
- [no-prototype-builtins](no-prototype-builtins.md)
Further Reading
for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array. Open
for (const i in fitleredDataByYear) {
- Read upRead up
- Exclude checks
disallow specified syntax (no-restricted-syntax)
JavaScript has a lot of language features, and not everyone likes all of them. As a result, some projects choose to disallow the use of certain language features altogether. For instance, you might decide to disallow the use of try-catch
or class
, or you might decide to disallow the use of the in
operator.
Rather than creating separate rules for every language feature you want to turn off, this rule allows you to configure the syntax elements you want to restrict use of. These elements are represented by their ESTree node types. For example, a function declaration is represented by FunctionDeclaration
and the with
statement is represented by WithStatement
. You may find the full list of AST node names you can use on GitHub and use the online parser to see what type of nodes your code consists of.
You can also specify [AST selectors](../developer-guide/selectors) to restrict, allowing much more precise control over syntax patterns.
Rule Details
This rule disallows specified (that is, user-defined) syntax.
Options
This rule takes a list of strings, where each string is an AST selector:
{
"rules": {
"no-restricted-syntax": ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"]
}
}
Alternatively, the rule also accepts objects, where the selector and an optional custom message are specified:
{
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "FunctionExpression",
"message": "Function expressions are not allowed."
},
{
"selector": "CallExpression[callee.name='setTimeout'][arguments.length!=2]",
"message": "setTimeout must always be invoked with two arguments."
}
]
}
}
If a custom message is specified with the message
property, ESLint will use that message when reporting occurrences of the syntax specified in the selector
property.
The string and object formats can be freely mixed in the configuration as needed.
Examples of incorrect code for this rule with the "FunctionExpression", "WithStatement", BinaryExpression[operator='in']
options:
/* eslint no-restricted-syntax: ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"] */
with (me) {
dontMess();
}
var doSomething = function () {};
foo in bar;
Examples of correct code for this rule with the "FunctionExpression", "WithStatement", BinaryExpression[operator='in']
options:
/* eslint no-restricted-syntax: ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"] */
me.dontMess();
function doSomething() {};
foo instanceof bar;
When Not To Use It
If you don't want to restrict your code from using any JavaScript features or syntax, you should not use this rule.
Related Rules
- [no-alert](no-alert.md)
- [no-console](no-console.md)
- [no-debugger](no-debugger.md)
- [no-restricted-properties](no-restricted-properties.md) Source: http://eslint.org/docs/rules/
for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array. Open
for (const j in fitleredDataByYear) {
- Read upRead up
- Exclude checks
disallow specified syntax (no-restricted-syntax)
JavaScript has a lot of language features, and not everyone likes all of them. As a result, some projects choose to disallow the use of certain language features altogether. For instance, you might decide to disallow the use of try-catch
or class
, or you might decide to disallow the use of the in
operator.
Rather than creating separate rules for every language feature you want to turn off, this rule allows you to configure the syntax elements you want to restrict use of. These elements are represented by their ESTree node types. For example, a function declaration is represented by FunctionDeclaration
and the with
statement is represented by WithStatement
. You may find the full list of AST node names you can use on GitHub and use the online parser to see what type of nodes your code consists of.
You can also specify [AST selectors](../developer-guide/selectors) to restrict, allowing much more precise control over syntax patterns.
Rule Details
This rule disallows specified (that is, user-defined) syntax.
Options
This rule takes a list of strings, where each string is an AST selector:
{
"rules": {
"no-restricted-syntax": ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"]
}
}
Alternatively, the rule also accepts objects, where the selector and an optional custom message are specified:
{
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "FunctionExpression",
"message": "Function expressions are not allowed."
},
{
"selector": "CallExpression[callee.name='setTimeout'][arguments.length!=2]",
"message": "setTimeout must always be invoked with two arguments."
}
]
}
}
If a custom message is specified with the message
property, ESLint will use that message when reporting occurrences of the syntax specified in the selector
property.
The string and object formats can be freely mixed in the configuration as needed.
Examples of incorrect code for this rule with the "FunctionExpression", "WithStatement", BinaryExpression[operator='in']
options:
/* eslint no-restricted-syntax: ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"] */
with (me) {
dontMess();
}
var doSomething = function () {};
foo in bar;
Examples of correct code for this rule with the "FunctionExpression", "WithStatement", BinaryExpression[operator='in']
options:
/* eslint no-restricted-syntax: ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"] */
me.dontMess();
function doSomething() {};
foo instanceof bar;
When Not To Use It
If you don't want to restrict your code from using any JavaScript features or syntax, you should not use this rule.
Related Rules
- [no-alert](no-alert.md)
- [no-console](no-console.md)
- [no-debugger](no-debugger.md)
- [no-restricted-properties](no-restricted-properties.md) Source: http://eslint.org/docs/rules/
for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array. Open
for (const i in Array.from(keys)) {
- Read upRead up
- Exclude checks
disallow specified syntax (no-restricted-syntax)
JavaScript has a lot of language features, and not everyone likes all of them. As a result, some projects choose to disallow the use of certain language features altogether. For instance, you might decide to disallow the use of try-catch
or class
, or you might decide to disallow the use of the in
operator.
Rather than creating separate rules for every language feature you want to turn off, this rule allows you to configure the syntax elements you want to restrict use of. These elements are represented by their ESTree node types. For example, a function declaration is represented by FunctionDeclaration
and the with
statement is represented by WithStatement
. You may find the full list of AST node names you can use on GitHub and use the online parser to see what type of nodes your code consists of.
You can also specify [AST selectors](../developer-guide/selectors) to restrict, allowing much more precise control over syntax patterns.
Rule Details
This rule disallows specified (that is, user-defined) syntax.
Options
This rule takes a list of strings, where each string is an AST selector:
{
"rules": {
"no-restricted-syntax": ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"]
}
}
Alternatively, the rule also accepts objects, where the selector and an optional custom message are specified:
{
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "FunctionExpression",
"message": "Function expressions are not allowed."
},
{
"selector": "CallExpression[callee.name='setTimeout'][arguments.length!=2]",
"message": "setTimeout must always be invoked with two arguments."
}
]
}
}
If a custom message is specified with the message
property, ESLint will use that message when reporting occurrences of the syntax specified in the selector
property.
The string and object formats can be freely mixed in the configuration as needed.
Examples of incorrect code for this rule with the "FunctionExpression", "WithStatement", BinaryExpression[operator='in']
options:
/* eslint no-restricted-syntax: ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"] */
with (me) {
dontMess();
}
var doSomething = function () {};
foo in bar;
Examples of correct code for this rule with the "FunctionExpression", "WithStatement", BinaryExpression[operator='in']
options:
/* eslint no-restricted-syntax: ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"] */
me.dontMess();
function doSomething() {};
foo instanceof bar;
When Not To Use It
If you don't want to restrict your code from using any JavaScript features or syntax, you should not use this rule.
Related Rules
- [no-alert](no-alert.md)
- [no-console](no-console.md)
- [no-debugger](no-debugger.md)
- [no-restricted-properties](no-restricted-properties.md) Source: http://eslint.org/docs/rules/
Expected to return a value at the end of arrow function. Invalid
let dataFormattedd = dataFormatted.map(j => {
- Read upRead up
- Exclude checks
Enforces return statements in callbacks of array's methods (array-callback-return)
Array
has several methods for filtering, mapping, and folding.
If we forget to write return
statement in a callback of those, it's probably a mistake.
// example: convert ['a', 'b', 'c'] --> {a: 0, b: 1, c: 2}
var indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
}, {}); // Error: cannot set property 'b' of undefined
This rule enforces usage of return
statement in callbacks of array's methods.
Rule Details
This rule finds callback functions of the following methods, then checks usage of return
statement.
Array.from
Array.prototype.every
Array.prototype.filter
Array.prototype.find
Array.prototype.findIndex
Array.prototype.map
Array.prototype.reduce
Array.prototype.reduceRight
Array.prototype.some
Array.prototype.sort
- And above of typed arrays.
Examples of incorrect code for this rule:
/*eslint array-callback-return: "error"*/
var indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
}, {});
var foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
});
var bar = foo.filter(function(x) {
if (x) {
return true;
} else {
return;
}
});
Examples of correct code for this rule:
/*eslint array-callback-return: "error"*/
var indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
return memo;
}, {});
var foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
return false;
});
var bar = foo.map(node => node.getAttribute("id"));
Known Limitations
This rule checks callback functions of methods with the given names, even if the object which has the method is not an array.
When Not To Use It
If you don't want to warn about usage of return
statement in callbacks of array's methods, then it's safe to disable this rule.
Source: http://eslint.org/docs/rules/
The object literal notation {} is preferrable. Open
const object = new Object()
- Read upRead up
- Exclude checks
disallow Object
constructors (no-new-object)
The Object
constructor is used to create new generic objects in JavaScript, such as:
var myObject = new Object();
However, this is no different from using the more concise object literal syntax:
var myObject = {};
For this reason, many prefer to always use the object literal syntax and never use the Object
constructor.
While there are no performance differences between the two approaches, the byte savings and conciseness of the object literal form is what has made it the de facto way of creating new objects.
Rule Details
This rule disallows Object
constructors.
Examples of incorrect code for this rule:
/*eslint no-new-object: "error"*/
var myObject = new Object();
var myObject = new Object;
Examples of correct code for this rule:
/*eslint no-new-object: "error"*/
var myObject = new CustomObject();
var myObject = {};
When Not To Use It
If you wish to allow the use of the Object
constructor, you can safely turn this rule off.
Related Rules
- [no-array-constructor](no-array-constructor.md)
- [no-new-wrappers](no-new-wrappers.md) Source: http://eslint.org/docs/rules/