require or disallow assignment operator shorthand where possible (operator-assignment)
JavaScript provides shorthand operators that combine variable assignment and some simple mathematical operations. For example, x = x + 4 can be shortened to x += 4. The supported shorthand forms are as follows:
Shorthand | Separate
-----------|------------
x += y | x = x + y
x -= y | x = x - y
x *= y | x = x * y
x /= y | x = x / y
x %= y | x = x % y
x <<= y | x = x << y
x >>= y | x = x >> y
x >>>= y | x = x >>> y
x &= y | x = x & y
x ^= y | x = x ^ y
x |= y | x = x | y
Rule Details
This rule requires or disallows assignment operator shorthand where possible.
Options
This rule has a single string option:
"always" (default) requires assignment operator shorthand where possible
"never" disallows assignment operator shorthand
always
Examples of incorrect code for this rule with the default "always" option:
Use of operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis.
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 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.
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/
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:
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:
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.
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 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/
require or disallow trailing commas (comma-dangle)
Trailing commas in object literals are valid according to the ECMAScript 5 (and ECMAScript 3!) spec. However, IE8 (when not in IE8 document mode) and below will throw an error when it encounters trailing commas in JavaScript.
var foo ={
bar:"baz",
qux:"quux",
};
Trailing commas simplify adding and removing items to objects and arrays, since only the lines you are modifying must be touched.
Another argument in favor of trailing commas is that it improves the clarity of diffs when an item is added or removed from an object or array:
Less clear:
var foo = {
- bar: "baz",
- qux: "quux"
+ bar: "baz"
};
More clear:
var foo = {
bar: "baz",
- qux: "quux",
};
Rule Details
This rule enforces consistent use of trailing commas in object and array literals.
Options
This rule has a string option or an object option:
{
"comma-dangle": ["error", "never"],
// or
"comma-dangle": ["error", {
"arrays": "never",
"objects": "never",
"imports": "never",
"exports": "never",
"functions": "ignore",
}]
}
"never" (default) disallows trailing commas
"always" requires trailing commas
"always-multiline" requires trailing commas when the last element or property is in a different line than the closing ] or } and disallows trailing commas when the last element or property is on the same line as the closing ] or }
"only-multiline" allows (but does not require) trailing commas when the last element or property is in a different line than the closing ] or } and disallows trailing commas when the last element or property is on the same line as the closing ] or }
Trailing commas in function declarations and function calls are valid syntax since ECMAScript 2017; however, the string option does not check these situations for backwards compatibility.
You can also use an object option to configure this rule for each type of syntax.
Each of the following options can be set to "never", "always", "always-multiline", "only-multiline", or "ignore".
The default for each option is "never" unless otherwise specified.
arrays is for array literals and array patterns of destructuring. (e.g. let [a,] = [1,];)
objects is for object literals and object patterns of destructuring. (e.g. let {a,} = {a: 1};)
imports is for import declarations of ES Modules. (e.g. import {a,} from "foo";)
exports is for export declarations of ES Modules. (e.g. export {a,};)
functions is for function declarations and function calls. (e.g. (function(a,){ })(b,);) functions is set to "ignore" by default for consistency with the string option.
never
Examples of incorrect code for this rule with the default "never" option:
/*eslint comma-dangle: ["error", "never"]*/
var foo ={
bar:"baz",
qux:"quux",
};
var arr =[1,2,];
foo({
bar:"baz",
qux:"quux",
});
Examples of correct code for this rule with the default "never" option:
/*eslint comma-dangle: ["error", "never"]*/
var foo ={
bar:"baz",
qux:"quux"
};
var arr =[1,2];
foo({
bar:"baz",
qux:"quux"
});
always
Examples of incorrect code for this rule with the "always" option:
/*eslint comma-dangle: ["error", "always"]*/
var foo ={
bar:"baz",
qux:"quux"
};
var arr =[1,2];
foo({
bar:"baz",
qux:"quux"
});
Examples of correct code for this rule with the "always" option:
/*eslint comma-dangle: ["error", "always"]*/
var foo ={
bar:"baz",
qux:"quux",
};
var arr =[1,2,];
foo({
bar:"baz",
qux:"quux",
});
always-multiline
Examples of incorrect code for this rule with the "always-multiline" option:
enforce consistent spacing between keys and values in object literal properties (key-spacing)
This rule enforces spacing around the colon in object literal properties. It can verify each property individually, or it can ensure horizontal alignment of adjacent properties in an object literal.
Rule Details
This rule enforces consistent spacing between keys and values in object literal properties. In the case of long lines, it is acceptable to add a new line wherever whitespace is allowed.
Options
This rule has an object option:
"beforeColon": false (default) disallows spaces between the key and the colon in object literals.
"beforeColon": true requires at least one space between the key and the colon in object literals.
"afterColon": true (default) requires at least one space between the colon and the value in object literals.
"afterColon": false disallows spaces between the colon and the value in object literals.
"mode": "strict" (default) enforces exactly one space before or after colons in object literals.
"mode": "minimum" enforces one or more spaces before or after colons in object literals.
"align": "value" enforces horizontal alignment of values in object literals.
"align": "colon" enforces horizontal alignment of both colons and values in object literals.
"align" with an object value allows for fine-grained spacing when values are being aligned in object literals.
"singleLine" specifies a spacing style for single-line object literals.
"multiLine" specifies a spacing style for multi-line object literals.
Please note that you can either use the top-level options or the grouped options (singleLine and multiLine) but not both.
beforeColon
Examples of incorrect code for this rule with the default { "beforeColon": false } option:
The align option can take additional configuration through the beforeColon, afterColon, mode, and on options.
If align is defined as an object, but not all of the parameters are provided, undefined parameters will default to the following:
// Defaults
align:{
"beforeColon":false,
"afterColon":true,
"on":"colon",
"mode":"strict"
}
Examples of correct code for this rule with sample { "align": { } } options:
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj ={
"one":1,
"seven":7
}
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": false,
"afterColon": false,
"on": "value"
}
}]*/
var obj ={
"one":1,
"seven":7
}
align and multiLine
The multiLine and align options can differ, which allows for fine-tuned control over the key-spacing of your files. align will not inherit from multiLine if align is configured as an object.
multiLine is used any time an object literal spans multiple lines. The align configuration is used when there is a group of properties in the same object. For example:
var myObj ={
key1:1,// uses multiLine
key2:2,// uses align (when defined)
key3:3,// uses align (when defined)
key4:4// uses multiLine
}
Examples of incorrect code for this rule with sample { "align": { }, "multiLine": { } } options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon":true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj ={
"myObjectFunction":function(){
// Do something
},
"one":1,
"seven":7
}
Examples of correct code for this rule with sample { "align": { }, "multiLine": { } } options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon": true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj ={
"myObjectFunction":function(){
// Do something
//
},// These are two separate groups, so no alignment between `myObjectFuction` and `one`
"one":1,
"seven":7// `one` and `seven` are in their own group, and therefore aligned
}
singleLine and multiLine
Examples of correct code for this rule with sample { "singleLine": { }, "multiLine": { } } options:
/*eslint "key-spacing": [2, {
"singleLine": {
"beforeColon": false,
"afterColon": true
},
"multiLine": {
"beforeColon": true,
"afterColon": true,
"align": "colon"
}
}]*/
var obj ={one:1,"two":2,three:3};
var obj2 ={
"two":2,
three:3
};
When Not To Use It
If you have another convention for property spacing that might not be consistent with the available options, or if you want to permit multiple styles concurrently you can safely disable this rule.
Source: http://eslint.org/docs/rules/
Require Variable Declarations to be at the top of their scope (vars-on-top)
The vars-on-top rule generates warnings when variable declarations are not used serially at the top of a function scope or the top of a program.
By default variable declarations are always moved (“hoisted”) invisibly to the top of their containing scope by the JavaScript interpreter.
This rule forces the programmer to represent that behaviour by manually moving the variable declaration to the top of its containing scope.
Rule Details
This rule aims to keep all variable declarations in the leading series of statements.
Allowing multiple declarations helps promote maintainability and is thus allowed.
Examples of incorrect code for this rule:
/*eslint vars-on-top: "error"*/
// Variable declarations in a block:
functiondoSomething(){
var first;
if(true){
first =true;
}
var second;
}
// Variable declaration in for initializer:
functiondoSomething(){
for(var i=0; i<10; i++){}
}
/*eslint vars-on-top: "error"*/
// Variables after other statements:
f();
var a;
Examples of correct code for this rule:
/*eslint vars-on-top: "error"*/
functiondoSomething(){
var first;
var second;//multiple declarations are allowed at the top
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:
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.
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/
enforce consistent spacing between keys and values in object literal properties (key-spacing)
This rule enforces spacing around the colon in object literal properties. It can verify each property individually, or it can ensure horizontal alignment of adjacent properties in an object literal.
Rule Details
This rule enforces consistent spacing between keys and values in object literal properties. In the case of long lines, it is acceptable to add a new line wherever whitespace is allowed.
Options
This rule has an object option:
"beforeColon": false (default) disallows spaces between the key and the colon in object literals.
"beforeColon": true requires at least one space between the key and the colon in object literals.
"afterColon": true (default) requires at least one space between the colon and the value in object literals.
"afterColon": false disallows spaces between the colon and the value in object literals.
"mode": "strict" (default) enforces exactly one space before or after colons in object literals.
"mode": "minimum" enforces one or more spaces before or after colons in object literals.
"align": "value" enforces horizontal alignment of values in object literals.
"align": "colon" enforces horizontal alignment of both colons and values in object literals.
"align" with an object value allows for fine-grained spacing when values are being aligned in object literals.
"singleLine" specifies a spacing style for single-line object literals.
"multiLine" specifies a spacing style for multi-line object literals.
Please note that you can either use the top-level options or the grouped options (singleLine and multiLine) but not both.
beforeColon
Examples of incorrect code for this rule with the default { "beforeColon": false } option:
The align option can take additional configuration through the beforeColon, afterColon, mode, and on options.
If align is defined as an object, but not all of the parameters are provided, undefined parameters will default to the following:
// Defaults
align:{
"beforeColon":false,
"afterColon":true,
"on":"colon",
"mode":"strict"
}
Examples of correct code for this rule with sample { "align": { } } options:
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj ={
"one":1,
"seven":7
}
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": false,
"afterColon": false,
"on": "value"
}
}]*/
var obj ={
"one":1,
"seven":7
}
align and multiLine
The multiLine and align options can differ, which allows for fine-tuned control over the key-spacing of your files. align will not inherit from multiLine if align is configured as an object.
multiLine is used any time an object literal spans multiple lines. The align configuration is used when there is a group of properties in the same object. For example:
var myObj ={
key1:1,// uses multiLine
key2:2,// uses align (when defined)
key3:3,// uses align (when defined)
key4:4// uses multiLine
}
Examples of incorrect code for this rule with sample { "align": { }, "multiLine": { } } options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon":true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj ={
"myObjectFunction":function(){
// Do something
},
"one":1,
"seven":7
}
Examples of correct code for this rule with sample { "align": { }, "multiLine": { } } options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon": true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj ={
"myObjectFunction":function(){
// Do something
//
},// These are two separate groups, so no alignment between `myObjectFuction` and `one`
"one":1,
"seven":7// `one` and `seven` are in their own group, and therefore aligned
}
singleLine and multiLine
Examples of correct code for this rule with sample { "singleLine": { }, "multiLine": { } } options:
/*eslint "key-spacing": [2, {
"singleLine": {
"beforeColon": false,
"afterColon": true
},
"multiLine": {
"beforeColon": true,
"afterColon": true,
"align": "colon"
}
}]*/
var obj ={one:1,"two":2,three:3};
var obj2 ={
"two":2,
three:3
};
When Not To Use It
If you have another convention for property spacing that might not be consistent with the available options, or if you want to permit multiple styles concurrently you can safely disable this rule.
Source: http://eslint.org/docs/rules/
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.