Function transformCore
has 42 lines of code (exceeds 25 allowed). Consider refactoring. Open
transformCore(config, file, callback) {
if (file.isNull()) {
callback(null, file);
return;
}
Function activatePostCSSPlugins
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
function activatePostCSSPlugins(config) {
const plugins = [];
if (config.lintRules || config.lintRules !== "") {
// TODO: Throw easy-to-understand error message
- 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
Identifier name 'activatePostCSSPlugins' is too long (> 16). Open
function activatePostCSSPlugins(config) {
- Read upRead up
- Exclude checks
enforce minimum and maximum identifier lengths (id-length)
Very short identifier names like e
, x
, _t
or very long ones like hashGeneratorResultOutputContainerObject
can make code harder to read and potentially less maintainable. To prevent this, one may enforce a minimum and/or maximum identifier length.
var x = 5; // too short; difficult to understand its purpose without context
Rule Details
This rule enforces a minimum and/or maximum identifier length convention.
Options
Examples of incorrect code for this rule with the default options:
/*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
/*eslint-env es6*/
var x = 5;
obj.e = document.body;
var foo = function (e) { };
try {
dangerousStuff();
} catch (e) {
// ignore as many do
}
var myObj = { a: 1 };
(a) => { a * a };
class x { }
class Foo { x() {} }
function foo(...x) { }
var { x } = {};
var { x: a} = {};
var { a: [x]} = {};
({ prop: obj.x }) = {};
Examples of correct code for this rule with the default options:
/*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
/*eslint-env es6*/
var num = 5;
function _f() { return 42; }
function _func() { return 42; }
obj.el = document.body;
var foo = function (evt) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
// ignore as many do
}
var myObj = { apple: 1 };
(num) => { num * num };
function foo(num = 0) { }
class MyClass { }
class Foo { method() {} }
function foo(...args) { }
var { prop } = {};
var { prop: a } = {};
var { prop: [x] } = {};
({ prop: obj.longName }) = {};
var data = { "x": 1 }; // excused because of quotes
data["y"] = 3; // excused because of calculated property access
This rule has a shorthand integer option for the "min"
object property.
Examples of incorrect code for this rule with a minimum of 4:
/*eslint id-length: ["error", 4]*/
/*eslint-env es6*/
var val = 5;
obj.e = document.body;
function (e) { };
try {
dangerousStuff();
} catch (e) {
// ignore as many do
}
var myObj = { a: 1 };
(val) => { val * val };
class x { }
class Foo { x() {} }
function foo(...x) { }
var { x } = {};
var { x: a} = {};
var { a: [x]} = {};
({ prop: obj.x }) = {};
Examples of correct code for this rule with a minimum of 4:
/*eslint id-length: ["error", 4]*/
/*eslint-env es6*/
var value = 5;
function func() { return 42; }
obj.element = document.body;
var foo = function (event) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
// ignore as many do
}
var myObj = { apple: 1 };
(value) => { value * value };
function foobar(value = 0) { }
class MyClass { }
class Foobar { method() {} }
function foobar(...args) { }
var { prop } = {};
var { prop: a } = {};
var { prop: [x] } = {};
({ prop: obj.name }) = {};
var data = { "x": 1 }; // excused because of quotes
data["y"] = 3; // excused because of calculated property access
This rule has an object option:
-
"min"
(default: 2) enforces a minimum identifier length -
"max"
(default: Infinity) enforces a maximum identifier length -
"properties": always
(default) enforces identifier length convention for property names -
"properties": never
ignores identifier length convention for property names -
"exceptions"
allows an array of specified identifier names
min
Examples of incorrect code for this rule with the { "min": 4 }
option:
/*eslint id-length: ["error", { "min": 4 }]*/
/*eslint-env es6*/
var val = 5;
obj.e = document.body;
function (e) { };
try {
dangerousStuff();
} catch (e) {
// ignore as many do
}
var myObj = { a: 1 };
(val) => { val * val };
class x { }
class Foo { x() {} }
function foo(...x) { }
var { x } = {};
var { x: a} = {};
var { a: [x]} = {};
({ prop: obj.x }) = {};
Examples of correct code for this rule with the { "min": 4 }
option:
/*eslint id-length: ["error", { "min": 4 }]*/
/*eslint-env es6*/
var value = 5;
function func() { return 42; }
obj.element = document.body;
var foo = function (event) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
// ignore as many do
}
var myObj = { apple: 1 };
(value) => { value * value };
function foobar(value = 0) { }
class MyClass { }
class Foobar { method() {} }
function foobar(...args) { }
var { prop } = {};
var { prop: a } = {};
var { prop: [x] } = {};
({ prop: obj.name }) = {};
var data = { "x": 1 }; // excused because of quotes
data["y"] = 3; // excused because of calculated property access
max
Examples of incorrect code for this rule with the { "max": 10 }
option:
/*eslint id-length: ["error", { "max": "10" }]*/
/*eslint-env es6*/
var reallyLongVarName = 5;
function reallyLongFuncName() { return 42; }
obj.reallyLongPropName = document.body;
var foo = function (reallyLongArgName) { /* do stuff */ };
try {
dangerousStuff();
} catch (reallyLongErrorName) {
// ignore as many do
}
(reallyLongArgName) => { return !reallyLongArgName; };
Examples of correct code for this rule with the { "max": 10 }
option:
/*eslint id-length: ["error", { "max": "10" }]*/
/*eslint-env es6*/
var varName = 5;
function funcName() { return 42; }
obj.propName = document.body;
var foo = function (arg) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
// ignore as many do
}
(arg) => { return !arg; };
properties
Examples of correct code for this rule with the { "properties": "never" }
option:
/*eslint id-length: ["error", { "properties": "never" }]*/
/*eslint-env es6*/
var myObj = { a: 1 };
({ a: obj.x.y.z }) = {};
({ prop: obj.i }) = {};
exceptions
Examples of additional correct code for this rule with the { "exceptions": ["x"] }
option:
/*eslint id-length: ["error", { "exceptions": ["x"] }]*/
/*eslint-env es6*/
var x = 5;
function x() { return 42; }
obj.x = document.body;
var foo = function (x) { /* do stuff */ };
try {
dangerousStuff();
} catch (x) {
// ignore as many do
}
(x) => { return x * x; };
Related Rules
- [max-len](max-len.md)
- [new-cap](new-cap.md)
- [func-names](func-names.md)
- [camelcase](camelcase.md) Source: http://eslint.org/docs/rules/
Expected line before comment. Open
// TODO: Throw easy-to-understand error message
- Read upRead up
- Exclude checks
require empty lines around comments (lines-around-comment)
Many style guides require empty lines before or after comments. The primary goal of these rules is to make the comments easier to read and improve readability of the code.
Rule Details
This rule requires empty lines before and/or after comments. It can be enabled separately for both block (/*
) and line (//
) comments. This rule does not apply to comments that appear on the same line as code and does not require empty lines at the beginning or end of a file.
Options
This rule has an object option:
-
"beforeBlockComment": true
(default) requires an empty line before block comments -
"afterBlockComment": true
requires an empty line after block comments -
"beforeLineComment": true
requires an empty line before line comments -
"afterLineComment": true
requires an empty line after line comments -
"allowBlockStart": true
allows comments to appear at the start of block statements -
"allowBlockEnd": true
allows comments to appear at the end of block statements -
"allowObjectStart": true
allows comments to appear at the start of object literals -
"allowObjectEnd": true
allows comments to appear at the end of object literals -
"allowArrayStart": true
allows comments to appear at the start of array literals -
"allowArrayEnd": true
allows comments to appear at the end of array literals -
"applyDefaultIgnorePatterns"
enables or disables the default comment patterns to be ignored by the rule -
"ignorePattern"
custom patterns to be ignored by the rule
beforeBlockComment
Examples of incorrect code for this rule with the default { "beforeBlockComment": true }
option:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/
var night = "long";
/* what a great and wonderful day */
var day = "great"
Examples of correct code for this rule with the default { "beforeBlockComment": true }
option:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/
var night = "long";
/* what a great and wonderful day */
var day = "great"
afterBlockComment
Examples of incorrect code for this rule with the { "afterBlockComment": true }
option:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/
var night = "long";
/* what a great and wonderful day */
var day = "great"
Examples of correct code for this rule with the { "afterBlockComment": true }
option:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/
var night = "long";
/* what a great and wonderful day */
var day = "great"
beforeLineComment
Examples of incorrect code for this rule with the { "beforeLineComment": true }
option:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/
var night = "long";
// what a great and wonderful day
var day = "great"
Examples of correct code for this rule with the { "beforeLineComment": true }
option:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/
var night = "long";
// what a great and wonderful day
var day = "great"
afterLineComment
Examples of incorrect code for this rule with the { "afterLineComment": true }
option:
/*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/
var night = "long";
// what a great and wonderful day
var day = "great"
Examples of correct code for this rule with the { "afterLineComment": true }
option:
/*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/
var night = "long";
// what a great and wonderful day
var day = "great"
allowBlockStart
Examples of correct code for this rule with the { "beforeLineComment": true, "allowBlockStart": true }
options:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowBlockStart": true }]*/
function foo(){
// what a great and wonderful day
var day = "great"
return day;
}
Examples of correct code for this rule with the { "beforeBlockComment": true, "allowBlockStart": true }
options:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowBlockStart": true }]*/
function foo(){
/* what a great and wonderful day */
var day = "great"
return day;
}
allowBlockEnd
Examples of correct code for this rule with the { "afterLineComment": true, "allowBlockEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowBlockEnd": true }]*/
function foo(){
var day = "great"
return day;
// what a great and wonderful day
}
Examples of correct code for this rule with the { "afterBlockComment": true, "allowBlockEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowBlockEnd": true }]*/
function foo(){
var day = "great"
return day;
/* what a great and wonderful day */
}
allowObjectStart
Examples of correct code for this rule with the { "beforeLineComment": true, "allowObjectStart": true }
option:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowObjectStart": true }]*/
var foo = {
// what a great and wonderful day
day: "great"
};
const {
// what a great and wonderful day
foo: someDay
} = {foo: "great"};
const {
// what a great and wonderful day
day
} = {day: "great"};
Examples of correct code for this rule with the { "beforeBlockComment": true, "allowObjectStart": true }
option:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowObjectStart": true }]*/
var foo = {
/* what a great and wonderful day */
day: "great"
};
const {
/* what a great and wonderful day */
foo: someDay
} = {foo: "great"};
const {
/* what a great and wonderful day */
day
} = {day: "great"};
allowObjectEnd
Examples of correct code for this rule with the { "afterLineComment": true, "allowObjectEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowObjectEnd": true }]*/
var foo = {
day: "great"
// what a great and wonderful day
};
const {
foo: someDay
// what a great and wonderful day
} = {foo: "great"};
const {
day
// what a great and wonderful day
} = {day: "great"};
Examples of correct code for this rule with the { "afterBlockComment": true, "allowObjectEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowObjectEnd": true }]*/
var foo = {
day: "great"
/* what a great and wonderful day */
};
const {
foo: someDay
/* what a great and wonderful day */
} = {foo: "great"};
const {
day
/* what a great and wonderful day */
} = {day: "great"};
allowArrayStart
Examples of correct code for this rule with the { "beforeLineComment": true, "allowArrayStart": true }
option:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowArrayStart": true }]*/
var day = [
// what a great and wonderful day
"great",
"wonderful"
];
const [
// what a great and wonderful day
someDay
] = ["great", "not great"];
Examples of correct code for this rule with the { "beforeBlockComment": true, "allowArrayStart": true }
option:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowArrayStart": true }]*/
var day = [
/* what a great and wonderful day */
"great",
"wonderful"
];
const [
/* what a great and wonderful day */
someDay
] = ["great", "not great"];
allowArrayEnd
Examples of correct code for this rule with the { "afterLineComment": true, "allowArrayEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowArrayEnd": true }]*/
var day = [
"great",
"wonderful"
// what a great and wonderful day
];
const [
someDay
// what a great and wonderful day
] = ["great", "not great"];
Examples of correct code for this rule with the { "afterBlockComment": true, "allowArrayEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowArrayEnd": true }]*/
var day = [
"great",
"wonderful"
/* what a great and wonderful day */
];
const [
someDay
/* what a great and wonderful day */
] = ["great", "not great"];
ignorePattern
By default this rule ignores comments starting with the following words: eslint
, jshint
, jslint
, istanbul
, global
, exported
, jscs
. An alternative regular expression can be provided.
Examples of correct code for the ignorePattern
option:
/*eslint lines-around-comment: ["error"]*/
foo();
/* eslint mentioned in this comment */,
bar();
/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */
foo();
/* a valid comment using pragma in it */
Examples of incorrect code for the ignorePattern
option:
/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */
1 + 1;
/* something else */
applyDefaultIgnorePatterns
Default ignore patterns are applied even when ignorePattern
is provided. If you want to omit default patterns, set this option to false
.
Examples of correct code for the { "applyDefaultIgnorePatterns": false }
option:
/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma", applyDefaultIgnorePatterns: false }] */
foo();
/* a valid comment using pragma in it */
Examples of incorrect code for the { "applyDefaultIgnorePatterns": false }
option:
/*eslint lines-around-comment: ["error", { "applyDefaultIgnorePatterns": false }] */
foo();
/* eslint mentioned in comment */
When Not To Use It
Many people enjoy a terser code style and don't mind comments bumping up against code. If you fall into that category this rule is not for you.
Related Rules
- [space-before-blocks](space-before-blocks.md)
- [spaced-comment](spaced-comment.md) Source: http://eslint.org/docs/rules/
Expected return with your callback function. Open
callback(null, file);
- Read upRead up
- Exclude checks
Enforce Return After Callback (callback-return)
The callback pattern is at the heart of most I/O and event-driven programming in JavaScript.
function doSomething(err, callback) {
if (err) {
return callback(err);
}
callback();
}
To prevent calling the callback multiple times it is important to return
anytime the callback is triggered outside
of the main function body. Neglecting this technique often leads to issues where you do something more than once.
For example, in the case of an HTTP request, you may try to send HTTP headers more than once leading Node.js to throw
a Can't render headers after they are sent to the client.
error.
Rule Details
This rule is aimed at ensuring that callbacks used outside of the main function block are always part-of or immediately
preceding a return
statement. This rule decides what is a callback based on the name of the function being called.
Options
The rule takes a single option - an array of possible callback names - which may include object methods. The default callback names are callback
, cb
, next
.
Default callback names
Examples of incorrect code for this rule with the default ["callback", "cb", "next"]
option:
/*eslint callback-return: "error"*/
function foo(err, callback) {
if (err) {
callback(err);
}
callback();
}
Examples of correct code for this rule with the default ["callback", "cb", "next"]
option:
/*eslint callback-return: "error"*/
function foo(err, callback) {
if (err) {
return callback(err);
}
callback();
}
Supplied callback names
Examples of incorrect code for this rule with the option ["done", "send.error", "send.success"]
:
/*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/
function foo(err, done) {
if (err) {
done(err);
}
done();
}
function bar(err, send) {
if (err) {
send.error(err);
}
send.success();
}
Examples of correct code for this rule with the option ["done", "send.error", "send.success"]
:
/*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/
function foo(err, done) {
if (err) {
return done(err);
}
done();
}
function bar(err, send) {
if (err) {
return send.error(err);
}
send.success();
}
Known Limitations
Because it is difficult to understand the meaning of a program through static analysis, this rule has limitations:
- false negatives when this rule reports correct code, but the program calls the callback more than one time (which is incorrect behavior)
- false positives when this rule reports incorrect code, but the program calls the callback only one time (which is correct behavior)
Passing the callback by reference
The static analysis of this rule does not detect that the program calls the callback if it is an argument of a function (for example, setTimeout
).
Example of a false negative when this rule reports correct code:
/*eslint callback-return: "error"*/
function foo(err, callback) {
if (err) {
setTimeout(callback, 0); // this is bad, but WILL NOT warn
}
callback();
}
Triggering the callback within a nested function
The static analysis of this rule does not detect that the program calls the callback from within a nested function or an immediately-invoked function expression (IIFE).
Example of a false negative when this rule reports correct code:
/*eslint callback-return: "error"*/
function foo(err, callback) {
if (err) {
process.nextTick(function() {
return callback(); // this is bad, but WILL NOT warn
});
}
callback();
}
If/else statements
The static analysis of this rule does not detect that the program calls the callback only one time in each branch of an if
statement.
Example of a false positive when this rule reports incorrect code:
/*eslint callback-return: "error"*/
function foo(err, callback) {
if (err) {
callback(err); // this is fine, but WILL warn
} else {
callback(); // this is fine, but WILL warn
}
}
When Not To Use It
There are some cases where you might want to call a callback function more than once. In those cases this rule may lead to incorrect behavior. In those cases you may want to reserve a special name for those callbacks and not include that in the list of callbacks that trigger warnings.
Further Reading
Related Rules
- [handle-callback-err](handle-callback-err.md) Source: http://eslint.org/docs/rules/
'fs' is defined but never used. Open
import fs from "fs";
- 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 'undefined' and instead saw 'void'. Open
if (this.config.env !== void 0) {
- Read upRead up
- Exclude checks
Disallow use of the void operator. (no-void)
The void
operator takes an operand and returns undefined
: void expression
will evaluate expression
and return undefined
. It can be used to ignore any side effects expression
may produce:
The common case of using void
operator is to get a "pure" undefined
value as prior to ES5 the undefined
variable was mutable:
// will always return undefined
(function(){
return void 0;
})();
// will return 1 in ES3 and undefined in ES5+
(function(){
undefined = 1;
return undefined;
})();
// will throw TypeError in ES5+
(function(){
'use strict';
undefined = 1;
})();
Another common case is to minify code as void 0
is shorter than undefined
:
foo = void 0;
foo = undefined;
When used with IIFE (immediately-invoked function expression), void
can be used to force the function keyword to be treated as an expression instead of a declaration:
var foo = 1;
void function(){ foo = 1; }() // will assign foo a value of 1
+function(){ foo = 1; }() // same as above
function(){ foo = 1; }() // will throw SyntaxError
Some code styles prohibit void
operator, marking it as non-obvious and hard to read.
Rule Details
This rule aims to eliminate use of void operator.
Examples of incorrect code for this rule:
/*eslint no-void: "error"*/
void foo
var foo = void bar();
When Not To Use It
If you intentionally use the void
operator then you can disable this rule.
Further Reading
Related Rules
- [no-undef-init](no-undef-init.md)
- [no-undefined](no-undefined.md) Source: http://eslint.org/docs/rules/