Showing 270 of 270 total issues
Arrow function should not return assignment. Open
elements.map(element => element.style[prefix('animationName', window, document)] = '');
- Read upRead up
- Exclude checks
Disallow Assignment in return Statement (no-return-assign)
One of the interesting, and sometimes confusing, aspects of JavaScript is that assignment can happen at almost any point. Because of this, an errant equals sign can end up causing assignment when the true intent was to do a comparison. This is especially true when using a return
statement. For example:
function doSomething() {
return foo = bar + 2;
}
It is difficult to tell the intent of the return
statement here. It's possible that the function is meant to return the result of bar + 2
, but then why is it assigning to foo
? It's also possible that the intent was to use a comparison operator such as ==
and that this code is an error.
Because of this ambiguity, it's considered a best practice to not use assignment in return
statements.
Rule Details
This rule aims to eliminate assignments from return
statements. As such, it will warn whenever an assignment is found as part of return
.
Options
The rule takes one option, a string, which must contain one of the following values:
-
except-parens
(default): Disallow assignments unless they are enclosed in parentheses. -
always
: Disallow all assignments.
except-parens
This is the default option. It disallows assignments unless they are enclosed in parentheses.
Examples of incorrect code for the default "except-parens"
option:
/*eslint no-return-assign: "error"*/
function doSomething() {
return foo = bar + 2;
}
function doSomething() {
return foo += 2;
}
Examples of correct code for the default "except-parens"
option:
/*eslint no-return-assign: "error"*/
function doSomething() {
return foo == bar + 2;
}
function doSomething() {
return foo === bar + 2;
}
function doSomething() {
return (foo = bar + 2);
}
always
This option disallows all assignments in return
statements.
All assignments are treated as problems.
Examples of incorrect code for the "always"
option:
/*eslint no-return-assign: ["error", "always"]*/
function doSomething() {
return foo = bar + 2;
}
function doSomething() {
return foo += 2;
}
function doSomething() {
return (foo = bar + 2);
}
Examples of correct code for the "always"
option:
/*eslint no-return-assign: ["error", "always"]*/
function doSomething() {
return foo == bar + 2;
}
function doSomething() {
return foo === bar + 2;
}
When Not To Use It
If you want to allow the use of assignment operators in a return
statement, then you can safely disable this rule.
Source: http://eslint.org/docs/rules/
Empty block statement. Open
if (process.env.NODE_ENV !== 'production') {
- Read upRead up
- Exclude checks
disallow empty block statements (no-empty)
Empty block statements, while not technically errors, usually occur due to refactoring that wasn't completed. They can cause confusion when reading code.
Rule Details
This rule disallows empty block statements. This rule ignores block statements which contain a comment (for example, in an empty catch
or finally
block of a try
statement to indicate that execution should continue regardless of errors).
Examples of incorrect code for this rule:
/*eslint no-empty: "error"*/
if (foo) {
}
while (foo) {
}
switch(foo) {
}
try {
doSomething();
} catch(ex) {
} finally {
}
Examples of correct code for this rule:
/*eslint no-empty: "error"*/
if (foo) {
// empty
}
while (foo) {
/* empty */
}
try {
doSomething();
} catch (ex) {
// continue regardless of error
}
try {
doSomething();
} finally {
/* continue regardless of error */
}
Options
This rule has an object option for exceptions:
-
"allowEmptyCatch": true
allows emptycatch
clauses (that is, which do not contain a comment)
allowEmptyCatch
Examples of additional correct code for this rule with the { "allowEmptyCatch": true }
option:
/* eslint no-empty: ["error", { "allowEmptyCatch": true }] */
try {
doSomething();
} catch (ex) {}
try {
doSomething();
}
catch (ex) {}
finally {
/* continue regardless of error */
}
When Not To Use It
If you intentionally use empty block statements then you can disable this rule.
Related Rules
- [no-empty-function](./no-empty-function.md) Source: http://eslint.org/docs/rules/
Comments should not begin with a lowercase character Open
/**
- 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 mix of '||' and '&&'. Open
if (intersection.length > 0 || intersection.length === 0 && options.changed === true) {
- Read upRead up
- Exclude checks
Disallow mixes of different operators (no-mixed-operators)
Enclosing complex expressions by parentheses clarifies the developer's intention, which makes the code more readable. This rule warns when different operators are used consecutively without parentheses in an expression.
var foo = a && b || c || d; /*BAD: Unexpected mix of '&&' and '||'.*/
var foo = (a && b) || c || d; /*GOOD*/
var foo = a && (b || c || d); /*GOOD*/
Rule Details
This rule checks BinaryExpression
and LogicalExpression
.
This rule may conflict with [no-extra-parens](no-extra-parens.md) rule.
If you use both this and [no-extra-parens](no-extra-parens.md) rule together, you need to use the nestedBinaryExpressions
option of [no-extra-parens](no-extra-parens.md) rule.
Examples of incorrect code for this rule:
/*eslint no-mixed-operators: "error"*/
var foo = a && b < 0 || c > 0 || d + 1 === 0;
var foo = a + b * c;
Examples of correct code for this rule:
/*eslint no-mixed-operators: "error"*/
var foo = a || b || c;
var foo = a && b && c;
var foo = (a && b < 0) || c > 0 || d + 1 === 0;
var foo = a && (b < 0 || c > 0 || d + 1 === 0);
var foo = a + (b * c);
var foo = (a + b) * c;
Options
{
"no-mixed-operators": [
"error",
{
"groups": [
["+", "-", "*", "/", "%", "**"],
["&", "|", "^", "~", "<<", ">>", ">>>"],
["==", "!=", "===", "!==", ">", ">=", "<", "<="],
["&&", "||"],
["in", "instanceof"]
],
"allowSamePrecedence": true
}
]
}
This rule has 2 options.
-
groups
(string[][]
) - specifies groups to compare operators. When this rule compares two operators, if both operators are included in a same group, this rule checks it. Otherwise, this rule ignores it. This value is a list of groups. The group is a list of binary operators. Default is the groups for each kind of operators. -
allowSamePrecedence
(boolean
) - specifies to allow mix of 2 operators if those have the same precedence. Default istrue
.
groups
The following operators can be used in groups
option:
- Arithmetic Operators:
"+"
,"-"
,"*"
,"/"
,"%"
,"**"
- Bitwise Operators:
"&"
,"|"
,"^"
,"~"
,"<<"
,">>"
,">>>"
- Comparison Operators:
"=="
,"!="
,"==="
,"!=="
,">"
,">="
,"<"
,"<="
- Logical Operators:
"&&"
,"||"
- Relational Operators:
"in"
,"instanceof"
Now, considers about {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}
configure.
This configure has 2 groups: bitwise operators and logical operators.
This rule checks only if both operators are included in a same group.
So, in this case, this rule comes to check between bitwise operators and between logical operators.
This rule ignores other operators.
Examples of incorrect code for this rule with {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}
option:
/*eslint no-mixed-operators: ["error", {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}]*/
var foo = a && b < 0 || c > 0 || d + 1 === 0;
var foo = a & b | c;
Examples of correct code for this rule with {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}
option:
/*eslint no-mixed-operators: ["error", {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}]*/
var foo = a || b > 0 || c + 1 === 0;
var foo = a && b > 0 && c + 1 === 0;
var foo = (a && b < 0) || c > 0 || d + 1 === 0;
var foo = a && (b < 0 || c > 0 || d + 1 === 0);
var foo = (a & b) | c;
var foo = a & (b | c);
var foo = a + b * c;
var foo = a + (b * c);
var foo = (a + b) * c;
allowSamePrecedence
Examples of correct code for this rule with {"allowSamePrecedence": true}
option:
/*eslint no-mixed-operators: ["error", {"allowSamePrecedence": true}]*/
// + and - have the same precedence.
var foo = a + b - c;
Examples of incorrect code for this rule with {"allowSamePrecedence": false}
option:
/*eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}]*/
// + and - have the same precedence.
var foo = a + b - c;
When Not To Use It
If you don't want to be notified about mixed operators, then it's safe to disable this rule.
Related Rules
- [no-extra-parens](no-extra-parens.md) Source: http://eslint.org/docs/rules/
Unnecessarily quoted property 'length' found. Open
'length': 3
- Read upRead up
- Exclude checks
require quotes around object literal property names (quote-props)
Object literal property names can be defined in two ways: using literals or using strings. For example, these two objects are equivalent:
var object1 = {
property: true
};
var object2 = {
"property": true
};
In many cases, it doesn't matter if you choose to use an identifier instead of a string or vice-versa. Even so, you might decide to enforce a consistent style in your code.
There are, however, some occasions when you must use quotes:
- If you are using an ECMAScript 3 JavaScript engine (such as IE8) and you want to use a keyword (such as
if
) as a property name. This restriction was removed in ECMAScript 5. - You want to use a non-identifier character in your property name, such as having a property with a space like
"one two"
.
Another example where quotes do matter is when using numeric literals as property keys:
var object = {
1e2: 1,
100: 2
};
This may look alright at first sight, but this code in fact throws a syntax error in ECMAScript 5 strict mode. This happens because 1e2
and 100
are coerced into strings before getting used as the property name. Both String(1e2)
and String(100)
happen to be equal to "100"
, which causes the "Duplicate data property in object literal not allowed in strict mode" error. Issues like that can be tricky to debug, so some prefer to require quotes around all property names.
Rule Details
This rule requires quotes around object literal property names.
Options
This rule has two options, a string option and an object option.
String option:
-
"always"
(default) requires quotes around all object literal property names -
"as-needed"
disallows quotes around object literal property names that are not strictly required -
"consistent"
enforces a consistent quote style requires quotes around object literal property names -
"consistent-as-needed"
requires quotes around all object literal property names if any name strictly requires quotes, otherwise disallows quotes around object property names
Object option:
-
"keywords": true
requires quotes around language keywords used as object property names (only applies when usingas-needed
orconsistent-as-needed
) -
"unnecessary": true
(default) disallows quotes around object literal property names that are not strictly required (only applies when usingas-needed
) -
"unnecessary": false
allows quotes around object literal property names that are not strictly required (only applies when usingas-needed
) -
"numbers": true
requires quotes around numbers used as object property names (only applies when usingas-needed
)
always
Examples of incorrect code for this rule with the default "always"
option:
/*eslint quote-props: ["error", "always"]*/
var object = {
foo: "bar",
baz: 42,
"qux-lorem": true
};
Examples of correct code for this rule with the default "always"
option:
/*eslint quote-props: ["error", "always"]*/
/*eslint-env es6*/
var object1 = {
"foo": "bar",
"baz": 42,
"qux-lorem": true
};
var object2 = {
'foo': 'bar',
'baz': 42,
'qux-lorem': true
};
var object3 = {
foo() {
return;
}
};
as-needed
Examples of incorrect code for this rule with the "as-needed"
option:
/*eslint quote-props: ["error", "as-needed"]*/
var object = {
"a": 0,
"0": 0,
"true": 0,
"null": 0
};
Examples of correct code for this rule with the "as-needed"
option:
/*eslint quote-props: ["error", "as-needed"]*/
/*eslint-env es6*/
var object1 = {
"a-b": 0,
"0x0": 0,
"1e2": 0
};
var object2 = {
foo: 'bar',
baz: 42,
true: 0,
0: 0,
'qux-lorem': true
};
var object3 = {
foo() {
return;
}
};
consistent
Examples of incorrect code for this rule with the "consistent"
option:
/*eslint quote-props: ["error", "consistent"]*/
var object1 = {
foo: "bar",
"baz": 42,
"qux-lorem": true
};
var object2 = {
'foo': 'bar',
baz: 42
};
Examples of correct code for this rule with the "consistent"
option:
/*eslint quote-props: ["error", "consistent"]*/
var object1 = {
"foo": "bar",
"baz": 42,
"qux-lorem": true
};
var object2 = {
'foo': 'bar',
'baz': 42
};
var object3 = {
foo: 'bar',
baz: 42
};
consistent-as-needed
Examples of incorrect code for this rule with the "consistent-as-needed"
option:
/*eslint quote-props: ["error", "consistent-as-needed"]*/
var object1 = {
foo: "bar",
"baz": 42,
"qux-lorem": true
};
var object2 = {
'foo': 'bar',
'baz': 42
};
Examples of correct code for this rule with the "consistent-as-needed"
option:
/*eslint quote-props: ["error", "consistent-as-needed"]*/
var object1 = {
"foo": "bar",
"baz": 42,
"qux-lorem": true
};
var object2 = {
foo: 'bar',
baz: 42
};
keywords
Examples of additional incorrect code for this rule with the "as-needed", { "keywords": true }
options:
/*eslint quote-props: ["error", "as-needed", { "keywords": true }]*/
var x = {
while: 1,
volatile: "foo"
};
Examples of additional incorrect code for this rule with the "consistent-as-needed", { "keywords": true }
options:
/*eslint quote-props: ["error", "consistent-as-needed", { "keywords": true }]*/
var x = {
"prop": 1,
"bar": "foo"
};
unnecessary
Examples of additional correct code for this rule with the "as-needed", { "unnecessary": false }
options:
/*eslint quote-props: ["error", "as-needed", { "keywords": true, "unnecessary": false }]*/
var x = {
"while": 1,
"foo": "bar" // Would normally have caused a warning
};
numbers
Examples of additional incorrect code for this rule with the "as-needed", { "numbers": true }
options:
/*eslint quote-props: ["error", "as-needed", { "numbers": true }]*/
var x = {
100: 1
}
When Not To Use It
If you don't care if property names are consistently wrapped in quotes or not, and you don't target legacy ES3 environments, turn this rule off.
Further Reading
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
Unexpected var, use let or const instead. Open
var name = 'transpile:' + [source, target].join(' → ');
- 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/
Comments should not begin with a lowercase character Open
// unwrap cssRules
- 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
Unnecessarily quoted property 'width' found. Open
'width': '0',
- Read upRead up
- Exclude checks
require quotes around object literal property names (quote-props)
Object literal property names can be defined in two ways: using literals or using strings. For example, these two objects are equivalent:
var object1 = {
property: true
};
var object2 = {
"property": true
};
In many cases, it doesn't matter if you choose to use an identifier instead of a string or vice-versa. Even so, you might decide to enforce a consistent style in your code.
There are, however, some occasions when you must use quotes:
- If you are using an ECMAScript 3 JavaScript engine (such as IE8) and you want to use a keyword (such as
if
) as a property name. This restriction was removed in ECMAScript 5. - You want to use a non-identifier character in your property name, such as having a property with a space like
"one two"
.
Another example where quotes do matter is when using numeric literals as property keys:
var object = {
1e2: 1,
100: 2
};
This may look alright at first sight, but this code in fact throws a syntax error in ECMAScript 5 strict mode. This happens because 1e2
and 100
are coerced into strings before getting used as the property name. Both String(1e2)
and String(100)
happen to be equal to "100"
, which causes the "Duplicate data property in object literal not allowed in strict mode" error. Issues like that can be tricky to debug, so some prefer to require quotes around all property names.
Rule Details
This rule requires quotes around object literal property names.
Options
This rule has two options, a string option and an object option.
String option:
-
"always"
(default) requires quotes around all object literal property names -
"as-needed"
disallows quotes around object literal property names that are not strictly required -
"consistent"
enforces a consistent quote style requires quotes around object literal property names -
"consistent-as-needed"
requires quotes around all object literal property names if any name strictly requires quotes, otherwise disallows quotes around object property names
Object option:
-
"keywords": true
requires quotes around language keywords used as object property names (only applies when usingas-needed
orconsistent-as-needed
) -
"unnecessary": true
(default) disallows quotes around object literal property names that are not strictly required (only applies when usingas-needed
) -
"unnecessary": false
allows quotes around object literal property names that are not strictly required (only applies when usingas-needed
) -
"numbers": true
requires quotes around numbers used as object property names (only applies when usingas-needed
)
always
Examples of incorrect code for this rule with the default "always"
option:
/*eslint quote-props: ["error", "always"]*/
var object = {
foo: "bar",
baz: 42,
"qux-lorem": true
};
Examples of correct code for this rule with the default "always"
option:
/*eslint quote-props: ["error", "always"]*/
/*eslint-env es6*/
var object1 = {
"foo": "bar",
"baz": 42,
"qux-lorem": true
};
var object2 = {
'foo': 'bar',
'baz': 42,
'qux-lorem': true
};
var object3 = {
foo() {
return;
}
};
as-needed
Examples of incorrect code for this rule with the "as-needed"
option:
/*eslint quote-props: ["error", "as-needed"]*/
var object = {
"a": 0,
"0": 0,
"true": 0,
"null": 0
};
Examples of correct code for this rule with the "as-needed"
option:
/*eslint quote-props: ["error", "as-needed"]*/
/*eslint-env es6*/
var object1 = {
"a-b": 0,
"0x0": 0,
"1e2": 0
};
var object2 = {
foo: 'bar',
baz: 42,
true: 0,
0: 0,
'qux-lorem': true
};
var object3 = {
foo() {
return;
}
};
consistent
Examples of incorrect code for this rule with the "consistent"
option:
/*eslint quote-props: ["error", "consistent"]*/
var object1 = {
foo: "bar",
"baz": 42,
"qux-lorem": true
};
var object2 = {
'foo': 'bar',
baz: 42
};
Examples of correct code for this rule with the "consistent"
option:
/*eslint quote-props: ["error", "consistent"]*/
var object1 = {
"foo": "bar",
"baz": 42,
"qux-lorem": true
};
var object2 = {
'foo': 'bar',
'baz': 42
};
var object3 = {
foo: 'bar',
baz: 42
};
consistent-as-needed
Examples of incorrect code for this rule with the "consistent-as-needed"
option:
/*eslint quote-props: ["error", "consistent-as-needed"]*/
var object1 = {
foo: "bar",
"baz": 42,
"qux-lorem": true
};
var object2 = {
'foo': 'bar',
'baz': 42
};
Examples of correct code for this rule with the "consistent-as-needed"
option:
/*eslint quote-props: ["error", "consistent-as-needed"]*/
var object1 = {
"foo": "bar",
"baz": 42,
"qux-lorem": true
};
var object2 = {
foo: 'bar',
baz: 42
};
keywords
Examples of additional incorrect code for this rule with the "as-needed", { "keywords": true }
options:
/*eslint quote-props: ["error", "as-needed", { "keywords": true }]*/
var x = {
while: 1,
volatile: "foo"
};
Examples of additional incorrect code for this rule with the "consistent-as-needed", { "keywords": true }
options:
/*eslint quote-props: ["error", "consistent-as-needed", { "keywords": true }]*/
var x = {
"prop": 1,
"bar": "foo"
};
unnecessary
Examples of additional correct code for this rule with the "as-needed", { "unnecessary": false }
options:
/*eslint quote-props: ["error", "as-needed", { "keywords": true, "unnecessary": false }]*/
var x = {
"while": 1,
"foo": "bar" // Would normally have caused a warning
};
numbers
Examples of additional incorrect code for this rule with the "as-needed", { "numbers": true }
options:
/*eslint quote-props: ["error", "as-needed", { "numbers": true }]*/
var x = {
100: 1
}
When Not To Use It
If you don't care if property names are consistently wrapped in quotes or not, and you don't target legacy ES3 environments, turn this rule off.
Further Reading
Comments should not begin with a lowercase character Open
// obsolete when switching to phantomjs2 / jsdom / zombie
- 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.