Showing 270 of 270 total issues
Unexpected parentheses around single function argument. Open
const intersection = searchedFiles.filter((searchedFile) => changedFiles.indexOf(searchedFile) > -1);
- Read upRead up
- Exclude checks
Require parens in arrow function arguments (arrow-parens)
Arrow functions can omit parentheses when they have exactly one parameter. In all other cases the parameter(s) must be wrapped in parentheses. This rule enforces the consistent use of parentheses in arrow functions.
Rule Details
This rule enforces parentheses around arrow function parameters regardless of arity. For example:
/*eslint-env es6*/
// Bad
a => {}
// Good
(a) => {}
Following this style will help you find arrow functions (=>
) which may be mistakenly included in a condition
when a comparison such as >=
was the intent.
/*eslint-env es6*/
// Bad
if (a => 2) {
}
// Good
if (a >= 2) {
}
The rule can also be configured to discourage the use of parens when they are not required:
/*eslint-env es6*/
// Bad
(a) => {}
// Good
a => {}
Options
This rule has a string option and an object one.
String options are:
-
"always"
(default) requires parens around arguments in all cases. -
"as-needed"
allows omitting parens when there is only one argument.
Object properties for variants of the "as-needed"
option:
-
"requireForBlockBody": true
modifies the as-needed rule in order to require parens if the function body is in an instructions block (surrounded by braces).
always
Examples of incorrect code for this rule with the default "always"
option:
/*eslint arrow-parens: ["error", "always"]*/
/*eslint-env es6*/
a => {};
a => a;
a => {'\n'};
a.then(foo => {});
a.then(foo => a);
a(foo => { if (true) {} });
Examples of correct code for this rule with the default "always"
option:
/*eslint arrow-parens: ["error", "always"]*/
/*eslint-env es6*/
() => {};
(a) => {};
(a) => a;
(a) => {'\n'}
a.then((foo) => {});
a.then((foo) => { if (true) {} });
If Statements
One of benefits of this option is that it prevents the incorrect use of arrow functions in conditionals:
/*eslint-env es6*/
var a = 1;
var b = 2;
// ...
if (a => b) {
console.log('bigger');
} else {
console.log('smaller');
}
// outputs 'bigger', not smaller as expected
The contents of the if
statement is an arrow function, not a comparison.
If the arrow function is intentional, it should be wrapped in parens to remove ambiguity.
/*eslint-env es6*/
var a = 1;
var b = 0;
// ...
if ((a) => b) {
console.log('truthy value returned');
} else {
console.log('falsey value returned');
}
// outputs 'truthy value returned'
The following is another example of this behavior:
/*eslint-env es6*/
var a = 1, b = 2, c = 3, d = 4;
var f = a => b ? c: d;
// f = ?
f
is an arrow function which takes a
as an argument and returns the result of b ? c: d
.
This should be rewritten like so:
/*eslint-env es6*/
var a = 1, b = 2, c = 3, d = 4;
var f = (a) => b ? c: d;
as-needed
Examples of incorrect code for this rule with the "as-needed"
option:
/*eslint arrow-parens: ["error", "as-needed"]*/
/*eslint-env es6*/
(a) => {};
(a) => a;
(a) => {'\n'};
a.then((foo) => {});
a.then((foo) => a);
a((foo) => { if (true) {} });
Examples of correct code for this rule with the "as-needed"
option:
/*eslint arrow-parens: ["error", "as-needed"]*/
/*eslint-env es6*/
() => {};
a => {};
a => a;
a => {'\n'};
a.then(foo => {});
a.then(foo => { if (true) {} });
(a, b, c) => a;
(a = 10) => a;
([a, b]) => a;
({a, b}) => a;
requireForBlockBody
Examples of incorrect code for the { "requireForBlockBody": true }
option:
/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/
/*eslint-env es6*/
(a) => a;
a => {};
a => {'\n'};
a.map((x) => x * x);
a.map(x => {
return x * x;
});
a.then(foo => {});
Examples of correct code for the { "requireForBlockBody": true }
option:
/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/
/*eslint-env es6*/
(a) => {};
(a) => {'\n'};
a => ({});
() => {};
a => a;
a.then((foo) => {});
a.then((foo) => { if (true) {} });
a((foo) => { if (true) {} });
(a, b, c) => a;
(a = 10) => a;
([a, b]) => a;
({a, b}) => a;
Further Reading
- The
"as-needed", { "requireForBlockBody": true }
rule is directly inspired by the Airbnb JS Style Guide. Source: http://eslint.org/docs/rules/
Comments should not begin with a lowercase character Open
// await css and html, inject them
- Read upRead up
- Exclude checks
enforce or disallow capitalization of the first letter of a comment (capitalized-comments)
Comments are useful for leaving information for future developers. In order for that information to be useful and not distracting, it is sometimes desirable for comments to follow a particular style. One element of comment formatting styles is whether the first word of a comment should be capitalized or lowercase.
In general, no comment style is any more or less valid than any others, but many developers would agree that a consistent style can improve a project's maintainability.
Rule Details
This rule aims to enforce a consistent style of comments across your codebase, specifically by either requiring or disallowing a capitalized letter as the first word character in a comment. This rule will not issue warnings when non-cased letters are used.
By default, this rule will require a non-lowercase letter at the beginning of comments.
Examples of incorrect code for this rule:
/* eslint capitalized-comments: ["error"] */
// lowercase comment
Examples of correct code for this rule:
// Capitalized comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
/* eslint semi:off */
/* eslint-env node */
/* eslint-disable */
/* eslint-enable */
/* istanbul ignore next */
/* jscs:enable */
/* jshint asi:true */
/* global foo */
/* globals foo */
/* exported myVar */
// eslint-disable-line
// eslint-disable-next-line
// https://github.com
Options
This rule has two options: a string value "always"
or "never"
which determines whether capitalization of the first word of a comment should be required or forbidden, and optionally an object containing more configuration parameters for the rule.
Here are the supported object options:
-
ignorePattern
: A string representing a regular expression pattern of words that should be ignored by this rule. If the first word of a comment matches the pattern, this rule will not report that comment.- Note that the following words are always ignored by this rule:
["jscs", "jshint", "eslint", "istanbul", "global", "globals", "exported"]
.
- Note that the following words are always ignored by this rule:
-
ignoreInlineComments
: If this istrue
, the rule will not report on comments in the middle of code. By default, this isfalse
. -
ignoreConsecutiveComments
: If this istrue
, the rule will not report on a comment which violates the rule, as long as the comment immediately follows another comment. By default, this isfalse
.
Here is an example configuration:
{
"capitalized-comments": [
"error",
"always",
{
"ignorePattern": "pragma|ignored",
"ignoreInlineComments": true
}
]
}
"always"
Using the "always"
option means that this rule will report any comments which start with a lowercase letter. This is the default configuration for this rule.
Note that configuration comments and comments which start with URLs are never reported.
Examples of incorrect code for this rule:
/* eslint capitalized-comments: ["error", "always"] */
// lowercase comment
Examples of correct code for this rule:
/* eslint capitalized-comments: ["error", "always"] */
// Capitalized comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
/* eslint semi:off */
/* eslint-env node */
/* eslint-disable */
/* eslint-enable */
/* istanbul ignore next */
/* jscs:enable */
/* jshint asi:true */
/* global foo */
/* globals foo */
/* exported myVar */
// eslint-disable-line
// eslint-disable-next-line
// https://github.com
"never"
Using the "never"
option means that this rule will report any comments which start with an uppercase letter.
Examples of incorrect code with the "never"
option:
/* eslint capitalized-comments: ["error", "never"] */
// Capitalized comment
Examples of correct code with the "never"
option:
/* eslint capitalized-comments: ["error", "never"] */
// lowercase comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
ignorePattern
The ignorePattern
object takes a string value, which is used as a regular expression applied to the first word of a comment.
Examples of correct code with the "ignorePattern"
option set to "pragma"
:
/* eslint capitalized-comments: ["error", "always", { "ignorePattern": "pragma" }] */
function foo() {
/* pragma wrap(true) */
}
ignoreInlineComments
Setting the ignoreInlineComments
option to true
means that comments in the middle of code (with a token on the same line as the beginning of the comment, and another token on the same line as the end of the comment) will not be reported by this rule.
Examples of correct code with the "ignoreInlineComments"
option set to true
:
/* eslint capitalized-comments: ["error", "always", { "ignoreInlineComments": true }] */
function foo(/* ignored */ a) {
}
ignoreConsecutiveComments
If the ignoreConsecutiveComments
option is set to true
, then comments which otherwise violate the rule will not be reported as long as they immediately follow another comment. This can be applied more than once.
Examples of correct code with ignoreConsecutiveComments
set to true
:
/* eslint capitalize-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
// This comment is valid since it has the correct capitalization.
// this comment is ignored since it follows another comment,
// and this one as well because it follows yet another comment.
/* Here is a block comment which has the correct capitalization, */
/* but this one is ignored due to being consecutive; */
/*
* in fact, even if any of these are multi-line, that is fine too.
*/
Examples of incorrect code with ignoreConsecutiveComments
set to true
:
/* eslint capitalize-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
// this comment is invalid, but only on this line.
// this comment does NOT get reported, since it is a consecutive comment.
Using Different Options for Line and Block Comments
If you wish to have a different configuration for line comments and block comments, you can do so by using two different object configurations (note that the capitalization option will be enforced consistently for line and block comments):
{
"capitalized-comments": [
"error",
"always",
{
"line": {
"ignorePattern": "pragma|ignored",
},
"block": {
"ignoreInlineComments": true,
"ignorePattern": "ignored"
}
}
]
}
Examples of incorrect code with different line and block comment configuration:
/* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */
// capitalized line comment, this is incorrect, blockignore does not help here
/* lowercased block comment, this is incorrect too */
Examples of correct code with different line and block comment configuration:
/* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */
// Uppercase line comment, this is correct
/* blockignore lowercase block comment, this is correct due to ignorePattern */
When Not To Use It
This rule can be disabled if you do not care about the grammatical style of comments in your codebase.
Compatibility
Unexpected var, use let or const instead. Open
var className = item.active ? 'jogwheel-item jogwheel-item--active' : 'jogwheel-item';
- Read upRead up
- Exclude checks
require let
or const
instead of var
(no-var)
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";
var CONFIG = {};
Examples of correct code for this rule:
/*eslint no-var: "error"*/
/*eslint-env es6*/
let x = "y";
const CONFIG = {};
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/
Unexpected var, use let or const instead. Open
var transpile = require('./transpile')(gulp, paths, watchOptions, cli);
- Read upRead up
- Exclude checks
require let
or const
instead of var
(no-var)
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";
var CONFIG = {};
Examples of correct code for this rule:
/*eslint no-var: "error"*/
/*eslint-env es6*/
let x = "y";
const CONFIG = {};
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/
Unexpected named function 'pack'. Open
return function pack() {
- Read upRead up
- Exclude checks
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 = function bar() {};
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 = function bar() {};
(function bar() {
// ...
}())
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"]*/
var bar = function() {};
(function bar() {
// ...
}())
never
Examples of incorrect code for this rule with the "never"
option:
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar = function bar() {};
(function bar() {
// ...
}())
Examples of correct code for this rule with the "never"
option:
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar = function() {};
(function() {
// ...
}())
Further Reading
Compatibility
- JSCS: requireAnonymousFunctions
- JSCS: disallowAnonymousFunctions Source: http://eslint.org/docs/rules/
Unexpected var, use let or const instead. Open
var href = item.href || `#${name.split(' ').join('-').toLowerCase()}`;
- Read upRead up
- Exclude checks
require let
or const
instead of var
(no-var)
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";
var CONFIG = {};
Examples of correct code for this rule:
/*eslint no-var: "error"*/
/*eslint-env es6*/
let x = "y";
const CONFIG = {};
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/
Missing space before function parentheses. Open
${props.navigation.map(function(item) {
- Read upRead up
- Exclude checks
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:
function withoutSpace(x) {
// ...
}
function withSpace (x) {
// ...
}
var anonymousWithoutSpace = function() {};
var anonymousWithSpace = 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*/
function foo() {
// ...
}
var bar = function() {
// ...
};
var bar = function foo() {
// ...
};
class Foo {
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*/
function foo () {
// ...
}
var bar = function () {
// ...
};
var bar = function foo () {
// ...
};
class Foo {
constructor () {
// ...
}
}
var foo = {
bar () {
// ...
}
};
// async arrow function expressions are ignored by default.
var foo = async () => 1
var foo = async() => 1
"never"
Examples of incorrect code for this rule with the "never"
option:
/*eslint space-before-function-paren: ["error", "never"]*/
/*eslint-env es6*/
function foo () {
// ...
}
var bar = function () {
// ...
};
var bar = function foo () {
// ...
};
class Foo {
constructor () {
// ...
}
}
var foo = {
bar () {
// ...
}
};
Examples of correct code for this rule with the "never"
option:
/*eslint space-before-function-paren: ["error", "never"]*/
/*eslint-env es6*/
function foo() {
// ...
}
var bar = function() {
// ...
};
var bar = function foo() {
// ...
};
class Foo {
constructor() {
// ...
}
}
var foo = {
bar() {
// ...
}
};
// async arrow function expressions are ignored by default.
var foo = async () => 1
var foo = async() => 1
{"anonymous": "always", "named": "never", "asyncArrow": "always"}
Examples of incorrect code for this rule with the {"anonymous": "always", "named": "never", "asyncArrow": "always"}
option:
/*eslint space-before-function-paren: ["error", {"anonymous": "always", "named": "never", "asyncArrow": "always"}]*/
/*eslint-env es6*/
function foo () {
// ...
}
var bar = function() {
// ...
};
class Foo {
constructor () {
// ...
}
}
var foo = {
bar () {
// ...
}
};
var foo = async(a) => await a
Examples of correct code for this rule with the {"anonymous": "always", "named": "never", "asyncArrow": "always"}
option:
/*eslint space-before-function-paren: ["error", {"anonymous": "always", "named": "never", "asyncArrow": "always"}]*/
/*eslint-env es6*/
function foo() {
// ...
}
var bar = function () {
// ...
};
class Foo {
constructor() {
// ...
}
}
var foo = {
bar() {
// ...
}
};
var foo = async (a) => await a
{"anonymous": "never", "named": "always"}
Examples of incorrect code for this rule with the {"anonymous": "never", "named": "always"}
option:
/*eslint space-before-function-paren: ["error", { "anonymous": "never", "named": "always" }]*/
/*eslint-env es6*/
function foo() {
// ...
}
var bar = function () {
// ...
};
class Foo {
constructor() {
// ...
}
}
var foo = {
bar() {
// ...
}
};
Examples of correct code for this rule with the {"anonymous": "never", "named": "always"}
option:
/*eslint space-before-function-paren: ["error", { "anonymous": "never", "named": "always" }]*/
/*eslint-env es6*/
function foo () {
// ...
}
var bar = function() {
// ...
};
class Foo {
constructor () {
// ...
}
}
var foo = {
bar () {
// ...
}
};
{"anonymous": "ignore", "named": "always"}
Examples of incorrect code for this rule with the {"anonymous": "ignore", "named": "always"}
option:
/*eslint space-before-function-paren: ["error", { "anonymous": "ignore", "named": "always" }]*/
/*eslint-env es6*/
function foo() {
// ...
}
class Foo {
constructor() {
// ...
}
}
var foo = {
bar() {
// ...
}
};
Examples of correct code for this rule with the {"anonymous": "ignore", "named": "always"}
option:
/*eslint space-before-function-paren: ["error", { "anonymous": "ignore", "named": "always" }]*/
/*eslint-env es6*/
var bar = function() {
// ...
};
var bar = function () {
// ...
};
function foo () {
// ...
}
class Foo {
constructor () {
// ...
}
}
var foo = {
bar () {
// ...
}
};
When Not To Use It
You can turn this rule off if you are not concerned with the consistency of spacing before function parenthesis.
Related Rules
- [space-after-keywords](space-after-keywords.md)
- [space-return-throw-case](space-return-throw-case.md) Source: http://eslint.org/docs/rules/
Missing semicolon. Open
return `<li class="${className}"><a href="${item.href}">${item.name}</a></li>`
- Read upRead up
- Exclude checks
require or disallow semicolons instead of ASI (semi)
JavaScript is unique amongst the C-like languages in that it doesn't require semicolons at the end of each statement. In many cases, the JavaScript engine can determine that a semicolon should be in a certain spot and will automatically add it. This feature is known as automatic semicolon insertion (ASI) and is considered one of the more controversial features of JavaScript. For example, the following lines are both valid:
var name = "ESLint"
var website = "eslint.org";
On the first line, the JavaScript engine will automatically insert a semicolon, so this is not considered a syntax error. The JavaScript engine still knows how to interpret the line and knows that the line end indicates the end of the statement.
In the debate over ASI, there are generally two schools of thought. The first is that we should treat ASI as if it didn't exist and always include semicolons manually. The rationale is that it's easier to always include semicolons than to try to remember when they are or are not required, and thus decreases the possibility of introducing an error.
However, the ASI mechanism can sometimes be tricky to people who are using semicolons. For example, consider this code:
return
{
name: "ESLint"
};
This may look like a return
statement that returns an object literal, however, the JavaScript engine will interpret this code as:
return;
{
name: "ESLint";
}
Effectively, a semicolon is inserted after the return
statement, causing the code below it (a labeled literal inside a block) to be unreachable. This rule and the [no-unreachable](no-unreachable.md) rule will protect your code from such cases.
On the other side of the argument are those who says that since semicolons are inserted automatically, they are optional and do not need to be inserted manually. However, the ASI mechanism can also be tricky to people who don't use semicolons. For example, consider this code:
var globalCounter = { }
(function () {
var n = 0
globalCounter.increment = function () {
return ++n
}
})()
In this example, a semicolon will not be inserted after the first line, causing a run-time error (because an empty object is called as if it's a function). The [no-unexpected-multiline](no-unexpected-multiline.md) rule can protect your code from such cases.
Although ASI allows for more freedom over your coding style, it can also make your code behave in an unexpected way, whether you use semicolons or not. Therefore, it is best to know when ASI takes place and when it does not, and have ESLint protect your code from these potentially unexpected cases. In short, as once described by Isaac Schlueter, a \n
character always ends a statement (just like a semicolon) unless one of the following is true:
- The statement has an unclosed paren, array literal, or object literal or ends in some other way that is not a valid way to end a statement. (For instance, ending with
.
or,
.) - The line is
--
or++
(in which case it will decrement/increment the next token.) - It is a
for()
,while()
,do
,if()
, orelse
, and there is no{
- The next line starts with
[
,(
,+
,*
,/
,-
,,
,.
, or some other binary operator that can only be found between two tokens in a single expression.
Rule Details
This rule enforces consistent use of semicolons.
Options
This rule has two options, a string option and an object option.
String option:
-
"always"
(default) requires semicolons at the end of statements -
"never"
disallows semicolons as the end of statements (except to disambiguate statements beginning with[
,(
,/
,+
, or-
)
Object option:
-
"omitLastInOneLineBlock": true
ignores the last semicolon in a block in which its braces (and therefore the content of the block) are in the same line
always
Examples of incorrect code for this rule with the default "always"
option:
/*eslint semi: ["error", "always"]*/
var name = "ESLint"
object.method = function() {
// ...
}
Examples of correct code for this rule with the default "always"
option:
/*eslint semi: "error"*/
var name = "ESLint";
object.method = function() {
// ...
};
never
Examples of incorrect code for this rule with the "never"
option:
/*eslint semi: ["error", "never"]*/
var name = "ESLint";
object.method = function() {
// ...
};
Examples of correct code for this rule with the "never"
option:
/*eslint semi: ["error", "never"]*/
var name = "ESLint"
object.method = function() {
// ...
}
var name = "ESLint"
;(function() {
// ...
})()
omitLastInOneLineBlock
Examples of additional correct code for this rule with the "always", { "omitLastInOneLineBlock": true }
options:
/*eslint semi: ["error", "always", { "omitLastInOneLineBlock": true}] */
if (foo) { bar() }
if (foo) { bar(); baz() }
When Not To Use It
If you do not want to enforce semicolon usage (or omission) in any particular way, then you can turn this rule off.
Further Reading
Related Rules
- [no-extra-semi](no-extra-semi.md)
- [no-unexpected-multiline](no-unexpected-multiline.md)
- [semi-spacing](semi-spacing.md) Source: http://eslint.org/docs/rules/
Unexpected var, use let or const instead. Open
var cached = require('gulp-cached');
- Read upRead up
- Exclude checks
require let
or const
instead of var
(no-var)
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";
var CONFIG = {};
Examples of correct code for this rule:
/*eslint no-var: "error"*/
/*eslint-env es6*/
let x = "y";
const CONFIG = {};
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/
Unexpected var, use let or const instead. Open
var build = require('./build')(gulp, paths, {fails: true, notifies: true}, cli);
- Read upRead up
- Exclude checks
require let
or const
instead of var
(no-var)
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";
var CONFIG = {};
Examples of correct code for this rule:
/*eslint no-var: "error"*/
/*eslint-env es6*/
let x = "y";
const CONFIG = {};
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/
Do not access Object.prototype method 'hasOwnProperty' from target object. Open
if (trap.hasOwnProperty(key)) {
- Read upRead up
- Exclude checks
Disallow use of Object.prototypes builtins directly (no-prototype-builtins)
In ECMAScript 5.1, Object.create
was added, which enables the creation of objects with a specified [[Prototype]]
. Object.create(null)
is a common pattern used to create objects that will be used as a Map. This can lead to errors when it is assumed that objects will have properties from Object.prototype
. This rule prevents calling Object.prototype
methods directly from an object.
Rule Details
This rule disallows calling some Object.prototype
methods directly on object instances.
Examples of incorrect code for this rule:
/*eslint no-prototype-builtins: "error"*/
var hasBarProperty = foo.hasOwnProperty("bar");
var isPrototypeOfBar = foo.isPrototypeOf(bar);
var barIsEnumerable = foo.propertyIsEnumerable("bar");
Examples of correct code for this rule:
/*eslint no-prototype-builtins: "error"*/
var hasBarProperty = Object.prototype.hasOwnProperty.call(foo, "bar");
var isPrototypeOfBar = Object.prototype.isPrototypeOf.call(foo, bar);
var barIsEnumerable = {}.propertyIsEnumerable.call(foo, "bar");
When Not To Use It
You may want to turn this rule off if you will never use an object that shadows an Object.prototype
method or which does not inherit from Object.prototype
.
Source: http://eslint.org/docs/rules/
Comments should not begin with a lowercase character Open
// filter not all media query
- Read upRead up
- Exclude checks
enforce or disallow capitalization of the first letter of a comment (capitalized-comments)
Comments are useful for leaving information for future developers. In order for that information to be useful and not distracting, it is sometimes desirable for comments to follow a particular style. One element of comment formatting styles is whether the first word of a comment should be capitalized or lowercase.
In general, no comment style is any more or less valid than any others, but many developers would agree that a consistent style can improve a project's maintainability.
Rule Details
This rule aims to enforce a consistent style of comments across your codebase, specifically by either requiring or disallowing a capitalized letter as the first word character in a comment. This rule will not issue warnings when non-cased letters are used.
By default, this rule will require a non-lowercase letter at the beginning of comments.
Examples of incorrect code for this rule:
/* eslint capitalized-comments: ["error"] */
// lowercase comment
Examples of correct code for this rule:
// Capitalized comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
/* eslint semi:off */
/* eslint-env node */
/* eslint-disable */
/* eslint-enable */
/* istanbul ignore next */
/* jscs:enable */
/* jshint asi:true */
/* global foo */
/* globals foo */
/* exported myVar */
// eslint-disable-line
// eslint-disable-next-line
// https://github.com
Options
This rule has two options: a string value "always"
or "never"
which determines whether capitalization of the first word of a comment should be required or forbidden, and optionally an object containing more configuration parameters for the rule.
Here are the supported object options:
-
ignorePattern
: A string representing a regular expression pattern of words that should be ignored by this rule. If the first word of a comment matches the pattern, this rule will not report that comment.- Note that the following words are always ignored by this rule:
["jscs", "jshint", "eslint", "istanbul", "global", "globals", "exported"]
.
- Note that the following words are always ignored by this rule:
-
ignoreInlineComments
: If this istrue
, the rule will not report on comments in the middle of code. By default, this isfalse
. -
ignoreConsecutiveComments
: If this istrue
, the rule will not report on a comment which violates the rule, as long as the comment immediately follows another comment. By default, this isfalse
.
Here is an example configuration:
{
"capitalized-comments": [
"error",
"always",
{
"ignorePattern": "pragma|ignored",
"ignoreInlineComments": true
}
]
}
"always"
Using the "always"
option means that this rule will report any comments which start with a lowercase letter. This is the default configuration for this rule.
Note that configuration comments and comments which start with URLs are never reported.
Examples of incorrect code for this rule:
/* eslint capitalized-comments: ["error", "always"] */
// lowercase comment
Examples of correct code for this rule:
/* eslint capitalized-comments: ["error", "always"] */
// Capitalized comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
/* eslint semi:off */
/* eslint-env node */
/* eslint-disable */
/* eslint-enable */
/* istanbul ignore next */
/* jscs:enable */
/* jshint asi:true */
/* global foo */
/* globals foo */
/* exported myVar */
// eslint-disable-line
// eslint-disable-next-line
// https://github.com
"never"
Using the "never"
option means that this rule will report any comments which start with an uppercase letter.
Examples of incorrect code with the "never"
option:
/* eslint capitalized-comments: ["error", "never"] */
// Capitalized comment
Examples of correct code with the "never"
option:
/* eslint capitalized-comments: ["error", "never"] */
// lowercase comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
ignorePattern
The ignorePattern
object takes a string value, which is used as a regular expression applied to the first word of a comment.
Examples of correct code with the "ignorePattern"
option set to "pragma"
:
/* eslint capitalized-comments: ["error", "always", { "ignorePattern": "pragma" }] */
function foo() {
/* pragma wrap(true) */
}
ignoreInlineComments
Setting the ignoreInlineComments
option to true
means that comments in the middle of code (with a token on the same line as the beginning of the comment, and another token on the same line as the end of the comment) will not be reported by this rule.
Examples of correct code with the "ignoreInlineComments"
option set to true
:
/* eslint capitalized-comments: ["error", "always", { "ignoreInlineComments": true }] */
function foo(/* ignored */ a) {
}
ignoreConsecutiveComments
If the ignoreConsecutiveComments
option is set to true
, then comments which otherwise violate the rule will not be reported as long as they immediately follow another comment. This can be applied more than once.
Examples of correct code with ignoreConsecutiveComments
set to true
:
/* eslint capitalize-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
// This comment is valid since it has the correct capitalization.
// this comment is ignored since it follows another comment,
// and this one as well because it follows yet another comment.
/* Here is a block comment which has the correct capitalization, */
/* but this one is ignored due to being consecutive; */
/*
* in fact, even if any of these are multi-line, that is fine too.
*/
Examples of incorrect code with ignoreConsecutiveComments
set to true
:
/* eslint capitalize-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
// this comment is invalid, but only on this line.
// this comment does NOT get reported, since it is a consecutive comment.
Using Different Options for Line and Block Comments
If you wish to have a different configuration for line comments and block comments, you can do so by using two different object configurations (note that the capitalization option will be enforced consistently for line and block comments):
{
"capitalized-comments": [
"error",
"always",
{
"line": {
"ignorePattern": "pragma|ignored",
},
"block": {
"ignoreInlineComments": true,
"ignorePattern": "ignored"
}
}
]
}
Examples of incorrect code with different line and block comment configuration:
/* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */
// capitalized line comment, this is incorrect, blockignore does not help here
/* lowercased block comment, this is incorrect too */
Examples of correct code with different line and block comment configuration:
/* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */
// Uppercase line comment, this is correct
/* blockignore lowercase block comment, this is correct due to ignorePattern */
When Not To Use It
This rule can be disabled if you do not care about the grammatical style of comments in your codebase.
Compatibility
Unexpected named async function 'inject'. Open
return async function inject(asset) {
- Read upRead up
- Exclude checks
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 = function bar() {};
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 = function bar() {};
(function bar() {
// ...
}())
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"]*/
var bar = function() {};
(function bar() {
// ...
}())
never
Examples of incorrect code for this rule with the "never"
option:
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar = function bar() {};
(function bar() {
// ...
}())
Examples of correct code for this rule with the "never"
option:
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar = function() {};
(function() {
// ...
}())
Further Reading
Compatibility
- JSCS: requireAnonymousFunctions
- JSCS: disallowAnonymousFunctions Source: http://eslint.org/docs/rules/
Comments should not begin with a lowercase character Open
// overwrite element.animate
- Read upRead up
- Exclude checks
enforce or disallow capitalization of the first letter of a comment (capitalized-comments)
Comments are useful for leaving information for future developers. In order for that information to be useful and not distracting, it is sometimes desirable for comments to follow a particular style. One element of comment formatting styles is whether the first word of a comment should be capitalized or lowercase.
In general, no comment style is any more or less valid than any others, but many developers would agree that a consistent style can improve a project's maintainability.
Rule Details
This rule aims to enforce a consistent style of comments across your codebase, specifically by either requiring or disallowing a capitalized letter as the first word character in a comment. This rule will not issue warnings when non-cased letters are used.
By default, this rule will require a non-lowercase letter at the beginning of comments.
Examples of incorrect code for this rule:
/* eslint capitalized-comments: ["error"] */
// lowercase comment
Examples of correct code for this rule:
// Capitalized comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
/* eslint semi:off */
/* eslint-env node */
/* eslint-disable */
/* eslint-enable */
/* istanbul ignore next */
/* jscs:enable */
/* jshint asi:true */
/* global foo */
/* globals foo */
/* exported myVar */
// eslint-disable-line
// eslint-disable-next-line
// https://github.com
Options
This rule has two options: a string value "always"
or "never"
which determines whether capitalization of the first word of a comment should be required or forbidden, and optionally an object containing more configuration parameters for the rule.
Here are the supported object options:
-
ignorePattern
: A string representing a regular expression pattern of words that should be ignored by this rule. If the first word of a comment matches the pattern, this rule will not report that comment.- Note that the following words are always ignored by this rule:
["jscs", "jshint", "eslint", "istanbul", "global", "globals", "exported"]
.
- Note that the following words are always ignored by this rule:
-
ignoreInlineComments
: If this istrue
, the rule will not report on comments in the middle of code. By default, this isfalse
. -
ignoreConsecutiveComments
: If this istrue
, the rule will not report on a comment which violates the rule, as long as the comment immediately follows another comment. By default, this isfalse
.
Here is an example configuration:
{
"capitalized-comments": [
"error",
"always",
{
"ignorePattern": "pragma|ignored",
"ignoreInlineComments": true
}
]
}
"always"
Using the "always"
option means that this rule will report any comments which start with a lowercase letter. This is the default configuration for this rule.
Note that configuration comments and comments which start with URLs are never reported.
Examples of incorrect code for this rule:
/* eslint capitalized-comments: ["error", "always"] */
// lowercase comment
Examples of correct code for this rule:
/* eslint capitalized-comments: ["error", "always"] */
// Capitalized comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
/* eslint semi:off */
/* eslint-env node */
/* eslint-disable */
/* eslint-enable */
/* istanbul ignore next */
/* jscs:enable */
/* jshint asi:true */
/* global foo */
/* globals foo */
/* exported myVar */
// eslint-disable-line
// eslint-disable-next-line
// https://github.com
"never"
Using the "never"
option means that this rule will report any comments which start with an uppercase letter.
Examples of incorrect code with the "never"
option:
/* eslint capitalized-comments: ["error", "never"] */
// Capitalized comment
Examples of correct code with the "never"
option:
/* eslint capitalized-comments: ["error", "never"] */
// lowercase comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
ignorePattern
The ignorePattern
object takes a string value, which is used as a regular expression applied to the first word of a comment.
Examples of correct code with the "ignorePattern"
option set to "pragma"
:
/* eslint capitalized-comments: ["error", "always", { "ignorePattern": "pragma" }] */
function foo() {
/* pragma wrap(true) */
}
ignoreInlineComments
Setting the ignoreInlineComments
option to true
means that comments in the middle of code (with a token on the same line as the beginning of the comment, and another token on the same line as the end of the comment) will not be reported by this rule.
Examples of correct code with the "ignoreInlineComments"
option set to true
:
/* eslint capitalized-comments: ["error", "always", { "ignoreInlineComments": true }] */
function foo(/* ignored */ a) {
}
ignoreConsecutiveComments
If the ignoreConsecutiveComments
option is set to true
, then comments which otherwise violate the rule will not be reported as long as they immediately follow another comment. This can be applied more than once.
Examples of correct code with ignoreConsecutiveComments
set to true
:
/* eslint capitalize-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
// This comment is valid since it has the correct capitalization.
// this comment is ignored since it follows another comment,
// and this one as well because it follows yet another comment.
/* Here is a block comment which has the correct capitalization, */
/* but this one is ignored due to being consecutive; */
/*
* in fact, even if any of these are multi-line, that is fine too.
*/
Examples of incorrect code with ignoreConsecutiveComments
set to true
:
/* eslint capitalize-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
// this comment is invalid, but only on this line.
// this comment does NOT get reported, since it is a consecutive comment.
Using Different Options for Line and Block Comments
If you wish to have a different configuration for line comments and block comments, you can do so by using two different object configurations (note that the capitalization option will be enforced consistently for line and block comments):
{
"capitalized-comments": [
"error",
"always",
{
"line": {
"ignorePattern": "pragma|ignored",
},
"block": {
"ignoreInlineComments": true,
"ignorePattern": "ignored"
}
}
]
}
Examples of incorrect code with different line and block comment configuration:
/* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */
// capitalized line comment, this is incorrect, blockignore does not help here
/* lowercased block comment, this is incorrect too */
Examples of correct code with different line and block comment configuration:
/* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */
// Uppercase line comment, this is correct
/* blockignore lowercase block comment, this is correct due to ignorePattern */
When Not To Use It
This rule can be disabled if you do not care about the grammatical style of comments in your codebase.
Compatibility
Expected indentation of 2 tabs but found 3. Open
}).join(' | ')}</div>
- Read upRead up
- Exclude checks
enforce consistent indentation (indent)
There are several common guidelines which require specific indentation of nested blocks and statements, like:
function hello(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;
function foo(d) {
e=f;
}
}
Examples of correct code for this rule with the default options:
/*eslint indent: "error"*/
if (a) {
b=c;
function foo(d) {
e=f;
}
}
This rule has an object option:
-
"SwitchCase"
(default: 0) enforces indentation level forcase
clauses inswitch
statements -
"VariableDeclarator"
(default: 1) enforces indentation level forvar
declarators; can also take an object to define separate rules forvar
,let
andconst
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 to2
will indent the multi-line variable declarations with 8 spaces. - Indent of 2 spaces with
VariableDeclarator
set to2
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 forvar
andlet
, 6 spaces forconst
statements. - Indent of tab with
VariableDeclarator
set to2
will indent the multi-line variable declarations with 2 tabs. - Indent of 2 spaces with
SwitchCase
set to0
will not indentcase
clauses with respect toswitch
statements. - Indent of 2 spaces with
SwitchCase
set to1
will indentcase
clauses with 2 spaces with respect toswitch
statements. - Indent of 2 spaces with
SwitchCase
set to2
will indentcase
clauses with 4 spaces with respect toswitch
statements. - Indent of tab with
SwitchCase
set to2
will indentcase
clauses with 2 tabs with respect toswitch
statements. - Indent of 2 spaces with
MemberExpression
set to0
will indent the multi-line property chains with 0 spaces. - Indent of 2 spaces with
MemberExpression
set to1
will indent the multi-line property chains with 2 spaces. - Indent of 2 spaces with
MemberExpression
set to2
will indent the multi-line property chains with 4 spaces. - Indent of 4 spaces with
MemberExpression
set to0
will indent the multi-line property chains with 0 spaces. - Indent of 4 spaces with
MemberExpression
set to1
will indent the multi-line property chains with 4 spaces. - Indent of 4 spaces with
MemberExpression
set to2
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;
function foo(d) {
e=f;
}
}
Examples of correct code for this rule with the "tab"
option:
/*eslint indent: ["error", "tab"]*/
if (a) {
/*tab*/b=c;
/*tab*/function foo(d) {
/*tab*//*tab*/e=f;
/*tab*/}
}
SwitchCase
Examples of incorrect code for this rule with the 2, { "SwitchCase": 1 }
options:
/*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
switch(a){
case "a":
break;
case "b":
break;
}
Examples of correct code for this rule with the 2, { "SwitchCase": 1 }
option:
/*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
switch(a){
case "a":
break;
case "b":
break;
}
VariableDeclarator
Examples of incorrect code for this rule with the 2, { "VariableDeclarator": 1 }
options:
/*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
/*eslint-env es6*/
var a,
b,
c;
let a,
b,
c;
const a = 1,
b = 2,
c = 3;
Examples of correct code for this rule with the 2, { "VariableDeclarator": 1 }
options:
/*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
/*eslint-env es6*/
var a,
b,
c;
let a,
b,
c;
const a = 1,
b = 2,
c = 3;
Examples of correct code for this rule with the 2, { "VariableDeclarator": 2 }
options:
/*eslint indent: ["error", 2, { "VariableDeclarator": 2 }]*/
/*eslint-env es6*/
var a,
b,
c;
let a,
b,
c;
const a = 1,
b = 2,
c = 3;
Examples of correct code for this rule with the 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }
options:
/*eslint indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/
/*eslint-env es6*/
var a,
b,
c;
let a,
b,
c;
const a = 1,
b = 2,
c = 3;
outerIIFEBody
Examples of incorrect code for this rule with the options 2, { "outerIIFEBody": 0 }
:
/*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
(function() {
function foo(x) {
return x + 1;
}
})();
if(y) {
console.log('foo');
}
Examples of correct code for this rule with the options 2, {"outerIIFEBody": 0}
:
/*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
(function() {
function foo(x) {
return x + 1;
}
})();
if(y) {
console.log('foo');
}
MemberExpression
Examples of incorrect code for this rule with the 2, { "MemberExpression": 1 }
options:
/*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
foo
.bar
.baz()
Examples of correct code for this rule with the 2, { "MemberExpression": 1 }
option:
/*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
foo
.bar
.baz();
// Any indentation is permitted in variable declarations and assignments.
var bip = aardvark.badger
.coyote;
FunctionDeclaration
Examples of incorrect code for this rule with the 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }
option:
/*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
function foo(bar,
baz,
qux) {
qux();
}
Examples of correct code for this rule with the 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }
option:
/*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
function foo(bar,
baz,
qux) {
qux();
}
Examples of incorrect code for this rule with the 2, { "FunctionDeclaration": {"parameters": "first"} }
option:
/*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
function foo(bar, baz,
qux, boop) {
qux();
}
Examples of correct code for this rule with the 2, { "FunctionDeclaration": {"parameters": "first"} }
option:
/*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
function foo(bar, baz,
qux, boop) {
qux();
}
FunctionExpression
Examples of incorrect code for this rule with the 2, { "FunctionExpression": {"body": 1, "parameters": 2} }
option:
/*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
var foo = function(bar,
baz,
qux) {
qux();
}
Examples of correct code for this rule with the 2, { "FunctionExpression": {"body": 1, "parameters": 2} }
option:
/*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
var foo = function(bar,
baz,
qux) {
qux();
}
Examples of incorrect code for this rule with the 2, { "FunctionExpression": {"parameters": "first"} }
option:
/*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
var foo = function(bar, baz,
qux, boop) {
qux();
}
Examples of correct code for this rule with the 2, { "FunctionExpression": {"parameters": "first"} }
option:
/*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
var foo = function(bar, baz,
qux, boop) {
qux();
}
CallExpression
Examples of incorrect code for this rule with the 2, { "CallExpression": {"arguments": 1} }
option:
/*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
foo(bar,
baz,
qux
);
Examples of correct code for this rule with the 2, { "CallExpression": {"arguments": 1} }
option:
/*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
foo(bar,
baz,
qux
);
Examples of incorrect code for this rule with the 2, { "CallExpression": {"arguments": "first"} }
option:
/*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
foo(bar, baz,
baz, boop, beep);
Examples of correct code for this rule with the 2, { "CallExpression": {"arguments": "first"} }
option:
/*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
foo(bar, baz,
baz, boop, beep);
ArrayExpression
Examples of incorrect code for this rule with the 2, { "ArrayExpression": 1 }
option:
/*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
var foo = [
bar,
baz,
qux
];
Examples of correct code for this rule with the 2, { "ArrayExpression": 1 }
option:
/*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
var foo = [
bar,
baz,
qux
];
Examples of incorrect code for this rule with the 2, { "ArrayExpression": "first" }
option:
/*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
var foo = [bar,
baz,
qux
];
Examples of correct code for this rule with the 2, { "ArrayExpression": "first" }
option:
/*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
var foo = [bar,
baz,
qux
];
ObjectExpression
Examples of incorrect code for this rule with the 2, { "ObjectExpression": 1 }
option:
/*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
var foo = {
bar: 1,
baz: 2,
qux: 3
};
Examples of correct code for this rule with the 2, { "ObjectExpression": 1 }
option:
/*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
var foo = {
bar: 1,
baz: 2,
qux: 3
};
Examples of incorrect code for this rule with the 2, { "ObjectExpression": "first" }
option:
/*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
var foo = { bar: 1,
baz: 2 };
Examples of correct code for this rule with the 2, { "ObjectExpression": "first" }
option:
/*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
var foo = { bar: 1,
baz: 2 };
Compatibility
-
JSHint:
indent
- JSCS: validateIndentation Source: http://eslint.org/docs/rules/
Unexpected var, use let or const instead. Open
var babel = require('gulp-babel');
- Read upRead up
- Exclude checks
require let
or const
instead of var
(no-var)
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";
var CONFIG = {};
Examples of correct code for this rule:
/*eslint no-var: "error"*/
/*eslint-env es6*/
let x = "y";
const CONFIG = {};
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/
Unexpected var, use let or const instead. Open
var testCss = require('./test-css')(gulp, paths, watchOptions, cli);
- Read upRead up
- Exclude checks
require let
or const
instead of var
(no-var)
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";
var CONFIG = {};
Examples of correct code for this rule:
/*eslint no-var: "error"*/
/*eslint-env es6*/
let x = "y";
const CONFIG = {};
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/
Unexpected negated condition. Open
} else if (!isNoPullRequest()) {
- Read upRead up
- Exclude checks
disallow negated conditions (no-negated-condition)
Negated conditions are more difficult to understand. Code can be made more readable by inverting the condition instead.
Rule Details
This rule disallows negated conditions in either of the following:
-
if
statements which have anelse
branch - ternary expressions
Examples of incorrect code for this rule:
/*eslint no-negated-condition: "error"*/
if (!a) {
doSomething();
} else {
doSomethingElse();
}
if (a != b) {
doSomething();
} else {
doSomethingElse();
}
if (a !== b) {
doSomething();
} else {
doSomethingElse();
}
!a ? c : b
Examples of correct code for this rule:
/*eslint no-negated-condition: "error"*/
if (!a) {
doSomething();
}
if (!a) {
doSomething();
} else if (b) {
doSomething();
}
if (a != b) {
doSomething();
}
a ? b : c
Source: http://eslint.org/docs/rules/
Unexpected parentheses around single function argument. Open
const intersection = searchedFiles.filter((searchedFile) => changedFiles.indexOf(searchedFile) > -1);
- Read upRead up
- Exclude checks
Require parens in arrow function arguments (arrow-parens)
Arrow functions can omit parentheses when they have exactly one parameter. In all other cases the parameter(s) must be wrapped in parentheses. This rule enforces the consistent use of parentheses in arrow functions.
Rule Details
This rule enforces parentheses around arrow function parameters regardless of arity. For example:
/*eslint-env es6*/
// Bad
a => {}
// Good
(a) => {}
Following this style will help you find arrow functions (=>
) which may be mistakenly included in a condition
when a comparison such as >=
was the intent.
/*eslint-env es6*/
// Bad
if (a => 2) {
}
// Good
if (a >= 2) {
}
The rule can also be configured to discourage the use of parens when they are not required:
/*eslint-env es6*/
// Bad
(a) => {}
// Good
a => {}
Options
This rule has a string option and an object one.
String options are:
-
"always"
(default) requires parens around arguments in all cases. -
"as-needed"
allows omitting parens when there is only one argument.
Object properties for variants of the "as-needed"
option:
-
"requireForBlockBody": true
modifies the as-needed rule in order to require parens if the function body is in an instructions block (surrounded by braces).
always
Examples of incorrect code for this rule with the default "always"
option:
/*eslint arrow-parens: ["error", "always"]*/
/*eslint-env es6*/
a => {};
a => a;
a => {'\n'};
a.then(foo => {});
a.then(foo => a);
a(foo => { if (true) {} });
Examples of correct code for this rule with the default "always"
option:
/*eslint arrow-parens: ["error", "always"]*/
/*eslint-env es6*/
() => {};
(a) => {};
(a) => a;
(a) => {'\n'}
a.then((foo) => {});
a.then((foo) => { if (true) {} });
If Statements
One of benefits of this option is that it prevents the incorrect use of arrow functions in conditionals:
/*eslint-env es6*/
var a = 1;
var b = 2;
// ...
if (a => b) {
console.log('bigger');
} else {
console.log('smaller');
}
// outputs 'bigger', not smaller as expected
The contents of the if
statement is an arrow function, not a comparison.
If the arrow function is intentional, it should be wrapped in parens to remove ambiguity.
/*eslint-env es6*/
var a = 1;
var b = 0;
// ...
if ((a) => b) {
console.log('truthy value returned');
} else {
console.log('falsey value returned');
}
// outputs 'truthy value returned'
The following is another example of this behavior:
/*eslint-env es6*/
var a = 1, b = 2, c = 3, d = 4;
var f = a => b ? c: d;
// f = ?
f
is an arrow function which takes a
as an argument and returns the result of b ? c: d
.
This should be rewritten like so:
/*eslint-env es6*/
var a = 1, b = 2, c = 3, d = 4;
var f = (a) => b ? c: d;
as-needed
Examples of incorrect code for this rule with the "as-needed"
option:
/*eslint arrow-parens: ["error", "as-needed"]*/
/*eslint-env es6*/
(a) => {};
(a) => a;
(a) => {'\n'};
a.then((foo) => {});
a.then((foo) => a);
a((foo) => { if (true) {} });
Examples of correct code for this rule with the "as-needed"
option:
/*eslint arrow-parens: ["error", "as-needed"]*/
/*eslint-env es6*/
() => {};
a => {};
a => a;
a => {'\n'};
a.then(foo => {});
a.then(foo => { if (true) {} });
(a, b, c) => a;
(a = 10) => a;
([a, b]) => a;
({a, b}) => a;
requireForBlockBody
Examples of incorrect code for the { "requireForBlockBody": true }
option:
/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/
/*eslint-env es6*/
(a) => a;
a => {};
a => {'\n'};
a.map((x) => x * x);
a.map(x => {
return x * x;
});
a.then(foo => {});
Examples of correct code for the { "requireForBlockBody": true }
option:
/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/
/*eslint-env es6*/
(a) => {};
(a) => {'\n'};
a => ({});
() => {};
a => a;
a.then((foo) => {});
a.then((foo) => { if (true) {} });
a((foo) => { if (true) {} });
(a, b, c) => a;
(a = 10) => a;
([a, b]) => a;
({a, b}) => a;
Further Reading
- The
"as-needed", { "requireForBlockBody": true }
rule is directly inspired by the Airbnb JS Style Guide. Source: http://eslint.org/docs/rules/
Comments should not begin with a lowercase character Open
// abort window.requestAnimationFrame
- Read upRead up
- Exclude checks
enforce or disallow capitalization of the first letter of a comment (capitalized-comments)
Comments are useful for leaving information for future developers. In order for that information to be useful and not distracting, it is sometimes desirable for comments to follow a particular style. One element of comment formatting styles is whether the first word of a comment should be capitalized or lowercase.
In general, no comment style is any more or less valid than any others, but many developers would agree that a consistent style can improve a project's maintainability.
Rule Details
This rule aims to enforce a consistent style of comments across your codebase, specifically by either requiring or disallowing a capitalized letter as the first word character in a comment. This rule will not issue warnings when non-cased letters are used.
By default, this rule will require a non-lowercase letter at the beginning of comments.
Examples of incorrect code for this rule:
/* eslint capitalized-comments: ["error"] */
// lowercase comment
Examples of correct code for this rule:
// Capitalized comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
/* eslint semi:off */
/* eslint-env node */
/* eslint-disable */
/* eslint-enable */
/* istanbul ignore next */
/* jscs:enable */
/* jshint asi:true */
/* global foo */
/* globals foo */
/* exported myVar */
// eslint-disable-line
// eslint-disable-next-line
// https://github.com
Options
This rule has two options: a string value "always"
or "never"
which determines whether capitalization of the first word of a comment should be required or forbidden, and optionally an object containing more configuration parameters for the rule.
Here are the supported object options:
-
ignorePattern
: A string representing a regular expression pattern of words that should be ignored by this rule. If the first word of a comment matches the pattern, this rule will not report that comment.- Note that the following words are always ignored by this rule:
["jscs", "jshint", "eslint", "istanbul", "global", "globals", "exported"]
.
- Note that the following words are always ignored by this rule:
-
ignoreInlineComments
: If this istrue
, the rule will not report on comments in the middle of code. By default, this isfalse
. -
ignoreConsecutiveComments
: If this istrue
, the rule will not report on a comment which violates the rule, as long as the comment immediately follows another comment. By default, this isfalse
.
Here is an example configuration:
{
"capitalized-comments": [
"error",
"always",
{
"ignorePattern": "pragma|ignored",
"ignoreInlineComments": true
}
]
}
"always"
Using the "always"
option means that this rule will report any comments which start with a lowercase letter. This is the default configuration for this rule.
Note that configuration comments and comments which start with URLs are never reported.
Examples of incorrect code for this rule:
/* eslint capitalized-comments: ["error", "always"] */
// lowercase comment
Examples of correct code for this rule:
/* eslint capitalized-comments: ["error", "always"] */
// Capitalized comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
/* eslint semi:off */
/* eslint-env node */
/* eslint-disable */
/* eslint-enable */
/* istanbul ignore next */
/* jscs:enable */
/* jshint asi:true */
/* global foo */
/* globals foo */
/* exported myVar */
// eslint-disable-line
// eslint-disable-next-line
// https://github.com
"never"
Using the "never"
option means that this rule will report any comments which start with an uppercase letter.
Examples of incorrect code with the "never"
option:
/* eslint capitalized-comments: ["error", "never"] */
// Capitalized comment
Examples of correct code with the "never"
option:
/* eslint capitalized-comments: ["error", "never"] */
// lowercase comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
ignorePattern
The ignorePattern
object takes a string value, which is used as a regular expression applied to the first word of a comment.
Examples of correct code with the "ignorePattern"
option set to "pragma"
:
/* eslint capitalized-comments: ["error", "always", { "ignorePattern": "pragma" }] */
function foo() {
/* pragma wrap(true) */
}
ignoreInlineComments
Setting the ignoreInlineComments
option to true
means that comments in the middle of code (with a token on the same line as the beginning of the comment, and another token on the same line as the end of the comment) will not be reported by this rule.
Examples of correct code with the "ignoreInlineComments"
option set to true
:
/* eslint capitalized-comments: ["error", "always", { "ignoreInlineComments": true }] */
function foo(/* ignored */ a) {
}
ignoreConsecutiveComments
If the ignoreConsecutiveComments
option is set to true
, then comments which otherwise violate the rule will not be reported as long as they immediately follow another comment. This can be applied more than once.
Examples of correct code with ignoreConsecutiveComments
set to true
:
/* eslint capitalize-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
// This comment is valid since it has the correct capitalization.
// this comment is ignored since it follows another comment,
// and this one as well because it follows yet another comment.
/* Here is a block comment which has the correct capitalization, */
/* but this one is ignored due to being consecutive; */
/*
* in fact, even if any of these are multi-line, that is fine too.
*/
Examples of incorrect code with ignoreConsecutiveComments
set to true
:
/* eslint capitalize-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
// this comment is invalid, but only on this line.
// this comment does NOT get reported, since it is a consecutive comment.
Using Different Options for Line and Block Comments
If you wish to have a different configuration for line comments and block comments, you can do so by using two different object configurations (note that the capitalization option will be enforced consistently for line and block comments):
{
"capitalized-comments": [
"error",
"always",
{
"line": {
"ignorePattern": "pragma|ignored",
},
"block": {
"ignoreInlineComments": true,
"ignorePattern": "ignored"
}
}
]
}
Examples of incorrect code with different line and block comment configuration:
/* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */
// capitalized line comment, this is incorrect, blockignore does not help here
/* lowercased block comment, this is incorrect too */
Examples of correct code with different line and block comment configuration:
/* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */
// Uppercase line comment, this is correct
/* blockignore lowercase block comment, this is correct due to ignorePattern */
When Not To Use It
This rule can be disabled if you do not care about the grammatical style of comments in your codebase.