for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array.
Require or disallow named function expressions (func-names)
A pattern that's becoming more common is to give function expressions names to aid in debugging. For example:
Foo.prototype.bar=functionbar(){};
Adding the second bar in the above example is optional. If you leave off the function name then when the function throws an exception you are likely to get something similar to anonymous function in the stack trace. If you provide the optional name for a function expression then you will get the name of the function expression in the stack trace.
Rule Details
This rule can enforce or disallow the use of named function expressions.
Options
This rule has a string option:
"always" (default) requires function expressions to have a name
"as-needed" requires function expressions to have a name, if the name cannot be assigned automatically in an ES6 environment
"never" disallows named function expressions, except in recursive functions, where a name is needed
always
Examples of incorrect code for this rule with the default "always" option:
/*eslint func-names: ["error", "always"]*/
Foo.prototype.bar=function(){};
(function(){
// ...
}())
Examples of correct code for this rule with the default "always" option:
/*eslint func-names: ["error", "always"]*/
Foo.prototype.bar=functionbar(){};
(functionbar(){
// ...
}())
as-needed
ECMAScript 6 introduced a name property on all functions. The value of name is determined by evaluating the code around the function to see if a name can be inferred. For example, a function assigned to a variable will automatically have a name property equal to the name of the variable. The value of name is then used in stack traces for easier debugging.
Examples of incorrect code for this rule with the default "as-needed" option:
/*eslint func-names: ["error", "as-needed"]*/
Foo.prototype.bar=function(){};
(function(){
// ...
}())
Examples of correct code for this rule with the default "as-needed" option:
/*eslint func-names: ["error", "as-needed"]*/
varbar=function(){};
(functionbar(){
// ...
}())
never
Examples of incorrect code for this rule with the "never" option:
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar=functionbar(){};
(functionbar(){
// ...
}())
Examples of correct code for this rule with the "never" 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/
Require or disallow named function expressions (func-names)
A pattern that's becoming more common is to give function expressions names to aid in debugging. For example:
Foo.prototype.bar=functionbar(){};
Adding the second bar in the above example is optional. If you leave off the function name then when the function throws an exception you are likely to get something similar to anonymous function in the stack trace. If you provide the optional name for a function expression then you will get the name of the function expression in the stack trace.
Rule Details
This rule can enforce or disallow the use of named function expressions.
Options
This rule has a string option:
"always" (default) requires function expressions to have a name
"as-needed" requires function expressions to have a name, if the name cannot be assigned automatically in an ES6 environment
"never" disallows named function expressions, except in recursive functions, where a name is needed
always
Examples of incorrect code for this rule with the default "always" option:
/*eslint func-names: ["error", "always"]*/
Foo.prototype.bar=function(){};
(function(){
// ...
}())
Examples of correct code for this rule with the default "always" option:
/*eslint func-names: ["error", "always"]*/
Foo.prototype.bar=functionbar(){};
(functionbar(){
// ...
}())
as-needed
ECMAScript 6 introduced a name property on all functions. The value of name is determined by evaluating the code around the function to see if a name can be inferred. For example, a function assigned to a variable will automatically have a name property equal to the name of the variable. The value of name is then used in stack traces for easier debugging.
Examples of incorrect code for this rule with the default "as-needed" option:
/*eslint func-names: ["error", "as-needed"]*/
Foo.prototype.bar=function(){};
(function(){
// ...
}())
Examples of correct code for this rule with the default "as-needed" option:
/*eslint func-names: ["error", "as-needed"]*/
varbar=function(){};
(functionbar(){
// ...
}())
never
Examples of incorrect code for this rule with the "never" option:
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar=functionbar(){};
(functionbar(){
// ...
}())
Examples of correct code for this rule with the "never" option:
The block-scoped-var rule generates warnings when variables are used outside of the block in which they were defined. This emulates C-style block scope.
Rule Details
This rule aims to reduce the usage of variables outside of their binding context and emulate traditional block scope from other languages. This is to help newcomers to the language avoid difficult bugs with variable hoisting.
enforce the consistent use of either backticks, double, or single quotes (quotes)
JavaScript allows you to define strings in one of three ways: double quotes, single quotes, and backticks (as of ECMAScript 6). For example:
/*eslint-env es6*/
var double ="double";
var single ='single';
var backtick =`backtick`;// ES6 only
Each of these lines creates a string and, in some cases, can be used interchangeably. The choice of how to define strings in a codebase is a stylistic one outside of template literals (which allow embedded of expressions to be interpreted).
Many codebases require strings to be defined in a consistent manner.
Rule Details
This rule enforces the consistent use of either backticks, double, or single quotes.
Options
This rule has two options, a string option and an object option.
String option:
"double" (default) requires the use of double quotes wherever possible
"single" requires the use of single quotes wherever possible
"backtick" requires the use of backticks wherever possible
Object option:
"avoidEscape": true allows strings to use single-quotes or double-quotes so long as the string contains a quote that would have to be escaped otherwise
"allowTemplateLiterals": true allows strings to use backticks
Deprecated: The object property avoid-escape is deprecated; please use the object property avoidEscape instead.
double
Examples of incorrect code for this rule with the default "double" option:
/*eslint quotes: ["error", "double"]*/
var single ='single';
var unescaped ='a string containing "double" quotes';
Examples of correct code for this rule with the default "double" option:
/*eslint quotes: ["error", "double"]*/
/*eslint-env es6*/
var double ="double";
var backtick =`back\ntick`;// backticks are allowed due to newline
var backtick = tag`backtick`;// backticks are allowed due to tag
single
Examples of incorrect code for this rule with the "single" option:
/*eslint quotes: ["error", "single"]*/
var double ="double";
var unescaped ="a string containing 'single' quotes";
Examples of correct code for this rule with the "single" option:
/*eslint quotes: ["error", "single"]*/
/*eslint-env es6*/
var single ='single';
var backtick =`back${x}tick`;// backticks are allowed due to substitution
backticks
Examples of incorrect code for this rule with the "backtick" option:
/*eslint quotes: ["error", "backtick"]*/
var single ='single';
var double ="double";
var unescaped ='a string containing `backticks`';
Examples of correct code for this rule with the "backtick" option:
/*eslint quotes: ["error", "backtick"]*/
/*eslint-env es6*/
var backtick =`backtick`;
avoidEscape
Examples of additional correct code for this rule with the "double", { "avoidEscape": true } options:
If you do not need consistency in your string styles, you can safely disable this rule.
Source: http://eslint.org/docs/rules/
Treat var as Block Scoped (block-scoped-var)
The block-scoped-var rule generates warnings when variables are used outside of the block in which they were defined. This emulates C-style block scope.
Rule Details
This rule aims to reduce the usage of variables outside of their binding context and emulate traditional block scope from other languages. This is to help newcomers to the language avoid difficult bugs with variable hoisting.
Brace style is closely related to indent style in programming and describes the placement of braces relative to their control statement and body. There are probably a dozen, if not more, brace styles in the world.
The one true brace style is one of the most common brace styles in JavaScript, in which the opening brace of a block is placed on the same line as its corresponding statement or declaration. For example:
if(foo){
bar();
}else{
baz();
}
One common variant of one true brace style is called Stroustrup, in which the else statements in an if-else construct, as well as catch and finally, must be on its own line after the preceding closing brace. For example:
if(foo){
bar();
}
else{
baz();
}
Another style is called Allman, in which all the braces are expected to be on their own lines without any extra indentation. For example:
if(foo)
{
bar();
}
else
{
baz();
}
While no style is considered better than the other, most developers agree that having a consistent style throughout a project is important for its long-term maintainability.
Rule Details
This rule enforces consistent brace style for blocks.
Options
This rule has a string option:
"1tbs" (default) enforces one true brace style
"stroustrup" enforces Stroustrup style
"allman" enforces Allman style
This rule has an object option for an exception:
"allowSingleLine": true (default false) allows the opening and closing braces for a block to be on the same line
1tbs
Examples of incorrect code for this rule with the default "1tbs" option:
/*eslint brace-style: "error"*/
functionfoo()
{
returntrue;
}
if(foo)
{
bar();
}
try
{
somethingRisky();
}catch(e)
{
handleError();
}
if(foo){
bar();
}
else{
baz();
}
Examples of correct code for this rule with the default "1tbs" option:
/*eslint brace-style: "error"*/
functionfoo(){
returntrue;
}
if(foo){
bar();
}
if(foo){
bar();
}else{
baz();
}
try{
somethingRisky();
}catch(e){
handleError();
}
// when there are no braces, there are no problems
if(foo)bar();
elseif(baz)boom();
Examples of correct code for this rule with the "1tbs", { "allowSingleLine": true } 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.
Brace style is closely related to indent style in programming and describes the placement of braces relative to their control statement and body. There are probably a dozen, if not more, brace styles in the world.
The one true brace style is one of the most common brace styles in JavaScript, in which the opening brace of a block is placed on the same line as its corresponding statement or declaration. For example:
if(foo){
bar();
}else{
baz();
}
One common variant of one true brace style is called Stroustrup, in which the else statements in an if-else construct, as well as catch and finally, must be on its own line after the preceding closing brace. For example:
if(foo){
bar();
}
else{
baz();
}
Another style is called Allman, in which all the braces are expected to be on their own lines without any extra indentation. For example:
if(foo)
{
bar();
}
else
{
baz();
}
While no style is considered better than the other, most developers agree that having a consistent style throughout a project is important for its long-term maintainability.
Rule Details
This rule enforces consistent brace style for blocks.
Options
This rule has a string option:
"1tbs" (default) enforces one true brace style
"stroustrup" enforces Stroustrup style
"allman" enforces Allman style
This rule has an object option for an exception:
"allowSingleLine": true (default false) allows the opening and closing braces for a block to be on the same line
1tbs
Examples of incorrect code for this rule with the default "1tbs" option:
/*eslint brace-style: "error"*/
functionfoo()
{
returntrue;
}
if(foo)
{
bar();
}
try
{
somethingRisky();
}catch(e)
{
handleError();
}
if(foo){
bar();
}
else{
baz();
}
Examples of correct code for this rule with the default "1tbs" option:
/*eslint brace-style: "error"*/
functionfoo(){
returntrue;
}
if(foo){
bar();
}
if(foo){
bar();
}else{
baz();
}
try{
somethingRisky();
}catch(e){
handleError();
}
// when there are no braces, there are no problems
if(foo)bar();
elseif(baz)boom();
Examples of correct code for this rule with the "1tbs", { "allowSingleLine": true } options:
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";
functiondo_something(){
// ...
}
obj.do_something=function(){
// ...
};
var obj ={
my_pref:1
};
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";
varMY_FAVORITE_COLOR="#112C85";
var foo = bar.baz_boom;
var foo ={qux: bar.baz_boom };
obj.do_something();
do_something();
newdo_something();
var{category_id: category }= query;
never
Examples of correct code for this rule with the { "properties": "never" } option:
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/
Disallow Undeclared Variables (no-undef)
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.
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 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:
This is a boolean option and it is true by default. When set to false, this option allows the use of this without restriction and checks for dynamically assigned this values such as when using Array.prototype.map with a context argument. Normally, the rule will flag the use of this whenever a function does not use bind() to specify the value of this constantly.
Examples of incorrect code for the { "allowUnboundThis": false } option:
This rule should not be used in ES3/5 environments.
In ES2015 (ES6) or later, if you don't want to be notified about function expressions in an argument list, you can safely disable this rule.
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:
Require or disallow a space before function parenthesis (space-before-function-paren)
When formatting a function, whitespace is allowed between the function name or function keyword and the opening paren. Named functions also require a space between the function keyword and the function name, but anonymous functions require no whitespace. For example:
functionwithoutSpace(x){
// ...
}
functionwithSpace(x){
// ...
}
varanonymousWithoutSpace=function(){};
varanonymousWithSpace=function(){};
Style guides may require a space after the function keyword for anonymous functions, while others specify no whitespace. Similarly, the space after a function name may or may not be required.
Rule Details
This rule aims to enforce consistent spacing before function parentheses and as such, will warn whenever whitespace doesn't match the preferences specified.
Options
This rule has a string option or an object option:
{
"space-before-function-paren":["error","always"],
// or
"space-before-function-paren":["error",{
"anonymous":"always",
"named":"always",
"asyncArrow":"ignore"
}],
}
always (default) requires a space followed by the ( of arguments.
never disallows any space followed by the ( of arguments.
The string option does not check async arrow function expressions for backward compatibility.
You can also use a separate option for each type of function.
Each of the following options can be set to "always", "never", or "ignore".
Default is "always" basically.
anonymous is for anonymous function expressions (e.g. function () {}).
named is for named function expressions (e.g. function foo () {}).
asyncArrow is for async arrow function expressions (e.g. async () => {}).
asyncArrow is set to "ignore" by default for backwards compatibility.
"always"
Examples of incorrect code for this rule with the default "always" option:
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/
functionfoo(){
// ...
}
varbar=function(){
// ...
};
varbar=functionfoo(){
// ...
};
classFoo{
constructor(){
// ...
}
}
var foo ={
bar(){
// ...
}
};
Examples of correct code for this rule with the default "always" option:
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/
functionfoo(){
// ...
}
varbar=function(){
// ...
};
varbar=functionfoo(){
// ...
};
classFoo{
constructor(){
// ...
}
}
var foo ={
bar(){
// ...
}
};
// async arrow function expressions are ignored by default.
varfoo=async()=>1
varfoo=async()=>1
"never"
Examples of incorrect code for this rule with the "never" option:
When using the parseInt() function it is common to omit the second argument, the radix, and let the function try to determine from the first argument what type of number it is. By default, parseInt() will autodetect decimal and hexadecimal (via 0x prefix). Prior to ECMAScript 5, parseInt() also autodetected octal literals, which caused problems because many developers assumed a leading 0 would be ignored.
This confusion led to the suggestion that you always use the radix parameter to parseInt() to eliminate unintended consequences. So instead of doing this:
var num =parseInt("071");// 57
Do this:
var num =parseInt("071",10);// 71
ECMAScript 5 changed the behavior of parseInt() so that it no longer autodetects octal literals and instead treats them as decimal literals. However, the differences between hexadecimal and decimal interpretation of the first parameter causes many developers to continue using the radix parameter to ensure the string is interpreted in the intended way.
On the other hand, if the code is targeting only ES5-compliant environments passing the radix 10 may be redundant. In such a case you might want to disallow using such a radix.
Rule Details
This rule is aimed at preventing the unintended conversion of a string to a number of a different base than intended or at preventing the redundant 10 radix if targeting modern environments only.
Options
There are two options for this rule:
"always" enforces providing a radix (default)
"as-needed" disallows providing the 10 radix
always
Examples of incorrect code for the default "always" option:
/*eslint radix: "error"*/
var num =parseInt("071");
var num =parseInt(someValue);
var num =parseInt("071","abc");
var num =parseInt();
Examples of correct code for the default "always" option:
/*eslint radix: "error"*/
var num =parseInt("071",10);
var num =parseInt("071",8);
var num =parseFloat(someValue);
as-needed
Examples of incorrect code for the "as-needed" option:
/*eslint radix: ["error", "as-needed"]*/
var num =parseInt("071",10);
var num =parseInt("071","abc");
var num =parseInt();
Examples of correct code for the "as-needed" option:
/*eslint radix: ["error", "as-needed"]*/
var num =parseInt("071");
var num =parseInt("071",8);
var num =parseFloat(someValue);
When Not To Use It
If you don't want to enforce either presence or omission of the 10 radix value you can turn this rule off.
This is a boolean option and it is true by default. When set to false, this option allows the use of this without restriction and checks for dynamically assigned this values such as when using Array.prototype.map with a context argument. Normally, the rule will flag the use of this whenever a function does not use bind() to specify the value of this constantly.
Examples of incorrect code for the { "allowUnboundThis": false } option:
This rule should not be used in ES3/5 environments.
In ES2015 (ES6) or later, if you don't want to be notified about function expressions in an argument list, you can safely disable this rule.
Source: http://eslint.org/docs/rules/
Require or disallow a space before function parenthesis (space-before-function-paren)
When formatting a function, whitespace is allowed between the function name or function keyword and the opening paren. Named functions also require a space between the function keyword and the function name, but anonymous functions require no whitespace. For example:
functionwithoutSpace(x){
// ...
}
functionwithSpace(x){
// ...
}
varanonymousWithoutSpace=function(){};
varanonymousWithSpace=function(){};
Style guides may require a space after the function keyword for anonymous functions, while others specify no whitespace. Similarly, the space after a function name may or may not be required.
Rule Details
This rule aims to enforce consistent spacing before function parentheses and as such, will warn whenever whitespace doesn't match the preferences specified.
Options
This rule has a string option or an object option:
{
"space-before-function-paren":["error","always"],
// or
"space-before-function-paren":["error",{
"anonymous":"always",
"named":"always",
"asyncArrow":"ignore"
}],
}
always (default) requires a space followed by the ( of arguments.
never disallows any space followed by the ( of arguments.
The string option does not check async arrow function expressions for backward compatibility.
You can also use a separate option for each type of function.
Each of the following options can be set to "always", "never", or "ignore".
Default is "always" basically.
anonymous is for anonymous function expressions (e.g. function () {}).
named is for named function expressions (e.g. function foo () {}).
asyncArrow is for async arrow function expressions (e.g. async () => {}).
asyncArrow is set to "ignore" by default for backwards compatibility.
"always"
Examples of incorrect code for this rule with the default "always" option:
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/
functionfoo(){
// ...
}
varbar=function(){
// ...
};
varbar=functionfoo(){
// ...
};
classFoo{
constructor(){
// ...
}
}
var foo ={
bar(){
// ...
}
};
Examples of correct code for this rule with the default "always" option:
/*eslint space-before-function-paren: "error"*/
/*eslint-env es6*/
functionfoo(){
// ...
}
varbar=function(){
// ...
};
varbar=functionfoo(){
// ...
};
classFoo{
constructor(){
// ...
}
}
var foo ={
bar(){
// ...
}
};
// async arrow function expressions are ignored by default.
varfoo=async()=>1
varfoo=async()=>1
"never"
Examples of incorrect code for this rule with the "never" option:
JavaScript has a lot of language features, and not everyone likes all of them. As a result, some projects choose to disallow the use of certain language features altogether. For instance, you might decide to disallow the use of try-catch or class, or you might decide to disallow the use of the in operator.
Rather than creating separate rules for every language feature you want to turn off, this rule allows you to configure the syntax elements you want to restrict use of. These elements are represented by their ESTree node types. For example, a function declaration is represented by FunctionDeclaration and the with statement is represented by WithStatement. You may find the full list of AST node names you can use on GitHub and use the online parser to see what type of nodes your code consists of.
You can also specify [AST selectors](../developer-guide/selectors) to restrict, allowing much more precise control over syntax patterns.
Rule Details
This rule disallows specified (that is, user-defined) syntax.
Options
This rule takes a list of strings, where each string is an AST selector:
"message": "setTimeout must always be invoked with two arguments."
}
]
}
}
If a custom message is specified with the message property, ESLint will use that message when reporting occurrences of the syntax specified in the selector property.
The string and object formats can be freely mixed in the configuration as needed.
Examples of incorrect code for this rule with the "FunctionExpression", "WithStatement", BinaryExpression[operator='in'] options: