Showing 175 of 177 total issues
Avoid too many return
statements within this function. Open
return val1.join('\n');
Function applyActions
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
const applyActions = (socketMessage, data) => {
if (data.tap) {
socketMessage.taps = [{
type: data.tap,
...(data.tapDuration ? {duration: data.tapDuration} : {}),
- 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
Function readConfigFile
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
function readConfigFile(filePath) {
const extension = path.extname(filePath);
if (extension === '.js') {
if (!path.isAbsolute(filePath)) {
// ensure correct handling of relative paths, we want to prevent searching in __dirname
- 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
Function openDeepLinkFactory
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
const openDeepLinkFactory = (classInstance) => {
const toJSON = (data) => ({
type: getRequestType(data, false),
request: {
type: 'openDeepLink',
- 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
Expected blank line after variable declarations. Open
const rcConfig = readRcConfig(argv.baseConfigFile || argv.configFile);
- Read upRead up
- Exclude checks
require or disallow an empty line after variable declarations (newline-after-var)
This rule was deprecated in ESLint v4.0.0 and replaced by the [padding-line-between-statements](padding-line-between-statements.md) rule.
As of today there is no consistency in separating variable declarations from the rest of the code. Some developers leave an empty line between var statements and the rest of the code like:
var foo;
// do something with foo
Whereas others don't leave any empty newlines at all.
var foo;
// do something with foo
The problem is when these developers work together in a project. This rule enforces a coding style where empty newlines are allowed or disallowed after var
, let
, or const
statements. It helps the code to look consistent across the entire project.
Rule Details
This rule enforces a coding style where empty lines are required or disallowed after var
, let
, or const
statements to achieve a consistent coding style across the project.
Options
This rule has a string option:
-
"always"
(default) requires an empty line aftervar
,let
, orconst
Comments on a line directly after var statements are treated like additional var statements.
-
"never"
disallows empty lines aftervar
,let
, orconst
always
Examples of incorrect code for this rule with the default "always"
option:
/*eslint newline-after-var: ["error", "always"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
Examples of correct code for this rule with the default "always"
option:
/*eslint newline-after-var: ["error", "always"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
never
Examples of incorrect code for this rule with the "never"
option:
/*eslint newline-after-var: ["error", "never"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
Examples of correct code for this rule with the "never"
option:
/*eslint newline-after-var: ["error", "never"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
Source: http://eslint.org/docs/rules/
'video' is defined but never used. Open
const elementFactory = (classInstance, video) => {
- Read upRead up
- Exclude checks
Disallow Unused Variables (no-unused-vars)
Variables that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. Such variables take up space in the code and can lead to confusion by readers.
Rule Details
This rule is aimed at eliminating unused variables, functions, and parameters of functions.
A variable is considered to be used if any of the following are true:
- It represents a function that is called (
doSomething()
) - It is read (
var y = x
) - It is passed into a function as an argument (
doSomething(x)
) - It is read inside of a function that is passed to another function (
doSomething(function() { foo(); })
)
A variable is not considered to be used if it is only ever assigned to (var x = 5
) or declared.
Examples of incorrect code for this rule:
/*eslint no-unused-vars: "error"*/
/*global some_unused_var*/
// It checks variables you have defined as global
some_unused_var = 42;
var x;
// Write-only variables are not considered as used.
var y = 10;
y = 5;
// A read for a modification of itself is not considered as used.
var z = 0;
z = z + 1;
// By default, unused arguments cause warnings.
(function(foo) {
return 5;
})();
// Unused recursive functions also cause warnings.
function fact(n) {
if (n < 2) return 1;
return n * fact(n - 1);
}
// When a function definition destructures an array, unused entries from the array also cause warnings.
function getY([x, y]) {
return y;
}
Examples of correct code for this rule:
/*eslint no-unused-vars: "error"*/
var x = 10;
alert(x);
// foo is considered used here
myFunc(function foo() {
// ...
}.bind(this));
(function(foo) {
return foo;
})();
var myFunc;
myFunc = setTimeout(function() {
// myFunc is considered used
myFunc();
}, 50);
// Only the second argument from the descructured array is used.
function getY([, y]) {
return y;
}
exported
In environments outside of CommonJS or ECMAScript modules, you may use var
to create a global variable that may be used by other scripts. You can use the /* exported variableName */
comment block to indicate that this variable is being exported and therefore should not be considered unused.
Note that /* exported */
has no effect for any of the following:
- when the environment is
node
orcommonjs
- when
parserOptions.sourceType
ismodule
- when
ecmaFeatures.globalReturn
istrue
The line comment // exported variableName
will not work as exported
is not line-specific.
Examples of correct code for /* exported variableName */
operation:
/* exported global_var */
var global_var = 42;
Options
This rule takes one argument which can be a string or an object. The string settings are the same as those of the vars
property (explained below).
By default this rule is enabled with all
option for variables and after-used
for arguments.
{
"rules": {
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }]
}
}
vars
The vars
option has two settings:
-
all
checks all variables for usage, including those in the global scope. This is the default setting. -
local
checks only that locally-declared variables are used but will allow global variables to be unused.
vars: local
Examples of correct code for the { "vars": "local" }
option:
/*eslint no-unused-vars: ["error", { "vars": "local" }]*/
/*global some_unused_var */
some_unused_var = 42;
varsIgnorePattern
The varsIgnorePattern
option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain ignored
or Ignored
.
Examples of correct code for the { "varsIgnorePattern": "[iI]gnored" }
option:
/*eslint no-unused-vars: ["error", { "varsIgnorePattern": "[iI]gnored" }]*/
var firstVarIgnored = 1;
var secondVar = 2;
console.log(secondVar);
args
The args
option has three settings:
-
after-used
- only the last argument must be used. This allows you, for instance, to have two named parameters to a function and as long as you use the second argument, ESLint will not warn you about the first. This is the default setting. -
all
- all named arguments must be used. -
none
- do not check arguments.
args: after-used
Examples of incorrect code for the default { "args": "after-used" }
option:
/*eslint no-unused-vars: ["error", { "args": "after-used" }]*/
// 1 error
// "baz" is defined but never used
(function(foo, bar, baz) {
return bar;
})();
Examples of correct code for the default { "args": "after-used" }
option:
/*eslint no-unused-vars: ["error", {"args": "after-used"}]*/
(function(foo, bar, baz) {
return baz;
})();
args: all
Examples of incorrect code for the { "args": "all" }
option:
/*eslint no-unused-vars: ["error", { "args": "all" }]*/
// 2 errors
// "foo" is defined but never used
// "baz" is defined but never used
(function(foo, bar, baz) {
return bar;
})();
args: none
Examples of correct code for the { "args": "none" }
option:
/*eslint no-unused-vars: ["error", { "args": "none" }]*/
(function(foo, bar, baz) {
return bar;
})();
ignoreRestSiblings
The ignoreRestSiblings
option is a boolean (default: false
). Using a Rest Property it is possible to "omit" properties from an object, but by default the sibling properties are marked as "unused". With this option enabled the rest property's siblings are ignored.
Examples of correct code for the { "ignoreRestSiblings": true }
option:
/*eslint no-unused-vars: ["error", { "ignoreRestSiblings": true }]*/
// 'type' is ignored because it has a rest property sibling.
var { type, ...coords } = data;
argsIgnorePattern
The argsIgnorePattern
option specifies exceptions not to check for usage: arguments whose names match a regexp pattern. For example, variables whose names begin with an underscore.
Examples of correct code for the { "argsIgnorePattern": "^_" }
option:
/*eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }]*/
function foo(x, _y) {
return x + 1;
}
foo();
caughtErrors
The caughtErrors
option is used for catch
block arguments validation.
It has two settings:
-
none
- do not check error objects. This is the default setting. -
all
- all named arguments must be used.
caughtErrors: none
Not specifying this rule is equivalent of assigning it to none
.
Examples of correct code for the { "caughtErrors": "none" }
option:
/*eslint no-unused-vars: ["error", { "caughtErrors": "none" }]*/
try {
//...
} catch (err) {
console.error("errors");
}
caughtErrors: all
Examples of incorrect code for the { "caughtErrors": "all" }
option:
/*eslint no-unused-vars: ["error", { "caughtErrors": "all" }]*/
// 1 error
// "err" is defined but never used
try {
//...
} catch (err) {
console.error("errors");
}
caughtErrorsIgnorePattern
The caughtErrorsIgnorePattern
option specifies exceptions not to check for usage: catch arguments whose names match a regexp pattern. For example, variables whose names begin with a string 'ignore'.
Examples of correct code for the { "caughtErrorsIgnorePattern": "^ignore" }
option:
/*eslint no-unused-vars: ["error", { "caughtErrorsIgnorePattern": "^ignore" }]*/
try {
//...
} catch (ignoreErr) {
console.error("errors");
}
When Not To Use It
If you don't want to be notified about unused variables or function arguments, you can safely turn this rule off. Source: http://eslint.org/docs/rules/
Expected blank line after variable declarations. Open
const extension = path.extname(filePath);
- Read upRead up
- Exclude checks
require or disallow an empty line after variable declarations (newline-after-var)
This rule was deprecated in ESLint v4.0.0 and replaced by the [padding-line-between-statements](padding-line-between-statements.md) rule.
As of today there is no consistency in separating variable declarations from the rest of the code. Some developers leave an empty line between var statements and the rest of the code like:
var foo;
// do something with foo
Whereas others don't leave any empty newlines at all.
var foo;
// do something with foo
The problem is when these developers work together in a project. This rule enforces a coding style where empty newlines are allowed or disallowed after var
, let
, or const
statements. It helps the code to look consistent across the entire project.
Rule Details
This rule enforces a coding style where empty lines are required or disallowed after var
, let
, or const
statements to achieve a consistent coding style across the project.
Options
This rule has a string option:
-
"always"
(default) requires an empty line aftervar
,let
, orconst
Comments on a line directly after var statements are treated like additional var statements.
-
"never"
disallows empty lines aftervar
,let
, orconst
always
Examples of incorrect code for this rule with the default "always"
option:
/*eslint newline-after-var: ["error", "always"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
Examples of correct code for this rule with the default "always"
option:
/*eslint newline-after-var: ["error", "always"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
never
Examples of incorrect code for this rule with the "never"
option:
/*eslint newline-after-var: ["error", "never"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
Examples of correct code for this rule with the "never"
option:
/*eslint newline-after-var: ["error", "never"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
Source: http://eslint.org/docs/rules/
Expected newline before return statement. Open
return require(filePath);
- Read upRead up
- Exclude checks
require an empty line before return
statements (newline-before-return)
This rule was deprecated in ESLint v4.0.0 and replaced by the [padding-line-between-statements](padding-line-between-statements.md) rule.
There is no hard and fast rule about whether empty lines should precede return
statements in JavaScript. However, clearly delineating where a function is returning can greatly increase the readability and clarity of the code. For example:
function foo(bar) {
var baz = 'baz';
if (!bar) {
bar = baz;
return bar;
}
return bar;
}
Adding newlines visibly separates the return statements from the previous lines, making it clear where the function exits and what value it returns:
function foo(bar) {
var baz = 'baz';
if (!bar) {
bar = baz;
return bar;
}
return bar;
}
Rule Details
This rule requires an empty line before return
statements to increase code clarity, except when the return
is alone inside a statement group (such as an if statement). In the latter case, the return
statement does not need to be delineated by virtue of it being alone. Comments are ignored and do not count as empty lines.
Examples of incorrect code for this rule:
/*eslint newline-before-return: "error"*/
function foo(bar) {
if (!bar) {
return;
}
return bar;
}
function foo(bar) {
if (!bar) {
return;
}
/* multi-line
comment */
return bar;
}
Examples of correct code for this rule:
/*eslint newline-before-return: "error"*/
function foo() {
return;
}
function foo() {
return;
}
function foo(bar) {
if (!bar) return;
}
function foo(bar) {
if (!bar) { return };
}
function foo(bar) {
if (!bar) {
return;
}
}
function foo(bar) {
if (!bar) {
return;
}
return bar;
}
function foo(bar) {
if (!bar) {
return;
}
}
function foo() {
// comment
return;
}
When Not To Use It
You can safely disable this rule if you do not have any strict conventions about whitespace before return
statements.
Related Rules
- [newline-after-var](newline-after-var.md) Source: http://eslint.org/docs/rules/
Expected blank line after variable declarations. Open
const response = await request([endpoints.device, {deviceId}], authorizeHttp, function(res) {
- Read upRead up
- Exclude checks
require or disallow an empty line after variable declarations (newline-after-var)
This rule was deprecated in ESLint v4.0.0 and replaced by the [padding-line-between-statements](padding-line-between-statements.md) rule.
As of today there is no consistency in separating variable declarations from the rest of the code. Some developers leave an empty line between var statements and the rest of the code like:
var foo;
// do something with foo
Whereas others don't leave any empty newlines at all.
var foo;
// do something with foo
The problem is when these developers work together in a project. This rule enforces a coding style where empty newlines are allowed or disallowed after var
, let
, or const
statements. It helps the code to look consistent across the entire project.
Rule Details
This rule enforces a coding style where empty lines are required or disallowed after var
, let
, or const
statements to achieve a consistent coding style across the project.
Options
This rule has a string option:
-
"always"
(default) requires an empty line aftervar
,let
, orconst
Comments on a line directly after var statements are treated like additional var statements.
-
"never"
disallows empty lines aftervar
,let
, orconst
always
Examples of incorrect code for this rule with the default "always"
option:
/*eslint newline-after-var: ["error", "always"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
Examples of correct code for this rule with the default "always"
option:
/*eslint newline-after-var: ["error", "always"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
never
Examples of incorrect code for this rule with the "never"
option:
/*eslint newline-after-var: ["error", "never"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
Examples of correct code for this rule with the "never"
option:
/*eslint newline-after-var: ["error", "never"]*/
/*eslint-env es6*/
var greet = "hello,",
name = "world";
console.log(greet, name);
let greet = "hello,",
name = "world";
console.log(greet, name);
var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);
var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
Source: http://eslint.org/docs/rules/
Requires a space before '}' Open
} catch (e) {/**/}
- Read upRead up
- Exclude checks
Disallow or enforce spaces inside of blocks after opening block and before closing block (block-spacing)
Rule Details
This rule enforces consistent spacing inside an open block token and the next token on the same line. This rule also enforces consistent spacing inside a close block token and previous token on the same line.
Options
This rule has a string option:
-
"always"
(default) requires one or more spaces -
"never"
disallows spaces
always
Examples of incorrect code for this rule with the default "always"
option:
/*eslint block-spacing: "error"*/
function foo() {return true;}
if (foo) { bar = 0;}
function baz() {let i = 0;
return i;
}
Examples of correct code for this rule with the default "always"
option:
/*eslint block-spacing: "error"*/
function foo() { return true; }
if (foo) { bar = 0; }
never
Examples of incorrect code for this rule with the "never"
option:
/*eslint block-spacing: ["error", "never"]*/
function foo() { return true; }
if (foo) { bar = 0;}
Examples of correct code for this rule with the "never"
option:
/*eslint block-spacing: ["error", "never"]*/
function foo() {return true;}
if (foo) {bar = 0;}
When Not To Use It
If you don't want to be notified about spacing style inside of blocks, you can safely disable this rule. Source: http://eslint.org/docs/rules/
'logger' is assigned a value but never used. Open
const {logger} = classInstance;
- Read upRead up
- Exclude checks
Disallow Unused Variables (no-unused-vars)
Variables that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. Such variables take up space in the code and can lead to confusion by readers.
Rule Details
This rule is aimed at eliminating unused variables, functions, and parameters of functions.
A variable is considered to be used if any of the following are true:
- It represents a function that is called (
doSomething()
) - It is read (
var y = x
) - It is passed into a function as an argument (
doSomething(x)
) - It is read inside of a function that is passed to another function (
doSomething(function() { foo(); })
)
A variable is not considered to be used if it is only ever assigned to (var x = 5
) or declared.
Examples of incorrect code for this rule:
/*eslint no-unused-vars: "error"*/
/*global some_unused_var*/
// It checks variables you have defined as global
some_unused_var = 42;
var x;
// Write-only variables are not considered as used.
var y = 10;
y = 5;
// A read for a modification of itself is not considered as used.
var z = 0;
z = z + 1;
// By default, unused arguments cause warnings.
(function(foo) {
return 5;
})();
// Unused recursive functions also cause warnings.
function fact(n) {
if (n < 2) return 1;
return n * fact(n - 1);
}
// When a function definition destructures an array, unused entries from the array also cause warnings.
function getY([x, y]) {
return y;
}
Examples of correct code for this rule:
/*eslint no-unused-vars: "error"*/
var x = 10;
alert(x);
// foo is considered used here
myFunc(function foo() {
// ...
}.bind(this));
(function(foo) {
return foo;
})();
var myFunc;
myFunc = setTimeout(function() {
// myFunc is considered used
myFunc();
}, 50);
// Only the second argument from the descructured array is used.
function getY([, y]) {
return y;
}
exported
In environments outside of CommonJS or ECMAScript modules, you may use var
to create a global variable that may be used by other scripts. You can use the /* exported variableName */
comment block to indicate that this variable is being exported and therefore should not be considered unused.
Note that /* exported */
has no effect for any of the following:
- when the environment is
node
orcommonjs
- when
parserOptions.sourceType
ismodule
- when
ecmaFeatures.globalReturn
istrue
The line comment // exported variableName
will not work as exported
is not line-specific.
Examples of correct code for /* exported variableName */
operation:
/* exported global_var */
var global_var = 42;
Options
This rule takes one argument which can be a string or an object. The string settings are the same as those of the vars
property (explained below).
By default this rule is enabled with all
option for variables and after-used
for arguments.
{
"rules": {
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }]
}
}
vars
The vars
option has two settings:
-
all
checks all variables for usage, including those in the global scope. This is the default setting. -
local
checks only that locally-declared variables are used but will allow global variables to be unused.
vars: local
Examples of correct code for the { "vars": "local" }
option:
/*eslint no-unused-vars: ["error", { "vars": "local" }]*/
/*global some_unused_var */
some_unused_var = 42;
varsIgnorePattern
The varsIgnorePattern
option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain ignored
or Ignored
.
Examples of correct code for the { "varsIgnorePattern": "[iI]gnored" }
option:
/*eslint no-unused-vars: ["error", { "varsIgnorePattern": "[iI]gnored" }]*/
var firstVarIgnored = 1;
var secondVar = 2;
console.log(secondVar);
args
The args
option has three settings:
-
after-used
- only the last argument must be used. This allows you, for instance, to have two named parameters to a function and as long as you use the second argument, ESLint will not warn you about the first. This is the default setting. -
all
- all named arguments must be used. -
none
- do not check arguments.
args: after-used
Examples of incorrect code for the default { "args": "after-used" }
option:
/*eslint no-unused-vars: ["error", { "args": "after-used" }]*/
// 1 error
// "baz" is defined but never used
(function(foo, bar, baz) {
return bar;
})();
Examples of correct code for the default { "args": "after-used" }
option:
/*eslint no-unused-vars: ["error", {"args": "after-used"}]*/
(function(foo, bar, baz) {
return baz;
})();
args: all
Examples of incorrect code for the { "args": "all" }
option:
/*eslint no-unused-vars: ["error", { "args": "all" }]*/
// 2 errors
// "foo" is defined but never used
// "baz" is defined but never used
(function(foo, bar, baz) {
return bar;
})();
args: none
Examples of correct code for the { "args": "none" }
option:
/*eslint no-unused-vars: ["error", { "args": "none" }]*/
(function(foo, bar, baz) {
return bar;
})();
ignoreRestSiblings
The ignoreRestSiblings
option is a boolean (default: false
). Using a Rest Property it is possible to "omit" properties from an object, but by default the sibling properties are marked as "unused". With this option enabled the rest property's siblings are ignored.
Examples of correct code for the { "ignoreRestSiblings": true }
option:
/*eslint no-unused-vars: ["error", { "ignoreRestSiblings": true }]*/
// 'type' is ignored because it has a rest property sibling.
var { type, ...coords } = data;
argsIgnorePattern
The argsIgnorePattern
option specifies exceptions not to check for usage: arguments whose names match a regexp pattern. For example, variables whose names begin with an underscore.
Examples of correct code for the { "argsIgnorePattern": "^_" }
option:
/*eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }]*/
function foo(x, _y) {
return x + 1;
}
foo();
caughtErrors
The caughtErrors
option is used for catch
block arguments validation.
It has two settings:
-
none
- do not check error objects. This is the default setting. -
all
- all named arguments must be used.
caughtErrors: none
Not specifying this rule is equivalent of assigning it to none
.
Examples of correct code for the { "caughtErrors": "none" }
option:
/*eslint no-unused-vars: ["error", { "caughtErrors": "none" }]*/
try {
//...
} catch (err) {
console.error("errors");
}
caughtErrors: all
Examples of incorrect code for the { "caughtErrors": "all" }
option:
/*eslint no-unused-vars: ["error", { "caughtErrors": "all" }]*/
// 1 error
// "err" is defined but never used
try {
//...
} catch (err) {
console.error("errors");
}
caughtErrorsIgnorePattern
The caughtErrorsIgnorePattern
option specifies exceptions not to check for usage: catch arguments whose names match a regexp pattern. For example, variables whose names begin with a string 'ignore'.
Examples of correct code for the { "caughtErrorsIgnorePattern": "^ignore" }
option:
/*eslint no-unused-vars: ["error", { "caughtErrorsIgnorePattern": "^ignore" }]*/
try {
//...
} catch (ignoreErr) {
console.error("errors");
}
When Not To Use It
If you don't want to be notified about unused variables or function arguments, you can safely turn this rule off. Source: http://eslint.org/docs/rules/
'devices' is never reassigned. Use 'const' instead. Open
let devices = [];
- Read upRead up
- Exclude checks
Suggest using const
(prefer-const)
If a variable is never reassigned, using the const
declaration is better.
const
declaration tells readers, "this variable is never reassigned," reducing cognitive load and improving maintainability.
Rule Details
This rule is aimed at flagging variables that are declared using let
keyword, but never reassigned after the initial assignment.
Examples of incorrect code for this rule:
/*eslint prefer-const: "error"*/
/*eslint-env es6*/
// it's initialized and never reassigned.
let a = 3;
console.log(a);
let a;
a = 0;
console.log(a);
// `i` is redefined (not reassigned) on each loop step.
for (let i in [1, 2, 3]) {
console.log(i);
}
// `a` is redefined (not reassigned) on each loop step.
for (let a of [1, 2, 3]) {
console.log(a);
}
Examples of correct code for this rule:
/*eslint prefer-const: "error"*/
/*eslint-env es6*/
// using const.
const a = 0;
// it's never initialized.
let a;
console.log(a);
// it's reassigned after initialized.
let a;
a = 0;
a = 1;
console.log(a);
// it's initialized in a different block from the declaration.
let a;
if (true) {
a = 0;
}
console.log(a);
// it's initialized at a place that we cannot write a variable declaration.
let a;
if (true) a = 0;
console.log(a);
// `i` gets a new binding each iteration
for (const i in [1, 2, 3]) {
console.log(i);
}
// `a` gets a new binding each iteration
for (const a of [1, 2, 3]) {
console.log(a);
}
// `end` is never reassigned, but we cannot separate the declarations without modifying the scope.
for (let i = 0, end = 10; i < end; ++i) {
console.log(a);
}
// suggest to use `no-var` rule.
var b = 3;
console.log(b);
Options
{
"prefer-const": ["error", {
"destructuring": "any",
"ignoreReadBeforeAssign": false
}]
}
destructuring
The kind of the way to address variables in destructuring. There are 2 values:
-
"any"
(default) - If any variables in destructuring should beconst
, this rule warns for those variables. -
"all"
- If all variables in destructuring should beconst
, this rule warns the variables. Otherwise, ignores them.
Examples of incorrect code for the default {"destructuring": "any"}
option:
/*eslint prefer-const: "error"*/
/*eslint-env es6*/
let {a, b} = obj; /*error 'b' is never reassigned, use 'const' instead.*/
a = a + 1;
Examples of correct code for the default {"destructuring": "any"}
option:
/*eslint prefer-const: "error"*/
/*eslint-env es6*/
// using const.
const {a: a0, b} = obj;
const a = a0 + 1;
// all variables are reassigned.
let {a, b} = obj;
a = a + 1;
b = b + 1;
Examples of incorrect code for the {"destructuring": "all"}
option:
/*eslint prefer-const: ["error", {"destructuring": "all"}]*/
/*eslint-env es6*/
// all of `a` and `b` should be const, so those are warned.
let {a, b} = obj; /*error 'a' is never reassigned, use 'const' instead.
'b' is never reassigned, use 'const' instead.*/
Examples of correct code for the {"destructuring": "all"}
option:
/*eslint prefer-const: ["error", {"destructuring": "all"}]*/
/*eslint-env es6*/
// 'b' is never reassigned, but all of `a` and `b` should not be const, so those are ignored.
let {a, b} = obj;
a = a + 1;
ignoreReadBeforeAssign
This is an option to avoid conflicting with no-use-before-define
rule (without "nofunc"
option).
If true
is specified, this rule will ignore variables that are read between the declaration and the first assignment.
Default is false
.
Examples of correct code for the {"ignoreReadBeforeAssign": true}
option:
/*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": true}]*/
/*eslint-env es6*/
let timer;
function initialize() {
if (foo()) {
clearInterval(timer);
}
}
timer = setInterval(initialize, 100);
Examples of correct code for the default {"ignoreReadBeforeAssign": false}
option:
/*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": false}]*/
/*eslint-env es6*/
const timer = setInterval(initialize, 100);
function initialize() {
if (foo()) {
clearInterval(timer);
}
}
When Not To Use It
If you don't want to be notified about variables that are never reassigned after initial assignment, you can safely disable this rule.
Related Rules
- [no-var](no-var.md)
- [no-use-before-define](no-use-before-define.md) Source: http://eslint.org/docs/rules/
Missing space before value for key 'extra'. Open
await captureException(e, {extra:{networkingError: true}});
- Read upRead up
- Exclude checks
enforce consistent spacing between keys and values in object literal properties (key-spacing)
This rule enforces spacing around the colon in object literal properties. It can verify each property individually, or it can ensure horizontal alignment of adjacent properties in an object literal.
Rule Details
This rule enforces consistent spacing between keys and values in object literal properties. In the case of long lines, it is acceptable to add a new line wherever whitespace is allowed.
Options
This rule has an object option:
-
"beforeColon": false (default) | true
-
false
: disallows spaces between the key and the colon in object literals. -
true
: requires at least one space between the key and the colon in object literals.
-
-
"afterColon": true (default) | false
-
true
: requires at least one space between the colon and the value in object literals. -
false
: disallows spaces between the colon and the value in object literals.
-
-
"mode": "strict" (default) | "minimum"
-
"strict"
: enforces exactly one space before or after colons in object literals. -
"minimum"
: enforces one or more spaces before or after colons in object literals.
-
-
"align": "value" | "colon"
-
"value"
: enforces horizontal alignment of values in object literals. -
"colon"
enforces horizontal alignment of both colons and values in object literals.
-
-
"align"
with an object value allows for fine-grained spacing when values are being aligned in object literals. -
"singleLine"
specifies a spacing style for single-line object literals. -
"multiLine"
specifies a spacing style for multi-line object literals.
Please note that you can either use the top-level options or the grouped options (singleLine
and multiLine
) but not both.
beforeColon
Examples of incorrect code for this rule with the default { "beforeColon": false }
option:
/*eslint key-spacing: ["error", { "beforeColon": false }]*/
var obj = { "foo" : 42 };
Examples of correct code for this rule with the default { "beforeColon": false }
option:
/*eslint key-spacing: ["error", { "beforeColon": false }]*/
var obj = { "foo": 42 };
Examples of incorrect code for this rule with the { "beforeColon": true }
option:
/*eslint key-spacing: ["error", { "beforeColon": true }]*/
var obj = { "foo": 42 };
Examples of correct code for this rule with the { "beforeColon": true }
option:
/*eslint key-spacing: ["error", { "beforeColon": true }]*/
var obj = { "foo" : 42 };
afterColon
Examples of incorrect code for this rule with the default { "afterColon": true }
option:
/*eslint key-spacing: ["error", { "afterColon": true }]*/
var obj = { "foo":42 };
Examples of correct code for this rule with the default { "afterColon": true }
option:
/*eslint key-spacing: ["error", { "afterColon": true }]*/
var obj = { "foo": 42 };
Examples of incorrect code for this rule with the { "afterColon": false }
option:
/*eslint key-spacing: ["error", { "afterColon": false }]*/
var obj = { "foo": 42 };
Examples of correct code for this rule with the { "afterColon": false }
option:
/*eslint key-spacing: ["error", { "afterColon": false }]*/
var obj = { "foo":42 };
mode
Examples of incorrect code for this rule with the default { "mode": "strict" }
option:
/*eslint key-spacing: ["error", { "mode": "strict" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the default { "mode": "strict" }
option:
/*eslint key-spacing: ["error", { "mode": "strict" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the { "mode": "minimum" }
option:
/*eslint key-spacing: ["error", { "mode": "minimum" }]*/
call({
foobar: 42,
bat: 2 * 2
});
align
Examples of incorrect code for this rule with the { "align": "value" }
option:
/*eslint key-spacing: ["error", { "align": "value" }]*/
var obj = {
a: value,
bcde: 42,
fg : foo()
};
Examples of correct code for this rule with the { "align": "value" }
option:
/*eslint key-spacing: ["error", { "align": "value" }]*/
var obj = {
a: value,
bcde: 42,
fg: foo(),
h: function() {
return this.a;
},
ijkl: 'Non-consecutive lines form a new group'
};
var obj = { a: "foo", longPropertyName: "bar" };
Examples of incorrect code for this rule with the { "align": "colon" }
option:
/*eslint key-spacing: ["error", { "align": "colon" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the { "align": "colon" }
option:
/*eslint key-spacing: ["error", { "align": "colon" }]*/
call({
foobar: 42,
bat : 2 * 2
});
align
The align
option can take additional configuration through the beforeColon
, afterColon
, mode
, and on
options.
If align
is defined as an object, but not all of the parameters are provided, undefined parameters will default to the following:
// Defaults
align: {
"beforeColon": false,
"afterColon": true,
"on": "colon",
"mode": "strict"
}
Examples of correct code for this rule with sample { "align": { } }
options:
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"one" : 1,
"seven" : 7
}
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": false,
"afterColon": false,
"on": "value"
}
}]*/
var obj = {
"one": 1,
"seven":7
}
align and multiLine
The multiLine
and align
options can differ, which allows for fine-tuned control over the key-spacing
of your files. align
will not inherit from multiLine
if align
is configured as an object.
multiLine
is used any time an object literal spans multiple lines. The align
configuration is used when there is a group of properties in the same object. For example:
var myObj = {
key1: 1, // uses multiLine
key2: 2, // uses align (when defined)
key3: 3, // uses align (when defined)
key4: 4 // uses multiLine
}
Examples of incorrect code for this rule with sample { "align": { }, "multiLine": { } }
options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon":true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"myObjectFunction": function() {
// Do something
},
"one" : 1,
"seven" : 7
}
Examples of correct code for this rule with sample { "align": { }, "multiLine": { } }
options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon": true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"myObjectFunction": function() {
// Do something
//
}, // These are two separate groups, so no alignment between `myObjectFuction` and `one`
"one" : 1,
"seven" : 7 // `one` and `seven` are in their own group, and therefore aligned
}
singleLine and multiLine
Examples of correct code for this rule with sample { "singleLine": { }, "multiLine": { } }
options:
/*eslint "key-spacing": [2, {
"singleLine": {
"beforeColon": false,
"afterColon": true
},
"multiLine": {
"beforeColon": true,
"afterColon": true,
"align": "colon"
}
}]*/
var obj = { one: 1, "two": 2, three: 3 };
var obj2 = {
"two" : 2,
three : 3
};
When Not To Use It
If you have another convention for property spacing that might not be consistent with the available options, or if you want to permit multiple styles concurrently you can safely disable this rule. Source: http://eslint.org/docs/rules/
Identifier 'NON_EMPTY_STRING_OR_NUll' is not in camel case. Open
NON_EMPTY_STRING_OR_NUll: Symbol('nonEmptyStringOrNull'),
- Read upRead up
- Exclude checks
Require CamelCase (camelcase)
When it comes to naming variables, style guides generally fall into one of two camps: camelcase (variableName
) and underscores (variable_name
). This rule focuses on using the camelcase approach. If your style guide calls for camelCasing your variable names, then this rule is for you!
Rule Details
This rule looks for any underscores (_
) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name. If ESLint decides that the variable is a constant (all uppercase), then no warning will be thrown. Otherwise, a warning will be thrown. This rule only flags definitions and assignments but not function calls. In case of ES6 import
statements, this rule only targets the name of the variable that will be imported into the local module scope.
Options
This rule has an object option:
-
"properties": "always"
(default) enforces camelcase style for property names -
"properties": "never"
does not check property names
always
Examples of incorrect code for this rule with the default { "properties": "always" }
option:
/*eslint camelcase: "error"*/
import { no_camelcased } from "external-module"
var my_favorite_color = "#112C85";
function do_something() {
// ...
}
obj.do_something = function() {
// ...
};
function foo({ no_camelcased }) {
// ...
};
function foo({ isCamelcased: no_camelcased }) {
// ...
}
function foo({ no_camelcased = 'default value' }) {
// ...
};
var obj = {
my_pref: 1
};
var { category_id = 1 } = query;
var { foo: no_camelcased } = bar;
var { foo: bar_baz = 1 } = quz;
Examples of correct code for this rule with the default { "properties": "always" }
option:
/*eslint camelcase: "error"*/
import { no_camelcased as camelCased } from "external-module";
var myFavoriteColor = "#112C85";
var _myFavoriteColor = "#112C85";
var myFavoriteColor_ = "#112C85";
var MY_FAVORITE_COLOR = "#112C85";
var foo = bar.baz_boom;
var foo = { qux: bar.baz_boom };
obj.do_something();
do_something();
new do_something();
var { category_id: category } = query;
function foo({ isCamelCased }) {
// ...
};
function foo({ isCamelCased: isAlsoCamelCased }) {
// ...
}
function foo({ isCamelCased = 'default value' }) {
// ...
};
var { categoryId = 1 } = query;
var { foo: isCamelCased } = bar;
var { foo: isCamelCased = 1 } = quz;
never
Examples of correct code for this rule with the { "properties": "never" }
option:
/*eslint camelcase: ["error", {properties: "never"}]*/
var obj = {
my_pref: 1
};
When Not To Use It
If you have established coding standards using a different naming convention (separating words with underscores), turn this rule off. Source: http://eslint.org/docs/rules/
Requires a space after '{' Open
} catch (e) {/**/}
- Read upRead up
- Exclude checks
Disallow or enforce spaces inside of blocks after opening block and before closing block (block-spacing)
Rule Details
This rule enforces consistent spacing inside an open block token and the next token on the same line. This rule also enforces consistent spacing inside a close block token and previous token on the same line.
Options
This rule has a string option:
-
"always"
(default) requires one or more spaces -
"never"
disallows spaces
always
Examples of incorrect code for this rule with the default "always"
option:
/*eslint block-spacing: "error"*/
function foo() {return true;}
if (foo) { bar = 0;}
function baz() {let i = 0;
return i;
}
Examples of correct code for this rule with the default "always"
option:
/*eslint block-spacing: "error"*/
function foo() { return true; }
if (foo) { bar = 0; }
never
Examples of incorrect code for this rule with the "never"
option:
/*eslint block-spacing: ["error", "never"]*/
function foo() { return true; }
if (foo) { bar = 0;}
Examples of correct code for this rule with the "never"
option:
/*eslint block-spacing: ["error", "never"]*/
function foo() {return true;}
if (foo) {bar = 0;}
When Not To Use It
If you don't want to be notified about spacing style inside of blocks, you can safely disable this rule. Source: http://eslint.org/docs/rules/