In JavaScript, prior to ES6, variable and function declarations are hoisted to the top of a scope, so it's possible to use identifiers before their formal declarations in code. This can be confusing and some believe it is best to always declare variables and functions before using them.
In ES6, block-level bindings (let and const) introduce a "temporal dead zone" where a ReferenceError will be thrown with any attempt to access the variable before its declaration.
Rule Details
This rule will warn when it encounters a reference to an identifier that has not yet been declared.
functions (boolean) -
The flag which shows whether or not this rule checks function declarations.
If this is true, this rule warns every reference to a function before the function declaration.
Otherwise, ignores those references.
Function declarations are hoisted, so it's safe.
Default is true.
classes (boolean) -
The flag which shows whether or not this rule checks class declarations of upper scopes.
If this is true, this rule warns every reference to a class before the class declaration.
Otherwise, ignores those references if the declaration is in upper function scopes.
Class declarations are not hoisted, so it might be danger.
Default is true.
variables (boolean) -
This flag determines whether or not the rule checks variable declarations in upper scopes.
If this is true, the rule warns every reference to a variable before the variable declaration.
Otherwise, the rule ignores a reference if the declaration is in an upper scope, while still reporting the reference if it's in the same scope as the declaration.
Default is true.
This rule accepts "nofunc" string as an option.
"nofunc" is the same as { "functions": false, "classes": true }.
functions
Examples of correct code for the { "functions": false } option:
In JavaScript, prior to ES6, variable and function declarations are hoisted to the top of a scope, so it's possible to use identifiers before their formal declarations in code. This can be confusing and some believe it is best to always declare variables and functions before using them.
In ES6, block-level bindings (let and const) introduce a "temporal dead zone" where a ReferenceError will be thrown with any attempt to access the variable before its declaration.
Rule Details
This rule will warn when it encounters a reference to an identifier that has not yet been declared.
functions (boolean) -
The flag which shows whether or not this rule checks function declarations.
If this is true, this rule warns every reference to a function before the function declaration.
Otherwise, ignores those references.
Function declarations are hoisted, so it's safe.
Default is true.
classes (boolean) -
The flag which shows whether or not this rule checks class declarations of upper scopes.
If this is true, this rule warns every reference to a class before the class declaration.
Otherwise, ignores those references if the declaration is in upper function scopes.
Class declarations are not hoisted, so it might be danger.
Default is true.
variables (boolean) -
This flag determines whether or not the rule checks variable declarations in upper scopes.
If this is true, the rule warns every reference to a variable before the variable declaration.
Otherwise, the rule ignores a reference if the declaration is in an upper scope, while still reporting the reference if it's in the same scope as the declaration.
Default is true.
This rule accepts "nofunc" string as an option.
"nofunc" is the same as { "functions": false, "classes": true }.
functions
Examples of correct code for the { "functions": false } option:
In JavaScript, prior to ES6, variable and function declarations are hoisted to the top of a scope, so it's possible to use identifiers before their formal declarations in code. This can be confusing and some believe it is best to always declare variables and functions before using them.
In ES6, block-level bindings (let and const) introduce a "temporal dead zone" where a ReferenceError will be thrown with any attempt to access the variable before its declaration.
Rule Details
This rule will warn when it encounters a reference to an identifier that has not yet been declared.
functions (boolean) -
The flag which shows whether or not this rule checks function declarations.
If this is true, this rule warns every reference to a function before the function declaration.
Otherwise, ignores those references.
Function declarations are hoisted, so it's safe.
Default is true.
classes (boolean) -
The flag which shows whether or not this rule checks class declarations of upper scopes.
If this is true, this rule warns every reference to a class before the class declaration.
Otherwise, ignores those references if the declaration is in upper function scopes.
Class declarations are not hoisted, so it might be danger.
Default is true.
variables (boolean) -
This flag determines whether or not the rule checks variable declarations in upper scopes.
If this is true, the rule warns every reference to a variable before the variable declaration.
Otherwise, the rule ignores a reference if the declaration is in an upper scope, while still reporting the reference if it's in the same scope as the declaration.
Default is true.
This rule accepts "nofunc" string as an option.
"nofunc" is the same as { "functions": false, "classes": true }.
functions
Examples of correct code for the { "functions": false } option:
ECMAScript 6 allows programmers to create variables with block scope instead of function scope using the let
and const keywords. Block scope is common in many other programming languages and helps programmers avoid mistakes
such as:
var count = people.length;
var enoughFood = count > sandwiches.length;
if(enoughFood){
var count = sandwiches.length;// accidentally overriding the count variable
console.log("We have "+ count +" sandwiches for everyone. Plenty for all!");
}
// our count variable is no longer accurate
console.log("We have "+ count +" people and "+ sandwiches.length +" sandwiches!");
Rule Details
This rule is aimed at discouraging the use of var and encouraging the use of const or let instead.
Examples
Examples of incorrect code for this rule:
/*eslint no-var: "error"*/
var x ="y";
varCONFIG={};
Examples of correct code for this rule:
/*eslint no-var: "error"*/
/*eslint-env es6*/
let x ="y";
constCONFIG={};
When Not To Use It
In addition to non-ES6 environments, existing JavaScript projects that are beginning to introduce ES6 into their
codebase may not want to apply this rule if the cost of migrating from var to let is too costly.
Source: http://eslint.org/docs/rules/
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){
return5;
})();
// Unused recursive functions also cause warnings.
functionfact(n){
if(n <2)return1;
return n *fact(n -1);
}
// When a function definition destructures an array, unused entries from the array also cause warnings.
functiongetY([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(functionfoo(){
// ...
}.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.
functiongetY([, 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 or commonjs
when parserOptions.sourceType is module
when ecmaFeatures.globalReturn is true
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.
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:
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:
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:
// '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:
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:
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/
enforce consistent indentation (indent)
There are several common guidelines which require specific indentation of nested blocks and statements, like:
functionhello(indentSize, type){
if(indentSize ===4&& type !=='tab'){
console.log('Each next indentation will increase on 4 spaces');
}
}
These are the most common scenarios recommended in different style guides:
Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix
Tabs: jQuery
Four spaces: Crockford
Rule Details
This rule enforces a consistent indentation style. The default style is 4 spaces.
Options
This rule has a mixed option:
For example, for 2-space indentation:
{
"indent": ["error", 2]
}
Or for tabbed indentation:
{
"indent": ["error", "tab"]
}
Examples of incorrect code for this rule with the default options:
/*eslint indent: "error"*/
if(a){
b=c;
functionfoo(d){
e=f;
}
}
Examples of correct code for this rule with the default options:
/*eslint indent: "error"*/
if(a){
b=c;
functionfoo(d){
e=f;
}
}
This rule has an object option:
"SwitchCase" (default: 0) enforces indentation level for case clauses in switch statements
"VariableDeclarator" (default: 1) enforces indentation level for var declarators; can also take an object to define separate rules for var, let and const declarations.
"outerIIFEBody" (default: 1) enforces indentation level for file-level IIFEs.
"MemberExpression" (off by default) enforces indentation level for multi-line property chains (except in variable declarations and assignments)
"FunctionDeclaration" takes an object to define rules for function declarations.
parameters (off by default) enforces indentation level for parameters in a function declaration. This can either be a number indicating indentation level, or the string "first" indicating that all parameters of the declaration must be aligned with the first parameter.
body (default: 1) enforces indentation level for the body of a function declaration.
"FunctionExpression" takes an object to define rules for function expressions.
parameters (off by default) enforces indentation level for parameters in a function expression. This can either be a number indicating indentation level, or the string "first" indicating that all parameters of the expression must be aligned with the first parameter.
body (default: 1) enforces indentation level for the body of a function expression.
"CallExpression" takes an object to define rules for function call expressions.
arguments (off by default) enforces indentation level for arguments in a call expression. This can either be a number indicating indentation level, or the string "first" indicating that all arguments of the expression must be aligned with the first argument.
"ArrayExpression" (default: 1) enforces indentation level for elements in arrays. It can also be set to the string "first", indicating that all the elements in the array should be aligned with the first element.
"ObjectExpression" (default: 1) enforces indentation level for properties in objects. It can be set to the string "first", indicating that all properties in the object should be aligned with the first property.
Level of indentation denotes the multiple of the indent specified. Example:
Indent of 4 spaces with VariableDeclarator set to 2 will indent the multi-line variable declarations with 8 spaces.
Indent of 2 spaces with VariableDeclarator set to 2 will indent the multi-line variable declarations with 4 spaces.
Indent of 2 spaces with VariableDeclarator set to {"var": 2, "let": 2, "const": 3} will indent the multi-line variable declarations with 4 spaces for var and let, 6 spaces for const statements.
Indent of tab with VariableDeclarator set to 2 will indent the multi-line variable declarations with 2 tabs.
Indent of 2 spaces with SwitchCase set to 0 will not indent case clauses with respect to switch statements.
Indent of 2 spaces with SwitchCase set to 1 will indent case clauses with 2 spaces with respect to switch statements.
Indent of 2 spaces with SwitchCase set to 2 will indent case clauses with 4 spaces with respect to switch statements.
Indent of tab with SwitchCase set to 2 will indent case clauses with 2 tabs with respect to switch statements.
Indent of 2 spaces with MemberExpression set to 0 will indent the multi-line property chains with 0 spaces.
Indent of 2 spaces with MemberExpression set to 1 will indent the multi-line property chains with 2 spaces.
Indent of 2 spaces with MemberExpression set to 2 will indent the multi-line property chains with 4 spaces.
Indent of 4 spaces with MemberExpression set to 0 will indent the multi-line property chains with 0 spaces.
Indent of 4 spaces with MemberExpression set to 1 will indent the multi-line property chains with 4 spaces.
Indent of 4 spaces with MemberExpression set to 2 will indent the multi-line property chains with 8 spaces.
tab
Examples of incorrect code for this rule with the "tab" option:
/*eslint indent: ["error", "tab"]*/
if(a){
b=c;
functionfoo(d){
e=f;
}
}
Examples of correct code for this rule with the "tab" option:
/*eslint indent: ["error", "tab"]*/
if(a){
/*tab*/b=c;
/*tab*/functionfoo(d){
/*tab*//*tab*/e=f;
/*tab*/}
}
SwitchCase
Examples of incorrect code for this rule with the 2, { "SwitchCase": 1 } options:
This rule can help you locate potential ReferenceErrors resulting from misspellings of variable and parameter names, or accidental implicit globals (for example, from forgetting the var keyword in a for loop initializer).
Rule Details
Any reference to an undeclared variable causes a warning, unless the variable is explicitly mentioned in a /*global ...*/ comment.
Examples of incorrect code for this rule:
/*eslint no-undef: "error"*/
var a =someFunction();
b =10;
Examples of correct code for this rule with global declaration:
/*global someFunction b:true*/
/*eslint no-undef: "error"*/
var a =someFunction();
b =10;
The b:true syntax in /*global */ indicates that assignment to b is correct.
Examples of incorrect code for this rule with global declaration:
/*global b*/
/*eslint no-undef: "error"*/
b =10;
By default, variables declared in /*global */ are read-only, therefore assignment is incorrect.
Options
typeof set to true will warn for variables used inside typeof check (Default false).
typeof
Examples of correct code for the default { "typeof": false } option:
/*eslint no-undef: "error"*/
if(typeof UndefinedIdentifier ==="undefined"){
// do something ...
}
You can use this option if you want to prevent typeof check on a variable which has not been declared.
Examples of incorrect code for the { "typeof": true } option:
For convenience, ESLint provides shortcuts that pre-define global variables exposed by popular libraries and runtime environments. This rule supports these environments, as listed in Specifying Environments. A few examples are given below.
browser
Examples of correct code for this rule with browser environment:
/*eslint no-undef: "error"*/
/*eslint-env browser*/
setTimeout(function(){
alert("Hello");
});
node
Examples of correct code for this rule with node environment:
/*eslint no-undef: "error"*/
/*eslint-env node*/
var fs =require("fs");
module.exports=function(){
console.log(fs);
};
When Not To Use It
If explicit declaration of global variables is not to your taste.
JavaScript allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to never omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity. So the following:
if(foo) foo++;
Can be rewritten as:
if(foo){
foo++;
}
There are, however, some who prefer to only use braces when there is more than one statement to be executed.
Rule Details
This rule is aimed at preventing bugs and increasing code clarity by ensuring that block statements are wrapped in curly braces. It will warn when it encounters blocks that omit curly braces.
Options
all
Examples of incorrect code for the default "all" option:
/*eslint curly: "error"*/
if(foo) foo++;
while(bar)
baz();
if(foo){
baz();
}elsequx();
Examples of correct code for the default "all" option:
/*eslint curly: "error"*/
if(foo){
foo++;
}
while(bar){
baz();
}
if(foo){
baz();
}else{
qux();
}
multi
By default, this rule warns whenever if, else, for, while, or do are used without block statements as their body. However, you can specify that block statements should be used only when there are multiple statements in the block and warn when there is only one statement in the block.
Examples of incorrect code for the "multi" option:
/*eslint curly: ["error", "multi"]*/
if(foo){
foo++;
}
if(foo)bar();
else{
foo++;
}
while(true){
doSomething();
}
for(var i=0; i < items.length; i++){
doSomething();
}
Examples of correct code for the "multi" option:
/*eslint curly: ["error", "multi"]*/
if(foo) foo++;
elsefoo();
while(true){
doSomething();
doSomethingElse();
}
multi-line
Alternatively, you can relax the rule to allow brace-less single-line if, else if, else, for, while, or do, while still enforcing the use of curly braces for other instances.
Examples of incorrect code for the "multi-line" option:
/*eslint curly: ["error", "multi-line"]*/
if(foo)
doSomething();
else
doSomethingElse();
if(foo)foo(
bar,
baz);
Examples of correct code for the "multi-line" option:
/*eslint curly: ["error", "multi-line"]*/
if(foo) foo++;elsedoSomething();
if(foo) foo++;
elseif(bar)baz()
elsedoSomething();
dosomething();
while(foo);
while(foo
&& bar)baz();
if(foo){
foo++;
}
if(foo){ foo++;}
while(true){
doSomething();
doSomethingElse();
}
multi-or-nest
You can use another configuration that forces brace-less if, else if, else, for, while, or do if their body contains only one single-line statement. And forces braces in all other cases.
Examples of incorrect code for the "multi-or-nest" option:
/*eslint curly: ["error", "multi-or-nest"]*/
if(!foo)
foo ={
bar: baz,
qux: foo
};
while(true)
if(foo)
doSomething();
else
doSomethingElse();
if(foo){
foo++;
}
while(true){
doSomething();
}
for(var i =0; foo; i++){
doSomething();
}
if(foo)
// some comment
bar();
Examples of correct code for the "multi-or-nest" option:
/*eslint curly: ["error", "multi-or-nest"]*/
if(!foo){
foo ={
bar: baz,
qux: foo
};
}
while(true){
if(foo)
doSomething();
else
doSomethingElse();
}
if(foo)
foo++;
while(true)
doSomething();
for(var i =0; foo; i++)
doSomething();
if(foo){
// some comment
bar();
}
consistent
When using any of the multi* options, you can add an option to enforce all bodies of a if,
else if and else chain to be with or without braces.
Examples of incorrect code for the "multi", "consistent" options:
If you have no strict conventions about when to use block statements and when not to, you can safely disable this rule.
Source: http://eslint.org/docs/rules/
Disallow Early Use (no-use-before-define)
In JavaScript, prior to ES6, variable and function declarations are hoisted to the top of a scope, so it's possible to use identifiers before their formal declarations in code. This can be confusing and some believe it is best to always declare variables and functions before using them.
In ES6, block-level bindings (let and const) introduce a "temporal dead zone" where a ReferenceError will be thrown with any attempt to access the variable before its declaration.
Rule Details
This rule will warn when it encounters a reference to an identifier that has not yet been declared.
functions (boolean) -
The flag which shows whether or not this rule checks function declarations.
If this is true, this rule warns every reference to a function before the function declaration.
Otherwise, ignores those references.
Function declarations are hoisted, so it's safe.
Default is true.
classes (boolean) -
The flag which shows whether or not this rule checks class declarations of upper scopes.
If this is true, this rule warns every reference to a class before the class declaration.
Otherwise, ignores those references if the declaration is in upper function scopes.
Class declarations are not hoisted, so it might be danger.
Default is true.
variables (boolean) -
This flag determines whether or not the rule checks variable declarations in upper scopes.
If this is true, the rule warns every reference to a variable before the variable declaration.
Otherwise, the rule ignores a reference if the declaration is in an upper scope, while still reporting the reference if it's in the same scope as the declaration.
Default is true.
This rule accepts "nofunc" string as an option.
"nofunc" is the same as { "functions": false, "classes": true }.
functions
Examples of correct code for the { "functions": false } option:
JavaScript allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to never omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity. So the following:
if(foo) foo++;
Can be rewritten as:
if(foo){
foo++;
}
There are, however, some who prefer to only use braces when there is more than one statement to be executed.
Rule Details
This rule is aimed at preventing bugs and increasing code clarity by ensuring that block statements are wrapped in curly braces. It will warn when it encounters blocks that omit curly braces.
Options
all
Examples of incorrect code for the default "all" option:
/*eslint curly: "error"*/
if(foo) foo++;
while(bar)
baz();
if(foo){
baz();
}elsequx();
Examples of correct code for the default "all" option:
/*eslint curly: "error"*/
if(foo){
foo++;
}
while(bar){
baz();
}
if(foo){
baz();
}else{
qux();
}
multi
By default, this rule warns whenever if, else, for, while, or do are used without block statements as their body. However, you can specify that block statements should be used only when there are multiple statements in the block and warn when there is only one statement in the block.
Examples of incorrect code for the "multi" option:
/*eslint curly: ["error", "multi"]*/
if(foo){
foo++;
}
if(foo)bar();
else{
foo++;
}
while(true){
doSomething();
}
for(var i=0; i < items.length; i++){
doSomething();
}
Examples of correct code for the "multi" option:
/*eslint curly: ["error", "multi"]*/
if(foo) foo++;
elsefoo();
while(true){
doSomething();
doSomethingElse();
}
multi-line
Alternatively, you can relax the rule to allow brace-less single-line if, else if, else, for, while, or do, while still enforcing the use of curly braces for other instances.
Examples of incorrect code for the "multi-line" option:
/*eslint curly: ["error", "multi-line"]*/
if(foo)
doSomething();
else
doSomethingElse();
if(foo)foo(
bar,
baz);
Examples of correct code for the "multi-line" option:
/*eslint curly: ["error", "multi-line"]*/
if(foo) foo++;elsedoSomething();
if(foo) foo++;
elseif(bar)baz()
elsedoSomething();
dosomething();
while(foo);
while(foo
&& bar)baz();
if(foo){
foo++;
}
if(foo){ foo++;}
while(true){
doSomething();
doSomethingElse();
}
multi-or-nest
You can use another configuration that forces brace-less if, else if, else, for, while, or do if their body contains only one single-line statement. And forces braces in all other cases.
Examples of incorrect code for the "multi-or-nest" option:
/*eslint curly: ["error", "multi-or-nest"]*/
if(!foo)
foo ={
bar: baz,
qux: foo
};
while(true)
if(foo)
doSomething();
else
doSomethingElse();
if(foo){
foo++;
}
while(true){
doSomething();
}
for(var i =0; foo; i++){
doSomething();
}
if(foo)
// some comment
bar();
Examples of correct code for the "multi-or-nest" option:
/*eslint curly: ["error", "multi-or-nest"]*/
if(!foo){
foo ={
bar: baz,
qux: foo
};
}
while(true){
if(foo)
doSomething();
else
doSomethingElse();
}
if(foo)
foo++;
while(true)
doSomething();
for(var i =0; foo; i++)
doSomething();
if(foo){
// some comment
bar();
}
consistent
When using any of the multi* options, you can add an option to enforce all bodies of a if,
else if and else chain to be with or without braces.
Examples of incorrect code for the "multi", "consistent" options:
If you have no strict conventions about when to use block statements and when not to, you can safely disable this rule.
Source: http://eslint.org/docs/rules/
require spacing around infix operators (space-infix-ops)
While formatting preferences are very personal, a number of style guides require spaces around operators, such as:
var sum =1+2;
The proponents of these extra spaces believe it make the code easier to read and can more easily highlight potential errors, such as:
var sum = i+++2;
While this is valid JavaScript syntax, it is hard to determine what the author intended.
Rule Details
This rule is aimed at ensuring there are spaces around infix operators.
Options
This rule accepts a single options argument with the following defaults:
There are several common guidelines which require specific indentation of nested blocks and statements, like:
functionhello(indentSize, type){
if(indentSize ===4&& type !=='tab'){
console.log('Each next indentation will increase on 4 spaces');
}
}
These are the most common scenarios recommended in different style guides:
Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix
Tabs: jQuery
Four spaces: Crockford
Rule Details
This rule enforces a consistent indentation style. The default style is 4 spaces.
Options
This rule has a mixed option:
For example, for 2-space indentation:
{
"indent": ["error", 2]
}
Or for tabbed indentation:
{
"indent": ["error", "tab"]
}
Examples of incorrect code for this rule with the default options:
/*eslint indent: "error"*/
if(a){
b=c;
functionfoo(d){
e=f;
}
}
Examples of correct code for this rule with the default options:
/*eslint indent: "error"*/
if(a){
b=c;
functionfoo(d){
e=f;
}
}
This rule has an object option:
"SwitchCase" (default: 0) enforces indentation level for case clauses in switch statements
"VariableDeclarator" (default: 1) enforces indentation level for var declarators; can also take an object to define separate rules for var, let and const declarations.
"outerIIFEBody" (default: 1) enforces indentation level for file-level IIFEs.
"MemberExpression" (off by default) enforces indentation level for multi-line property chains (except in variable declarations and assignments)
"FunctionDeclaration" takes an object to define rules for function declarations.
parameters (off by default) enforces indentation level for parameters in a function declaration. This can either be a number indicating indentation level, or the string "first" indicating that all parameters of the declaration must be aligned with the first parameter.
body (default: 1) enforces indentation level for the body of a function declaration.
"FunctionExpression" takes an object to define rules for function expressions.
parameters (off by default) enforces indentation level for parameters in a function expression. This can either be a number indicating indentation level, or the string "first" indicating that all parameters of the expression must be aligned with the first parameter.
body (default: 1) enforces indentation level for the body of a function expression.
"CallExpression" takes an object to define rules for function call expressions.
arguments (off by default) enforces indentation level for arguments in a call expression. This can either be a number indicating indentation level, or the string "first" indicating that all arguments of the expression must be aligned with the first argument.
"ArrayExpression" (default: 1) enforces indentation level for elements in arrays. It can also be set to the string "first", indicating that all the elements in the array should be aligned with the first element.
"ObjectExpression" (default: 1) enforces indentation level for properties in objects. It can be set to the string "first", indicating that all properties in the object should be aligned with the first property.
Level of indentation denotes the multiple of the indent specified. Example:
Indent of 4 spaces with VariableDeclarator set to 2 will indent the multi-line variable declarations with 8 spaces.
Indent of 2 spaces with VariableDeclarator set to 2 will indent the multi-line variable declarations with 4 spaces.
Indent of 2 spaces with VariableDeclarator set to {"var": 2, "let": 2, "const": 3} will indent the multi-line variable declarations with 4 spaces for var and let, 6 spaces for const statements.
Indent of tab with VariableDeclarator set to 2 will indent the multi-line variable declarations with 2 tabs.
Indent of 2 spaces with SwitchCase set to 0 will not indent case clauses with respect to switch statements.
Indent of 2 spaces with SwitchCase set to 1 will indent case clauses with 2 spaces with respect to switch statements.
Indent of 2 spaces with SwitchCase set to 2 will indent case clauses with 4 spaces with respect to switch statements.
Indent of tab with SwitchCase set to 2 will indent case clauses with 2 tabs with respect to switch statements.
Indent of 2 spaces with MemberExpression set to 0 will indent the multi-line property chains with 0 spaces.
Indent of 2 spaces with MemberExpression set to 1 will indent the multi-line property chains with 2 spaces.
Indent of 2 spaces with MemberExpression set to 2 will indent the multi-line property chains with 4 spaces.
Indent of 4 spaces with MemberExpression set to 0 will indent the multi-line property chains with 0 spaces.
Indent of 4 spaces with MemberExpression set to 1 will indent the multi-line property chains with 4 spaces.
Indent of 4 spaces with MemberExpression set to 2 will indent the multi-line property chains with 8 spaces.
tab
Examples of incorrect code for this rule with the "tab" option:
/*eslint indent: ["error", "tab"]*/
if(a){
b=c;
functionfoo(d){
e=f;
}
}
Examples of correct code for this rule with the "tab" option:
/*eslint indent: ["error", "tab"]*/
if(a){
/*tab*/b=c;
/*tab*/functionfoo(d){
/*tab*//*tab*/e=f;
/*tab*/}
}
SwitchCase
Examples of incorrect code for this rule with the 2, { "SwitchCase": 1 } options:
There are several common guidelines which require specific indentation of nested blocks and statements, like:
functionhello(indentSize, type){
if(indentSize ===4&& type !=='tab'){
console.log('Each next indentation will increase on 4 spaces');
}
}
These are the most common scenarios recommended in different style guides:
Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix
Tabs: jQuery
Four spaces: Crockford
Rule Details
This rule enforces a consistent indentation style. The default style is 4 spaces.
Options
This rule has a mixed option:
For example, for 2-space indentation:
{
"indent": ["error", 2]
}
Or for tabbed indentation:
{
"indent": ["error", "tab"]
}
Examples of incorrect code for this rule with the default options:
/*eslint indent: "error"*/
if(a){
b=c;
functionfoo(d){
e=f;
}
}
Examples of correct code for this rule with the default options:
/*eslint indent: "error"*/
if(a){
b=c;
functionfoo(d){
e=f;
}
}
This rule has an object option:
"SwitchCase" (default: 0) enforces indentation level for case clauses in switch statements
"VariableDeclarator" (default: 1) enforces indentation level for var declarators; can also take an object to define separate rules for var, let and const declarations.
"outerIIFEBody" (default: 1) enforces indentation level for file-level IIFEs.
"MemberExpression" (off by default) enforces indentation level for multi-line property chains (except in variable declarations and assignments)
"FunctionDeclaration" takes an object to define rules for function declarations.
parameters (off by default) enforces indentation level for parameters in a function declaration. This can either be a number indicating indentation level, or the string "first" indicating that all parameters of the declaration must be aligned with the first parameter.
body (default: 1) enforces indentation level for the body of a function declaration.
"FunctionExpression" takes an object to define rules for function expressions.
parameters (off by default) enforces indentation level for parameters in a function expression. This can either be a number indicating indentation level, or the string "first" indicating that all parameters of the expression must be aligned with the first parameter.
body (default: 1) enforces indentation level for the body of a function expression.
"CallExpression" takes an object to define rules for function call expressions.
arguments (off by default) enforces indentation level for arguments in a call expression. This can either be a number indicating indentation level, or the string "first" indicating that all arguments of the expression must be aligned with the first argument.
"ArrayExpression" (default: 1) enforces indentation level for elements in arrays. It can also be set to the string "first", indicating that all the elements in the array should be aligned with the first element.
"ObjectExpression" (default: 1) enforces indentation level for properties in objects. It can be set to the string "first", indicating that all properties in the object should be aligned with the first property.
Level of indentation denotes the multiple of the indent specified. Example:
Indent of 4 spaces with VariableDeclarator set to 2 will indent the multi-line variable declarations with 8 spaces.
Indent of 2 spaces with VariableDeclarator set to 2 will indent the multi-line variable declarations with 4 spaces.
Indent of 2 spaces with VariableDeclarator set to {"var": 2, "let": 2, "const": 3} will indent the multi-line variable declarations with 4 spaces for var and let, 6 spaces for const statements.
Indent of tab with VariableDeclarator set to 2 will indent the multi-line variable declarations with 2 tabs.
Indent of 2 spaces with SwitchCase set to 0 will not indent case clauses with respect to switch statements.
Indent of 2 spaces with SwitchCase set to 1 will indent case clauses with 2 spaces with respect to switch statements.
Indent of 2 spaces with SwitchCase set to 2 will indent case clauses with 4 spaces with respect to switch statements.
Indent of tab with SwitchCase set to 2 will indent case clauses with 2 tabs with respect to switch statements.
Indent of 2 spaces with MemberExpression set to 0 will indent the multi-line property chains with 0 spaces.
Indent of 2 spaces with MemberExpression set to 1 will indent the multi-line property chains with 2 spaces.
Indent of 2 spaces with MemberExpression set to 2 will indent the multi-line property chains with 4 spaces.
Indent of 4 spaces with MemberExpression set to 0 will indent the multi-line property chains with 0 spaces.
Indent of 4 spaces with MemberExpression set to 1 will indent the multi-line property chains with 4 spaces.
Indent of 4 spaces with MemberExpression set to 2 will indent the multi-line property chains with 8 spaces.
tab
Examples of incorrect code for this rule with the "tab" option:
/*eslint indent: ["error", "tab"]*/
if(a){
b=c;
functionfoo(d){
e=f;
}
}
Examples of correct code for this rule with the "tab" option:
/*eslint indent: ["error", "tab"]*/
if(a){
/*tab*/b=c;
/*tab*/functionfoo(d){
/*tab*//*tab*/e=f;
/*tab*/}
}
SwitchCase
Examples of incorrect code for this rule with the 2, { "SwitchCase": 1 } options:
require or disallow padding within blocks (padded-blocks)
Some style guides require block statements to start and end with blank lines. The goal is
to improve readability by visually separating the block content and the surrounding code.
if(a){
b();
}
Since it's good to have a consistent code style, you should either always write
padded blocks or never do it.
Rule Details
This rule enforces consistent empty line padding within blocks.
Options
This rule has one option, which can be a string option or an object option.
String option:
"always" (default) requires empty lines at the beginning and ending of block statements (except switch statements and classes)
"never" disallows empty lines at the beginning and ending of block statements (except switch statements and classes)
Object option:
"blocks" require or disallow padding within block statements
"classes" require or disallow padding within classes
"switches" require or disallow padding within switch statements
always
Examples of incorrect code for this rule with the default "always" option:
/*eslint padded-blocks: ["error", "always"]*/
if(a){
b();
}
if(a){b();}
if(a)
{
b();
}
if(a){
b();
}
if(a){
b();
}
if(a){
// comment
b();
}
Examples of correct code for this rule with the default "always" option:
/*eslint padded-blocks: ["error", "always"]*/
if(a){
b();
}
if(a)
{
b();
}
if(a){
// comment
b();
}
never
Examples of incorrect code for this rule with the "never" option:
/*eslint padded-blocks: ["error", "never"]*/
if(a){
b();
}
if(a)
{
b();
}
if(a){
b();
}
if(a){
b();
}
Examples of correct code for this rule with the "never" option:
/*eslint padded-blocks: ["error", "never"]*/
if(a){
b();
}
if(a)
{
b();
}
blocks
Examples of incorrect code for this rule with the { "blocks": "always" } option:
You can turn this rule off if you are not concerned with the consistency of padding within blocks.
Source: http://eslint.org/docs/rules/
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){
return5;
})();
// Unused recursive functions also cause warnings.
functionfact(n){
if(n <2)return1;
return n *fact(n -1);
}
// When a function definition destructures an array, unused entries from the array also cause warnings.
functiongetY([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(functionfoo(){
// ...
}.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.
functiongetY([, 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 or commonjs
when parserOptions.sourceType is module
when ecmaFeatures.globalReturn is true
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.
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:
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:
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:
// '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:
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:
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/
disallow dangling underscores in identifiers (no-underscore-dangle)
As far as naming conventions for identifiers go, dangling underscores may be the most polarizing in JavaScript. Dangling underscores are underscores at either the beginning or end of an identifier, such as:
var _foo;
There is actually a long history of using dangling underscores to indicate "private" members of objects in JavaScript (though JavaScript doesn't have truly private members, this convention served as a warning). This began with SpiderMonkey adding nonstandard methods such as __defineGetter__(). The intent with the underscores was to make it obvious that this method was special in some way. Since that time, using a single underscore prefix has become popular as a way to indicate "private" members of objects.
Whether or not you choose to allow dangling underscores in identifiers is purely a convention and has no effect on performance, readability, or complexity. It's purely a preference.
Rule Details
This rule disallows dangling underscores in identifiers.
Examples of incorrect code for this rule:
/*eslint no-underscore-dangle: "error"*/
var foo_;
var __proto__ ={};
foo._bar();
Examples of correct code for this rule:
/*eslint no-underscore-dangle: "error"*/
var _ =require('underscore');
var obj = _.contains(items, item);
obj.__proto__ ={};
var file = __filename;
Options
This rule has an object option:
"allow" allows specified identifiers to have dangling underscores
"allowAfterThis": false (default) disallows dangling underscores in members of the this object
"allowAfterSuper": false (default) disallows dangling underscores in members of the super object
allow
Examples of additional correct code for this rule with the { "allow": ["foo_", "_bar"] } option:
EcmaScript 6 provides a concise form for defining object literal methods and properties. This
syntax can make defining complex object literals much cleaner.
Here are a few common examples using the ES5 syntax:
// properties
var foo ={
x: x,
y: y,
z: z,
};
// methods
var foo ={
a:function(){},
b:function(){}
};
Now here are ES6 equivalents:
/*eslint-env es6*/
// properties
var foo ={x, y, z};
// methods
var foo ={
a(){},
b(){}
};
Rule Details
This rule enforces the use of the shorthand syntax. This applies
to all methods (including generators) defined in object literals and any
properties defined where the key name matches name of the assigned variable.
Each of the following properties would warn:
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo ={
w:function(){},
x:function*(){},
[y]:function(){},
z: z
};
In that case the expected syntax would have been:
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo ={
w(){},
*x(){},
[y](){},
z
};
This rule does not flag arrow functions inside of object literals.
The following will not warn:
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo ={
x:(y)=> y
};
Options
The rule takes an option which specifies when it should be applied. It can be set to one of the following values:
"always" (default) expects that the shorthand will be used whenever possible.
"methods" ensures the method shorthand is used (also applies to generators).
"properties" ensures the property shorthand is used (where the key and variable name match).
"never" ensures that no property or method shorthand is used in any object literal.
"consistent" ensures that either all shorthand or all longform will be used in an object literal.
"consistent-as-needed" ensures that either all shorthand or all longform will be used in an object literal, but ensures all shorthand whenever possible.
You can set the option in configuration like this:
{
"object-shorthand": ["error", "always"]
}
Additionally, the rule takes an optional object configuration:
"avoidQuotes": true indicates that longform syntax is preferred whenever the object key is a string literal (default: false). Note that this option can only be enabled when the string option is set to "always", "methods", or "properties".
"ignoreConstructors": true can be used to prevent the rule from reporting errors for constructor functions. (By default, the rule treats constructors the same way as other functions.) Note that this option can only be enabled when the string option is set to "always" or "methods".
"avoidExplicitReturnArrows": true indicates that methods are preferred over explicit-return arrow functions for function properties. (By default, the rule allows either of these.) Note that this option can only be enabled when the string option is set to "always" or "methods".
Anyone not yet in an ES6 environment would not want to apply this rule. Others may find the terseness of the shorthand
syntax harder to read and may not want to encourage it with this rule.
ECMAScript 6 allows programmers to create variables with block scope instead of function scope using the let
and const keywords. Block scope is common in many other programming languages and helps programmers avoid mistakes
such as:
var count = people.length;
var enoughFood = count > sandwiches.length;
if(enoughFood){
var count = sandwiches.length;// accidentally overriding the count variable
console.log("We have "+ count +" sandwiches for everyone. Plenty for all!");
}
// our count variable is no longer accurate
console.log("We have "+ count +" people and "+ sandwiches.length +" sandwiches!");
Rule Details
This rule is aimed at discouraging the use of var and encouraging the use of const or let instead.
Examples
Examples of incorrect code for this rule:
/*eslint no-var: "error"*/
var x ="y";
varCONFIG={};
Examples of correct code for this rule:
/*eslint no-var: "error"*/
/*eslint-env es6*/
let x ="y";
constCONFIG={};
When Not To Use It
In addition to non-ES6 environments, existing JavaScript projects that are beginning to introduce ES6 into their
codebase may not want to apply this rule if the cost of migrating from var to let is too costly.
Source: http://eslint.org/docs/rules/
Require Following Curly Brace Conventions (curly)
JavaScript allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to never omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity. So the following:
if(foo) foo++;
Can be rewritten as:
if(foo){
foo++;
}
There are, however, some who prefer to only use braces when there is more than one statement to be executed.
Rule Details
This rule is aimed at preventing bugs and increasing code clarity by ensuring that block statements are wrapped in curly braces. It will warn when it encounters blocks that omit curly braces.
Options
all
Examples of incorrect code for the default "all" option:
/*eslint curly: "error"*/
if(foo) foo++;
while(bar)
baz();
if(foo){
baz();
}elsequx();
Examples of correct code for the default "all" option:
/*eslint curly: "error"*/
if(foo){
foo++;
}
while(bar){
baz();
}
if(foo){
baz();
}else{
qux();
}
multi
By default, this rule warns whenever if, else, for, while, or do are used without block statements as their body. However, you can specify that block statements should be used only when there are multiple statements in the block and warn when there is only one statement in the block.
Examples of incorrect code for the "multi" option:
/*eslint curly: ["error", "multi"]*/
if(foo){
foo++;
}
if(foo)bar();
else{
foo++;
}
while(true){
doSomething();
}
for(var i=0; i < items.length; i++){
doSomething();
}
Examples of correct code for the "multi" option:
/*eslint curly: ["error", "multi"]*/
if(foo) foo++;
elsefoo();
while(true){
doSomething();
doSomethingElse();
}
multi-line
Alternatively, you can relax the rule to allow brace-less single-line if, else if, else, for, while, or do, while still enforcing the use of curly braces for other instances.
Examples of incorrect code for the "multi-line" option:
/*eslint curly: ["error", "multi-line"]*/
if(foo)
doSomething();
else
doSomethingElse();
if(foo)foo(
bar,
baz);
Examples of correct code for the "multi-line" option:
/*eslint curly: ["error", "multi-line"]*/
if(foo) foo++;elsedoSomething();
if(foo) foo++;
elseif(bar)baz()
elsedoSomething();
dosomething();
while(foo);
while(foo
&& bar)baz();
if(foo){
foo++;
}
if(foo){ foo++;}
while(true){
doSomething();
doSomethingElse();
}
multi-or-nest
You can use another configuration that forces brace-less if, else if, else, for, while, or do if their body contains only one single-line statement. And forces braces in all other cases.
Examples of incorrect code for the "multi-or-nest" option:
/*eslint curly: ["error", "multi-or-nest"]*/
if(!foo)
foo ={
bar: baz,
qux: foo
};
while(true)
if(foo)
doSomething();
else
doSomethingElse();
if(foo){
foo++;
}
while(true){
doSomething();
}
for(var i =0; foo; i++){
doSomething();
}
if(foo)
// some comment
bar();
Examples of correct code for the "multi-or-nest" option:
/*eslint curly: ["error", "multi-or-nest"]*/
if(!foo){
foo ={
bar: baz,
qux: foo
};
}
while(true){
if(foo)
doSomething();
else
doSomethingElse();
}
if(foo)
foo++;
while(true)
doSomething();
for(var i =0; foo; i++)
doSomething();
if(foo){
// some comment
bar();
}
consistent
When using any of the multi* options, you can add an option to enforce all bodies of a if,
else if and else chain to be with or without braces.
Examples of incorrect code for the "multi", "consistent" options:
If you have no strict conventions about when to use block statements and when not to, you can safely disable this rule.
Source: http://eslint.org/docs/rules/