Function render
has 155 lines of code (exceeds 25 allowed). Consider refactoring. Invalid
render() {
const {
agency,
pageType,
isAgency,
Function render
has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring. Invalid
render() {
const {
agency,
pageType,
isAgency,
- 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
File NibrsContainer.js
has 269 lines of code (exceeds 250 allowed). Consider refactoring. Open
import startCase from 'lodash.startcase'
import PropTypes from 'prop-types'
import React from 'react'
import { connect } from 'react-redux'
Function getCards
has 42 lines of code (exceeds 25 allowed). Consider refactoring. Invalid
getCards(data, place, categories, until) {
let cards = []
const content = []
let cnt = 0
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 countDataByYear) {
- 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 'this' to be used by class method 'initialNibrsYear'. Invalid
initialNibrsYear(place, placeType, since) {
- Read upRead up
- Exclude checks
Enforce that class methods utilize this
(class-methods-use-this)
If a class method does not use this
, it can safely be made a static function.
It's possible to have a class method which doesn't use this
, such as:
class A {
constructor() {
this.a = "hi";
}
print() {
console.log(this.a);
}
sayHi() {
console.log("hi");
}
}
let a = new A();
a.sayHi(); // => "hi"
In the example above, the sayHi
method doesn't use this
, so we can make it a static method:
class A {
constructor() {
this.a = "hi";
}
print() {
console.log(this.a);
}
static sayHi() {
console.log("hi");
}
}
A.sayHi(); // => "hi"
Also note in the above examples that the code calling the function on an instance of the class (let a = new A(); a.sayHi();
) changes to calling it on the class itself (A.sayHi();
).
Rule Details
This rule is aimed to flag class methods that do not use this
.
Examples of incorrect code for this rule:
/*eslint class-methods-use-this: "error"*/
/*eslint-env es6*/
class A {
foo() {
console.log("Hello World"); /*error Expected 'this' to be used by class method 'foo'.*/
}
}
Examples of correct code for this rule:
/*eslint class-methods-use-this: "error"*/
/*eslint-env es6*/
class A {
foo() {
this.bar = "Hello World"; // OK, this is used
}
}
class A {
constructor() {
// OK. constructor is exempt
}
}
class A {
static foo() {
// OK. static methods aren't expected to use this.
}
}
Options
Exceptions
"class-methods-use-this": [<enabled>, { "exceptMethods": [<...exceptions>] }]</enabled>
The exceptMethods
option allows you to pass an array of method names for which you would like to ignore warnings.
Examples of incorrect code for this rule when used without exceptMethods:
/*eslint class-methods-use-this: "error"*/
class A {
foo() {
}
}
Examples of correct code for this rule when used with exceptMethods:
/*eslint class-methods-use-this: ["error", { "exceptMethods": ["foo"] }] */
class A {
foo() {
}
}
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 countDataByYear) {
- 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/
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
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 countDataByYear) {
- 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
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/