Showing 2,141 of 2,141 total issues
Unexpected dangling '_' in '_events'. Open
var _events = {
- Read upRead up
- Exclude checks
disallow dangling underscores in identifiers (no-underscore-dangle)
As far as naming conventions for identifiers go, dangling underscores may be the most polarizing in JavaScript. Dangling underscores are underscores at either the beginning or end of an identifier, such as:
var _foo;
There is actually a long history of using dangling underscores to indicate "private" members of objects in JavaScript (though JavaScript doesn't have truly private members, this convention served as a warning). This began with SpiderMonkey adding nonstandard methods such as __defineGetter__()
. The intent with the underscores was to make it obvious that this method was special in some way. Since that time, using a single underscore prefix has become popular as a way to indicate "private" members of objects.
Whether or not you choose to allow dangling underscores in identifiers is purely a convention and has no effect on performance, readability, or complexity. It's purely a preference.
Rule Details
This rule disallows dangling underscores in identifiers.
Examples of incorrect code for this rule:
/*eslint no-underscore-dangle: "error"*/
var foo_;
var __proto__ = {};
foo._bar();
Examples of correct code for this rule:
/*eslint no-underscore-dangle: "error"*/
var _ = require('underscore');
var obj = _.contains(items, item);
obj.__proto__ = {};
var file = __filename;
Options
This rule has an object option:
-
"allow"
allows specified identifiers to have dangling underscores -
"allowAfterThis": false
(default) disallows dangling underscores in members of thethis
object -
"allowAfterSuper": false
(default) disallows dangling underscores in members of thesuper
object
allow
Examples of additional correct code for this rule with the { "allow": ["foo_", "_bar"] }
option:
/*eslint no-underscore-dangle: ["error", { "allow": ["foo_", "_bar"] }]*/
var foo_;
foo._bar();
allowAfterThis
Examples of correct code for this rule with the { "allowAfterThis": true }
option:
/*eslint no-underscore-dangle: ["error", { "allowAfterThis": true }]*/
var a = this.foo_;
this._bar();
allowAfterSuper
Examples of correct code for this rule with the { "allowAfterSuper": true }
option:
/*eslint no-underscore-dangle: ["error", { "allowAfterSuper": true }]*/
var a = super.foo_;
super._bar();
When Not To Use It
If you want to allow dangling underscores in identifiers, then you can safely turn this rule off. Source: http://eslint.org/docs/rules/
Unexpected var, use let or const instead. Open
var _closeButton;
- 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 string concatenation. Open
_arrow.style.top = (offsetY + arrowOffset.top) + 'px';
- Read upRead up
- Exclude checks
Suggest using template literals instead of string concatenation. (prefer-template)
In ES2015 (ES6), we can use template literals instead of string concatenation.
var str = "Hello, " + name + "!";
/*eslint-env es6*/
var str = `Hello, ${name}!`;
Rule Details
This rule is aimed to flag usage of +
operators with strings.
Examples
Examples of incorrect code for this rule:
/*eslint prefer-template: "error"*/
var str = "Hello, " + name + "!";
var str = "Time: " + (12 * 60 * 60 * 1000);
Examples of correct code for this rule:
/*eslint prefer-template: "error"*/
/*eslint-env es6*/
var str = "Hello World!";
var str = `Hello, ${name}!`;
var str = `Time: ${12 * 60 * 60 * 1000}`;
// This is reported by `no-useless-concat`.
var str = "Hello, " + "World!";
When Not To Use It
This rule should not be used in ES3/5 environments.
In ES2015 (ES6) or later, if you don't want to be notified about string concatenation, you can safely disable this rule.
Related Rules
- [no-useless-concat](no-useless-concat.md)
- [quotes](quotes.md) Source: http://eslint.org/docs/rules/
Expected { after 'else'. Open
else
- Read upRead up
- Exclude checks
Require Following Curly Brace Conventions (curly)
JavaScript allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to never omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity. So the following:
if (foo) foo++;
Can be rewritten as:
if (foo) {
foo++;
}
There are, however, some who prefer to only use braces when there is more than one statement to be executed.
Rule Details
This rule is aimed at preventing bugs and increasing code clarity by ensuring that block statements are wrapped in curly braces. It will warn when it encounters blocks that omit curly braces.
Options
all
Examples of incorrect code for the default "all"
option:
/*eslint curly: "error"*/
if (foo) foo++;
while (bar)
baz();
if (foo) {
baz();
} else qux();
Examples of correct code for the default "all"
option:
/*eslint curly: "error"*/
if (foo) {
foo++;
}
while (bar) {
baz();
}
if (foo) {
baz();
} else {
qux();
}
multi
By default, this rule warns whenever if
, else
, for
, while
, or do
are used without block statements as their body. However, you can specify that block statements should be used only when there are multiple statements in the block and warn when there is only one statement in the block.
Examples of incorrect code for the "multi"
option:
/*eslint curly: ["error", "multi"]*/
if (foo) {
foo++;
}
if (foo) bar();
else {
foo++;
}
while (true) {
doSomething();
}
for (var i=0; i < items.length; i++) {
doSomething();
}
Examples of correct code for the "multi"
option:
/*eslint curly: ["error", "multi"]*/
if (foo) foo++;
else foo();
while (true) {
doSomething();
doSomethingElse();
}
multi-line
Alternatively, you can relax the rule to allow brace-less single-line if
, else if
, else
, for
, while
, or do
, while still enforcing the use of curly braces for other instances.
Examples of incorrect code for the "multi-line"
option:
/*eslint curly: ["error", "multi-line"]*/
if (foo)
doSomething();
else
doSomethingElse();
if (foo) foo(
bar,
baz);
Examples of correct code for the "multi-line"
option:
/*eslint curly: ["error", "multi-line"]*/
if (foo) foo++; else doSomething();
if (foo) foo++;
else if (bar) baz()
else doSomething();
do something();
while (foo);
while (foo
&& bar) baz();
if (foo) {
foo++;
}
if (foo) { foo++; }
while (true) {
doSomething();
doSomethingElse();
}
multi-or-nest
You can use another configuration that forces brace-less if
, else if
, else
, for
, while
, or do
if their body contains only one single-line statement. And forces braces in all other cases.
Examples of incorrect code for the "multi-or-nest"
option:
/*eslint curly: ["error", "multi-or-nest"]*/
if (!foo)
foo = {
bar: baz,
qux: foo
};
while (true)
if(foo)
doSomething();
else
doSomethingElse();
if (foo) {
foo++;
}
while (true) {
doSomething();
}
for (var i = 0; foo; i++) {
doSomething();
}
if (foo)
// some comment
bar();
Examples of correct code for the "multi-or-nest"
option:
/*eslint curly: ["error", "multi-or-nest"]*/
if (!foo) {
foo = {
bar: baz,
qux: foo
};
}
while (true) {
if(foo)
doSomething();
else
doSomethingElse();
}
if (foo)
foo++;
while (true)
doSomething();
for (var i = 0; foo; i++)
doSomething();
if (foo) {
// some comment
bar();
}
consistent
When using any of the multi*
options, you can add an option to enforce all bodies of a if
,
else if
and else
chain to be with or without braces.
Examples of incorrect code for the "multi", "consistent"
options:
/*eslint curly: ["error", "multi", "consistent"]*/
if (foo) {
bar();
baz();
} else
buz();
if (foo)
bar();
else if (faa)
bor();
else {
other();
things();
}
if (true)
foo();
else {
baz();
}
if (foo) {
foo++;
}
Examples of correct code for the "multi", "consistent"
options:
/*eslint curly: ["error", "multi", "consistent"]*/
if (foo) {
bar();
baz();
} else {
buz();
}
if (foo) {
bar();
} else if (faa) {
bor();
} else {
other();
things();
}
if (true)
foo();
else
baz();
if (foo)
foo++;
When Not To Use It
If you have no strict conventions about when to use block statements and when not to, you can safely disable this rule. Source: http://eslint.org/docs/rules/
Expected property shorthand. Open
init:init
- Read upRead up
- Exclude checks
Require Object Literal Shorthand Syntax (object-shorthand)
EcmaScript 6 provides a concise form for defining object literal methods and properties. This syntax can make defining complex object literals much cleaner.
Here are a few common examples using the ES5 syntax:
// properties
var foo = {
x: x,
y: y,
z: z,
};
// methods
var foo = {
a: function() {},
b: function() {}
};
Now here are ES6 equivalents:
/*eslint-env es6*/
// properties
var foo = {x, y, z};
// methods
var foo = {
a() {},
b() {}
};
Rule Details
This rule enforces the use of the shorthand syntax. This applies to all methods (including generators) defined in object literals and any properties defined where the key name matches name of the assigned variable.
Each of the following properties would warn:
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo = {
w: function() {},
x: function *() {},
[y]: function() {},
z: z
};
In that case the expected syntax would have been:
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo = {
w() {},
*x() {},
[y]() {},
z
};
This rule does not flag arrow functions inside of object literals. The following will not warn:
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo = {
x: (y) => y
};
Options
The rule takes an option which specifies when it should be applied. It can be set to one of the following values:
-
"always"
(default) expects that the shorthand will be used whenever possible. -
"methods"
ensures the method shorthand is used (also applies to generators). -
"properties"
ensures the property shorthand is used (where the key and variable name match). -
"never"
ensures that no property or method shorthand is used in any object literal. -
"consistent"
ensures that either all shorthand or all longform will be used in an object literal. -
"consistent-as-needed"
ensures that either all shorthand or all longform will be used in an object literal, but ensures all shorthand whenever possible.
You can set the option in configuration like this:
{
"object-shorthand": ["error", "always"]
}
Additionally, the rule takes an optional object configuration:
-
"avoidQuotes": true
indicates that longform syntax is preferred whenever the object key is a string literal (default:false
). Note that this option can only be enabled when the string option is set to"always"
,"methods"
, or"properties"
. -
"ignoreConstructors": true
can be used to prevent the rule from reporting errors for constructor functions. (By default, the rule treats constructors the same way as other functions.) Note that this option can only be enabled when the string option is set to"always"
or"methods"
. -
"avoidExplicitReturnArrows": true
indicates that methods are preferred over explicit-return arrow functions for function properties. (By default, the rule allows either of these.) Note that this option can only be enabled when the string option is set to"always"
or"methods"
.
avoidQuotes
{
"object-shorthand": ["error", "always", { "avoidQuotes": true }]
}
Example of incorrect code for this rule with the "always", { "avoidQuotes": true }
option:
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
/*eslint-env es6*/
var foo = {
"bar-baz"() {}
};
Example of correct code for this rule with the "always", { "avoidQuotes": true }
option:
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
/*eslint-env es6*/
var foo = {
"bar-baz": function() {},
"qux": qux
};
ignoreConstructors
{
"object-shorthand": ["error", "always", { "ignoreConstructors": true }]
}
Example of correct code for this rule with the "always", { "ignoreConstructors": true }
option:
/*eslint object-shorthand: ["error", "always", { "ignoreConstructors": true }]*/
/*eslint-env es6*/
var foo = {
ConstructorFunction: function() {}
};
avoidExplicitReturnArrows
{
"object-shorthand": ["error", "always", { "avoidExplicitReturnArrows": true }]
}
Example of incorrect code for this rule with the "always", { "avoidExplicitReturnArrows": true }
option:
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
/*eslint-env es6*/
var foo = {
foo: (bar, baz) => {
return bar + baz;
},
qux: (foobar) => {
return foobar * 2;
}
};
Example of correct code for this rule with the "always", { "avoidExplicitReturnArrows": true }
option:
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
/*eslint-env es6*/
var foo = {
foo(bar, baz) {
return bar + baz;
},
qux: foobar => foobar * 2
};
Example of incorrect code for this rule with the "consistent"
option:
/*eslint object-shorthand: [2, "consistent"]*/
/*eslint-env es6*/
var foo = {
a,
b: "foo",
};
Examples of correct code for this rule with the "consistent"
option:
/*eslint object-shorthand: [2, "consistent"]*/
/*eslint-env es6*/
var foo = {
a: a,
b: "foo"
};
var bar = {
a,
b,
};
Example of incorrect code with the "consistent-as-needed"
option, which is very similar to "consistent"
:
/*eslint object-shorthand: [2, "consistent-as-needed"]*/
/*eslint-env es6*/
var foo = {
a: a,
b: b,
};
When Not To Use It
Anyone not yet in an ES6 environment would not want to apply this rule. Others may find the terseness of the shorthand syntax harder to read and may not want to encourage it with this rule.
Further Reading
Object initializer - MDN Source: http://eslint.org/docs/rules/
Missing trailing comma. Open
refresh:refresh
- Read upRead up
- Exclude checks
require or disallow trailing commas (comma-dangle)
Trailing commas in object literals are valid according to the ECMAScript 5 (and ECMAScript 3!) spec. However, IE8 (when not in IE8 document mode) and below will throw an error when it encounters trailing commas in JavaScript.
var foo = {
bar: "baz",
qux: "quux",
};
Trailing commas simplify adding and removing items to objects and arrays, since only the lines you are modifying must be touched. Another argument in favor of trailing commas is that it improves the clarity of diffs when an item is added or removed from an object or array:
Less clear:
var foo = {
- bar: "baz",
- qux: "quux"
+ bar: "baz"
};
More clear:
var foo = {
bar: "baz",
- qux: "quux",
};
Rule Details
This rule enforces consistent use of trailing commas in object and array literals.
Options
This rule has a string option or an object option:
{
"comma-dangle": ["error", "never"],
// or
"comma-dangle": ["error", {
"arrays": "never",
"objects": "never",
"imports": "never",
"exports": "never",
"functions": "ignore",
}]
}
-
"never"
(default) disallows trailing commas -
"always"
requires trailing commas -
"always-multiline"
requires trailing commas when the last element or property is in a different line than the closing]
or}
and disallows trailing commas when the last element or property is on the same line as the closing]
or}
-
"only-multiline"
allows (but does not require) trailing commas when the last element or property is in a different line than the closing]
or}
and disallows trailing commas when the last element or property is on the same line as the closing]
or}
Trailing commas in function declarations and function calls are valid syntax since ECMAScript 2017; however, the string option does not check these situations for backwards compatibility.
You can also use an object option to configure this rule for each type of syntax.
Each of the following options can be set to "never"
, "always"
, "always-multiline"
, "only-multiline"
, or "ignore"
.
The default for each option is "never"
unless otherwise specified.
-
arrays
is for array literals and array patterns of destructuring. (e.g.let [a,] = [1,];
) -
objects
is for object literals and object patterns of destructuring. (e.g.let {a,} = {a: 1};
) -
imports
is for import declarations of ES Modules. (e.g.import {a,} from "foo";
) -
exports
is for export declarations of ES Modules. (e.g.export {a,};
) -
functions
is for function declarations and function calls. (e.g.(function(a,){ })(b,);
)
functions
is set to"ignore"
by default for consistency with the string option.
never
Examples of incorrect code for this rule with the default "never"
option:
/*eslint comma-dangle: ["error", "never"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var arr = [1,2,];
foo({
bar: "baz",
qux: "quux",
});
Examples of correct code for this rule with the default "never"
option:
/*eslint comma-dangle: ["error", "never"]*/
var foo = {
bar: "baz",
qux: "quux"
};
var arr = [1,2];
foo({
bar: "baz",
qux: "quux"
});
always
Examples of incorrect code for this rule with the "always"
option:
/*eslint comma-dangle: ["error", "always"]*/
var foo = {
bar: "baz",
qux: "quux"
};
var arr = [1,2];
foo({
bar: "baz",
qux: "quux"
});
Examples of correct code for this rule with the "always"
option:
/*eslint comma-dangle: ["error", "always"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var arr = [1,2,];
foo({
bar: "baz",
qux: "quux",
});
always-multiline
Examples of incorrect code for this rule with the "always-multiline"
option:
/*eslint comma-dangle: ["error", "always-multiline"]*/
var foo = {
bar: "baz",
qux: "quux"
};
var foo = { bar: "baz", qux: "quux", };
var arr = [1,2,];
var arr = [1,
2,];
var arr = [
1,
2
];
foo({
bar: "baz",
qux: "quux"
});
Examples of correct code for this rule with the "always-multiline"
option:
/*eslint comma-dangle: ["error", "always-multiline"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var foo = {bar: "baz", qux: "quux"};
var arr = [1,2];
var arr = [1,
2];
var arr = [
1,
2,
];
foo({
bar: "baz",
qux: "quux",
});
only-multiline
Examples of incorrect code for this rule with the "only-multiline"
option:
/*eslint comma-dangle: ["error", "only-multiline"]*/
var foo = { bar: "baz", qux: "quux", };
var arr = [1,2,];
var arr = [1,
2,];
Examples of correct code for this rule with the "only-multiline"
option:
/*eslint comma-dangle: ["error", "only-multiline"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var foo = {
bar: "baz",
qux: "quux"
};
var foo = {bar: "baz", qux: "quux"};
var arr = [1,2];
var arr = [1,
2];
var arr = [
1,
2,
];
var arr = [
1,
2
];
foo({
bar: "baz",
qux: "quux",
});
foo({
bar: "baz",
qux: "quux"
});
functions
Examples of incorrect code for this rule with the {"functions": "never"}
option:
/*eslint comma-dangle: ["error", {"functions": "never"}]*/
function foo(a, b,) {
}
foo(a, b,);
new foo(a, b,);
Examples of correct code for this rule with the {"functions": "never"}
option:
/*eslint comma-dangle: ["error", {"functions": "never"}]*/
function foo(a, b) {
}
foo(a, b);
new foo(a, b);
Examples of incorrect code for this rule with the {"functions": "always"}
option:
/*eslint comma-dangle: ["error", {"functions": "always"}]*/
function foo(a, b) {
}
foo(a, b);
new foo(a, b);
Examples of correct code for this rule with the {"functions": "always"}
option:
/*eslint comma-dangle: ["error", {"functions": "always"}]*/
function foo(a, b,) {
}
foo(a, b,);
new foo(a, b,);
When Not To Use It
You can turn this rule off if you are not concerned with dangling commas. Source: http://eslint.org/docs/rules/
Missing space before value for key 'ERROR'. Open
var type = { VALID:1, ERROR:2, WARNING:4, INFO:8 };
- Read upRead up
- Exclude checks
enforce consistent spacing between keys and values in object literal properties (key-spacing)
This rule enforces spacing around the colon in object literal properties. It can verify each property individually, or it can ensure horizontal alignment of adjacent properties in an object literal.
Rule Details
This rule enforces consistent spacing between keys and values in object literal properties. In the case of long lines, it is acceptable to add a new line wherever whitespace is allowed.
Options
This rule has an object option:
-
"beforeColon": false
(default) disallows spaces between the key and the colon in object literals. -
"beforeColon": true
requires at least one space between the key and the colon in object literals. -
"afterColon": true
(default) requires at least one space between the colon and the value in object literals. -
"afterColon": false
disallows spaces between the colon and the value in object literals. -
"mode": "strict"
(default) enforces exactly one space before or after colons in object literals. -
"mode": "minimum"
enforces one or more spaces before or after colons in object literals. -
"align": "value"
enforces horizontal alignment of values in object literals. -
"align": "colon"
enforces horizontal alignment of both colons and values in object literals. -
"align"
with an object value allows for fine-grained spacing when values are being aligned in object literals. -
"singleLine"
specifies a spacing style for single-line object literals. -
"multiLine"
specifies a spacing style for multi-line object literals.
Please note that you can either use the top-level options or the grouped options (singleLine
and multiLine
) but not both.
beforeColon
Examples of incorrect code for this rule with the default { "beforeColon": false }
option:
/*eslint key-spacing: ["error", { "beforeColon": false }]*/
var obj = { "foo" : 42 };
Examples of correct code for this rule with the default { "beforeColon": false }
option:
/*eslint key-spacing: ["error", { "beforeColon": false }]*/
var obj = { "foo": 42 };
Examples of incorrect code for this rule with the { "beforeColon": true }
option:
/*eslint key-spacing: ["error", { "beforeColon": true }]*/
var obj = { "foo": 42 };
Examples of correct code for this rule with the { "beforeColon": true }
option:
/*eslint key-spacing: ["error", { "beforeColon": true }]*/
var obj = { "foo" : 42 };
afterColon
Examples of incorrect code for this rule with the default { "afterColon": true }
option:
/*eslint key-spacing: ["error", { "afterColon": true }]*/
var obj = { "foo":42 };
Examples of correct code for this rule with the default { "afterColon": true }
option:
/*eslint key-spacing: ["error", { "afterColon": true }]*/
var obj = { "foo": 42 };
Examples of incorrect code for this rule with the { "afterColon": false }
option:
/*eslint key-spacing: ["error", { "afterColon": false }]*/
var obj = { "foo": 42 };
Examples of correct code for this rule with the { "afterColon": false }
option:
/*eslint key-spacing: ["error", { "afterColon": false }]*/
var obj = { "foo":42 };
mode
Examples of incorrect code for this rule with the default { "mode": "strict" }
option:
/*eslint key-spacing: ["error", { "mode": "strict" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the default { "mode": "strict" }
option:
/*eslint key-spacing: ["error", { "mode": "strict" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the { "mode": "minimum" }
option:
/*eslint key-spacing: ["error", { "mode": "minimum" }]*/
call({
foobar: 42,
bat: 2 * 2
});
align
Examples of incorrect code for this rule with the { "align": "value" }
option:
/*eslint key-spacing: ["error", { "align": "value" }]*/
var obj = {
a: value,
bcde: 42,
fg : foo()
};
Examples of correct code for this rule with the { "align": "value" }
option:
/*eslint key-spacing: ["error", { "align": "value" }]*/
var obj = {
a: value,
bcde: 42,
fg: foo(),
h: function() {
return this.a;
},
ijkl: 'Non-consecutive lines form a new group'
};
var obj = { a: "foo", longPropertyName: "bar" };
Examples of incorrect code for this rule with the { "align": "colon" }
option:
/*eslint key-spacing: ["error", { "align": "colon" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the { "align": "colon" }
option:
/*eslint key-spacing: ["error", { "align": "colon" }]*/
call({
foobar: 42,
bat : 2 * 2
});
align
The align
option can take additional configuration through the beforeColon
, afterColon
, mode
, and on
options.
If align
is defined as an object, but not all of the parameters are provided, undefined parameters will default to the following:
// Defaults
align: {
"beforeColon": false,
"afterColon": true,
"on": "colon",
"mode": "strict"
}
Examples of correct code for this rule with sample { "align": { } }
options:
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"one" : 1,
"seven" : 7
}
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": false,
"afterColon": false,
"on": "value"
}
}]*/
var obj = {
"one": 1,
"seven":7
}
align and multiLine
The multiLine
and align
options can differ, which allows for fine-tuned control over the key-spacing
of your files. align
will not inherit from multiLine
if align
is configured as an object.
multiLine
is used any time an object literal spans multiple lines. The align
configuration is used when there is a group of properties in the same object. For example:
var myObj = {
key1: 1, // uses multiLine
key2: 2, // uses align (when defined)
key3: 3, // uses align (when defined)
key4: 4 // uses multiLine
}
Examples of incorrect code for this rule with sample { "align": { }, "multiLine": { } }
options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon":true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"myObjectFunction": function() {
// Do something
},
"one" : 1,
"seven" : 7
}
Examples of correct code for this rule with sample { "align": { }, "multiLine": { } }
options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon": true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"myObjectFunction": function() {
// Do something
//
}, // These are two separate groups, so no alignment between `myObjectFuction` and `one`
"one" : 1,
"seven" : 7 // `one` and `seven` are in their own group, and therefore aligned
}
singleLine and multiLine
Examples of correct code for this rule with sample { "singleLine": { }, "multiLine": { } }
options:
/*eslint "key-spacing": [2, {
"singleLine": {
"beforeColon": false,
"afterColon": true
},
"multiLine": {
"beforeColon": true,
"afterColon": true,
"align": "colon"
}
}]*/
var obj = { one: 1, "two": 2, three: 3 };
var obj2 = {
"two" : 2,
three : 3
};
When Not To Use It
If you have another convention for property spacing that might not be consistent with the available options, or if you want to permit multiple styles concurrently you can safely disable this rule. Source: http://eslint.org/docs/rules/
Unexpected dangling '_' in '_closeBtnClicked'. Open
function _closeBtnClicked() {
- Read upRead up
- Exclude checks
disallow dangling underscores in identifiers (no-underscore-dangle)
As far as naming conventions for identifiers go, dangling underscores may be the most polarizing in JavaScript. Dangling underscores are underscores at either the beginning or end of an identifier, such as:
var _foo;
There is actually a long history of using dangling underscores to indicate "private" members of objects in JavaScript (though JavaScript doesn't have truly private members, this convention served as a warning). This began with SpiderMonkey adding nonstandard methods such as __defineGetter__()
. The intent with the underscores was to make it obvious that this method was special in some way. Since that time, using a single underscore prefix has become popular as a way to indicate "private" members of objects.
Whether or not you choose to allow dangling underscores in identifiers is purely a convention and has no effect on performance, readability, or complexity. It's purely a preference.
Rule Details
This rule disallows dangling underscores in identifiers.
Examples of incorrect code for this rule:
/*eslint no-underscore-dangle: "error"*/
var foo_;
var __proto__ = {};
foo._bar();
Examples of correct code for this rule:
/*eslint no-underscore-dangle: "error"*/
var _ = require('underscore');
var obj = _.contains(items, item);
obj.__proto__ = {};
var file = __filename;
Options
This rule has an object option:
-
"allow"
allows specified identifiers to have dangling underscores -
"allowAfterThis": false
(default) disallows dangling underscores in members of thethis
object -
"allowAfterSuper": false
(default) disallows dangling underscores in members of thesuper
object
allow
Examples of additional correct code for this rule with the { "allow": ["foo_", "_bar"] }
option:
/*eslint no-underscore-dangle: ["error", { "allow": ["foo_", "_bar"] }]*/
var foo_;
foo._bar();
allowAfterThis
Examples of correct code for this rule with the { "allowAfterThis": true }
option:
/*eslint no-underscore-dangle: ["error", { "allowAfterThis": true }]*/
var a = this.foo_;
this._bar();
allowAfterSuper
Examples of correct code for this rule with the { "allowAfterSuper": true }
option:
/*eslint no-underscore-dangle: ["error", { "allowAfterSuper": true }]*/
var a = super.foo_;
super._bar();
When Not To Use It
If you want to allow dangling underscores in identifiers, then you can safely turn this rule off. Source: http://eslint.org/docs/rules/
Expected property shorthand. Open
hide:hide,
- Read upRead up
- Exclude checks
Require Object Literal Shorthand Syntax (object-shorthand)
EcmaScript 6 provides a concise form for defining object literal methods and properties. This syntax can make defining complex object literals much cleaner.
Here are a few common examples using the ES5 syntax:
// properties
var foo = {
x: x,
y: y,
z: z,
};
// methods
var foo = {
a: function() {},
b: function() {}
};
Now here are ES6 equivalents:
/*eslint-env es6*/
// properties
var foo = {x, y, z};
// methods
var foo = {
a() {},
b() {}
};
Rule Details
This rule enforces the use of the shorthand syntax. This applies to all methods (including generators) defined in object literals and any properties defined where the key name matches name of the assigned variable.
Each of the following properties would warn:
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo = {
w: function() {},
x: function *() {},
[y]: function() {},
z: z
};
In that case the expected syntax would have been:
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo = {
w() {},
*x() {},
[y]() {},
z
};
This rule does not flag arrow functions inside of object literals. The following will not warn:
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo = {
x: (y) => y
};
Options
The rule takes an option which specifies when it should be applied. It can be set to one of the following values:
-
"always"
(default) expects that the shorthand will be used whenever possible. -
"methods"
ensures the method shorthand is used (also applies to generators). -
"properties"
ensures the property shorthand is used (where the key and variable name match). -
"never"
ensures that no property or method shorthand is used in any object literal. -
"consistent"
ensures that either all shorthand or all longform will be used in an object literal. -
"consistent-as-needed"
ensures that either all shorthand or all longform will be used in an object literal, but ensures all shorthand whenever possible.
You can set the option in configuration like this:
{
"object-shorthand": ["error", "always"]
}
Additionally, the rule takes an optional object configuration:
-
"avoidQuotes": true
indicates that longform syntax is preferred whenever the object key is a string literal (default:false
). Note that this option can only be enabled when the string option is set to"always"
,"methods"
, or"properties"
. -
"ignoreConstructors": true
can be used to prevent the rule from reporting errors for constructor functions. (By default, the rule treats constructors the same way as other functions.) Note that this option can only be enabled when the string option is set to"always"
or"methods"
. -
"avoidExplicitReturnArrows": true
indicates that methods are preferred over explicit-return arrow functions for function properties. (By default, the rule allows either of these.) Note that this option can only be enabled when the string option is set to"always"
or"methods"
.
avoidQuotes
{
"object-shorthand": ["error", "always", { "avoidQuotes": true }]
}
Example of incorrect code for this rule with the "always", { "avoidQuotes": true }
option:
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
/*eslint-env es6*/
var foo = {
"bar-baz"() {}
};
Example of correct code for this rule with the "always", { "avoidQuotes": true }
option:
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
/*eslint-env es6*/
var foo = {
"bar-baz": function() {},
"qux": qux
};
ignoreConstructors
{
"object-shorthand": ["error", "always", { "ignoreConstructors": true }]
}
Example of correct code for this rule with the "always", { "ignoreConstructors": true }
option:
/*eslint object-shorthand: ["error", "always", { "ignoreConstructors": true }]*/
/*eslint-env es6*/
var foo = {
ConstructorFunction: function() {}
};
avoidExplicitReturnArrows
{
"object-shorthand": ["error", "always", { "avoidExplicitReturnArrows": true }]
}
Example of incorrect code for this rule with the "always", { "avoidExplicitReturnArrows": true }
option:
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
/*eslint-env es6*/
var foo = {
foo: (bar, baz) => {
return bar + baz;
},
qux: (foobar) => {
return foobar * 2;
}
};
Example of correct code for this rule with the "always", { "avoidExplicitReturnArrows": true }
option:
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
/*eslint-env es6*/
var foo = {
foo(bar, baz) {
return bar + baz;
},
qux: foobar => foobar * 2
};
Example of incorrect code for this rule with the "consistent"
option:
/*eslint object-shorthand: [2, "consistent"]*/
/*eslint-env es6*/
var foo = {
a,
b: "foo",
};
Examples of correct code for this rule with the "consistent"
option:
/*eslint object-shorthand: [2, "consistent"]*/
/*eslint-env es6*/
var foo = {
a: a,
b: "foo"
};
var bar = {
a,
b,
};
Example of incorrect code with the "consistent-as-needed"
option, which is very similar to "consistent"
:
/*eslint object-shorthand: [2, "consistent-as-needed"]*/
/*eslint-env es6*/
var foo = {
a: a,
b: b,
};
When Not To Use It
Anyone not yet in an ES6 environment would not want to apply this rule. Others may find the terseness of the shorthand syntax harder to read and may not want to encourage it with this rule.
Further Reading
Object initializer - MDN Source: http://eslint.org/docs/rules/
Unexpected var, use let or const instead. Open
var SHOW_CHAR = 400;
- 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 value for key 'LESS_TEXT'. Open
LESS_TEXT:LESS_TEXT,
- Read upRead up
- Exclude checks
enforce consistent spacing between keys and values in object literal properties (key-spacing)
This rule enforces spacing around the colon in object literal properties. It can verify each property individually, or it can ensure horizontal alignment of adjacent properties in an object literal.
Rule Details
This rule enforces consistent spacing between keys and values in object literal properties. In the case of long lines, it is acceptable to add a new line wherever whitespace is allowed.
Options
This rule has an object option:
-
"beforeColon": false
(default) disallows spaces between the key and the colon in object literals. -
"beforeColon": true
requires at least one space between the key and the colon in object literals. -
"afterColon": true
(default) requires at least one space between the colon and the value in object literals. -
"afterColon": false
disallows spaces between the colon and the value in object literals. -
"mode": "strict"
(default) enforces exactly one space before or after colons in object literals. -
"mode": "minimum"
enforces one or more spaces before or after colons in object literals. -
"align": "value"
enforces horizontal alignment of values in object literals. -
"align": "colon"
enforces horizontal alignment of both colons and values in object literals. -
"align"
with an object value allows for fine-grained spacing when values are being aligned in object literals. -
"singleLine"
specifies a spacing style for single-line object literals. -
"multiLine"
specifies a spacing style for multi-line object literals.
Please note that you can either use the top-level options or the grouped options (singleLine
and multiLine
) but not both.
beforeColon
Examples of incorrect code for this rule with the default { "beforeColon": false }
option:
/*eslint key-spacing: ["error", { "beforeColon": false }]*/
var obj = { "foo" : 42 };
Examples of correct code for this rule with the default { "beforeColon": false }
option:
/*eslint key-spacing: ["error", { "beforeColon": false }]*/
var obj = { "foo": 42 };
Examples of incorrect code for this rule with the { "beforeColon": true }
option:
/*eslint key-spacing: ["error", { "beforeColon": true }]*/
var obj = { "foo": 42 };
Examples of correct code for this rule with the { "beforeColon": true }
option:
/*eslint key-spacing: ["error", { "beforeColon": true }]*/
var obj = { "foo" : 42 };
afterColon
Examples of incorrect code for this rule with the default { "afterColon": true }
option:
/*eslint key-spacing: ["error", { "afterColon": true }]*/
var obj = { "foo":42 };
Examples of correct code for this rule with the default { "afterColon": true }
option:
/*eslint key-spacing: ["error", { "afterColon": true }]*/
var obj = { "foo": 42 };
Examples of incorrect code for this rule with the { "afterColon": false }
option:
/*eslint key-spacing: ["error", { "afterColon": false }]*/
var obj = { "foo": 42 };
Examples of correct code for this rule with the { "afterColon": false }
option:
/*eslint key-spacing: ["error", { "afterColon": false }]*/
var obj = { "foo":42 };
mode
Examples of incorrect code for this rule with the default { "mode": "strict" }
option:
/*eslint key-spacing: ["error", { "mode": "strict" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the default { "mode": "strict" }
option:
/*eslint key-spacing: ["error", { "mode": "strict" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the { "mode": "minimum" }
option:
/*eslint key-spacing: ["error", { "mode": "minimum" }]*/
call({
foobar: 42,
bat: 2 * 2
});
align
Examples of incorrect code for this rule with the { "align": "value" }
option:
/*eslint key-spacing: ["error", { "align": "value" }]*/
var obj = {
a: value,
bcde: 42,
fg : foo()
};
Examples of correct code for this rule with the { "align": "value" }
option:
/*eslint key-spacing: ["error", { "align": "value" }]*/
var obj = {
a: value,
bcde: 42,
fg: foo(),
h: function() {
return this.a;
},
ijkl: 'Non-consecutive lines form a new group'
};
var obj = { a: "foo", longPropertyName: "bar" };
Examples of incorrect code for this rule with the { "align": "colon" }
option:
/*eslint key-spacing: ["error", { "align": "colon" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the { "align": "colon" }
option:
/*eslint key-spacing: ["error", { "align": "colon" }]*/
call({
foobar: 42,
bat : 2 * 2
});
align
The align
option can take additional configuration through the beforeColon
, afterColon
, mode
, and on
options.
If align
is defined as an object, but not all of the parameters are provided, undefined parameters will default to the following:
// Defaults
align: {
"beforeColon": false,
"afterColon": true,
"on": "colon",
"mode": "strict"
}
Examples of correct code for this rule with sample { "align": { } }
options:
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"one" : 1,
"seven" : 7
}
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": false,
"afterColon": false,
"on": "value"
}
}]*/
var obj = {
"one": 1,
"seven":7
}
align and multiLine
The multiLine
and align
options can differ, which allows for fine-tuned control over the key-spacing
of your files. align
will not inherit from multiLine
if align
is configured as an object.
multiLine
is used any time an object literal spans multiple lines. The align
configuration is used when there is a group of properties in the same object. For example:
var myObj = {
key1: 1, // uses multiLine
key2: 2, // uses align (when defined)
key3: 3, // uses align (when defined)
key4: 4 // uses multiLine
}
Examples of incorrect code for this rule with sample { "align": { }, "multiLine": { } }
options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon":true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"myObjectFunction": function() {
// Do something
},
"one" : 1,
"seven" : 7
}
Examples of correct code for this rule with sample { "align": { }, "multiLine": { } }
options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon": true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"myObjectFunction": function() {
// Do something
//
}, // These are two separate groups, so no alignment between `myObjectFuction` and `one`
"one" : 1,
"seven" : 7 // `one` and `seven` are in their own group, and therefore aligned
}
singleLine and multiLine
Examples of correct code for this rule with sample { "singleLine": { }, "multiLine": { } }
options:
/*eslint "key-spacing": [2, {
"singleLine": {
"beforeColon": false,
"afterColon": true
},
"multiLine": {
"beforeColon": true,
"afterColon": true,
"align": "colon"
}
}]*/
var obj = { one: 1, "two": 2, three: 3 };
var obj2 = {
"two" : 2,
three : 3
};
When Not To Use It
If you have another convention for property spacing that might not be consistent with the available options, or if you want to permit multiple styles concurrently you can safely disable this rule. Source: http://eslint.org/docs/rules/
Expected space(s) after "while". Open
while( numberElms > 0 )
- Read upRead up
- Exclude checks
enforce consistent spacing before and after keywords (keyword-spacing)
Keywords are syntax elements of JavaScript, such as function
and if
.
These identifiers have special meaning to the language and so often appear in a different color in code editors.
As an important part of the language, style guides often refer to the spacing that should be used around keywords.
For example, you might have a style guide that says keywords should be always surrounded by spaces, which would mean if-else
statements must look like this:
if (foo) {
// ...
} else {
// ...
}
Of course, you could also have a style guide that disallows spaces around keywords.
Rule Details
This rule enforces consistent spacing around keywords and keyword-like tokens: as
(in module declarations), async
(of async functions), await
(of await expressions), break
, case
, catch
, class
, const
, continue
, debugger
, default
, delete
, do
, else
, export
, extends
, finally
, for
, from
(in module declarations), function
, get
(of getters), if
, import
, in
, instanceof
, let
, new
, of
(in for-of statements), return
, set
(of setters), static
, super
, switch
, this
, throw
, try
, typeof
, var
, void
, while
, with
, and yield
. This rule is designed carefully not to conflict with other spacing rules: it does not apply to spacing where other rules report problems.
Options
This rule has an object option:
-
"before": true
(default) requires at least one space before keywords -
"before": false
disallows spaces before keywords -
"after": true
(default) requires at least one space after keywords -
"after": false
disallows spaces after keywords -
"overrides"
allows overriding spacing style for specified keywords
before
Examples of incorrect code for this rule with the default { "before": true }
option:
/*eslint keyword-spacing: ["error", { "before": true }]*/
if (foo) {
//...
}else if (bar) {
//...
}else {
//...
}
Examples of correct code for this rule with the default { "before": true }
option:
/*eslint keyword-spacing: ["error", { "before": true }]*/
/*eslint-env es6*/
if (foo) {
//...
} else if (bar) {
//...
} else {
//...
}
// no conflict with `array-bracket-spacing`
let a = [this];
let b = [function() {}];
// no conflict with `arrow-spacing`
let a = ()=> this.foo;
// no conflict with `block-spacing`
{function foo() {}}
// no conflict with `comma-spacing`
let a = [100,this.foo, this.bar];
// not conflict with `computed-property-spacing`
obj[this.foo] = 0;
// no conflict with `generator-star-spacing`
function *foo() {}
// no conflict with `key-spacing`
let obj = {
foo:function() {}
};
// no conflict with `object-curly-spacing`
let obj = {foo: this};
// no conflict with `semi-spacing`
let a = this;function foo() {}
// no conflict with `space-in-parens`
(function () {})();
// no conflict with `space-infix-ops`
if ("foo"in {foo: 0}) {}
if (10+this.foo<= this.bar) {}
// no conflict with `jsx-curly-spacing`
let a =
Examples of incorrect code for this rule with the { "before": false }
option:
/*eslint keyword-spacing: ["error", { "before": false }]*/
if (foo) {
//...
} else if (bar) {
//...
} else {
//...
}
Examples of correct code for this rule with the { "before": false }
option:
/*eslint keyword-spacing: ["error", { "before": false }]*/
if (foo) {
//...
}else if (bar) {
//...
}else {
//...
}
after
Examples of incorrect code for this rule with the default { "after": true }
option:
/*eslint keyword-spacing: ["error", { "after": true }]*/
if(foo) {
//...
} else if(bar) {
//...
} else{
//...
}
Examples of correct code for this rule with the default { "after": true }
option:
/*eslint keyword-spacing: ["error", { "after": true }]*/
if (foo) {
//...
} else if (bar) {
//...
} else {
//...
}
// not conflict with `array-bracket-spacing`
let a = [this];
// not conflict with `arrow-spacing`
let a = ()=> this.foo;
// not conflict with `comma-spacing`
let a = [100, this.foo, this.bar];
// not conflict with `computed-property-spacing`
obj[this.foo] = 0;
// not conflict with `generator-star-spacing`
function* foo() {}
// not conflict with `key-spacing`
let obj = {
foo:function() {}
};
// not conflict with `func-call-spacing`
class A {
constructor() {
super();
}
}
// not conflict with `object-curly-spacing`
let obj = {foo: this};
// not conflict with `semi-spacing`
let a = this;function foo() {}
// not conflict with `space-before-function-paren`
function() {}
// no conflict with `space-infix-ops`
if ("foo"in{foo: 0}) {}
if (10+this.foo<= this.bar) {}
// no conflict with `space-unary-ops`
function* foo(a) {
return yield+a;
}
// no conflict with `yield-star-spacing`
function* foo(a) {
return yield* a;
}
// no conflict with `jsx-curly-spacing`
let a =
Examples of incorrect code for this rule with the { "after": false }
option:
/*eslint keyword-spacing: ["error", { "after": false }]*/
if (foo) {
//...
} else if (bar) {
//...
} else {
//...
}
Examples of correct code for this rule with the { "after": false }
option:
/*eslint keyword-spacing: ["error", { "after": false }]*/
if(foo) {
//...
} else if(bar) {
//...
} else{
//...
}
overrides
Examples of correct code for this rule with the { "overrides": { "if": { "after": false }, "for": { "after": false }, "while": { "after": false } } }
option:
/*eslint keyword-spacing: ["error", { "overrides": {
"if": { "after": false },
"for": { "after": false },
"while": { "after": false }
} }]*/
if(foo) {
//...
} else if(bar) {
//...
} else {
//...
}
for(;;);
while(true) {
//...
}
When Not To Use It
If you don't want to enforce consistency on keyword spacing, then it's safe to disable this rule. Source: http://eslint.org/docs/rules/
Unexpected var, use let or const instead. Open
var MORE_TEXT = '<span><</span>more<span>></span>';
- 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/
'CharacterLimited' was used before it was defined. Open
return new CharacterLimited(elm, defaults);
- Read upRead up
- Exclude checks
Disallow Early Use (no-use-before-define)
In JavaScript, prior to ES6, variable and function declarations are hoisted to the top of a scope, so it's possible to use identifiers before their formal declarations in code. This can be confusing and some believe it is best to always declare variables and functions before using them.
In ES6, block-level bindings (let
and const
) introduce a "temporal dead zone" where a ReferenceError
will be thrown with any attempt to access the variable before its declaration.
Rule Details
This rule will warn when it encounters a reference to an identifier that has not yet been declared.
Examples of incorrect code for this rule:
/*eslint no-use-before-define: "error"*/
/*eslint-env es6*/
alert(a);
var a = 10;
f();
function f() {}
function g() {
return b;
}
var b = 1;
// With blockBindings: true
{
alert(c);
let c = 1;
}
Examples of correct code for this rule:
/*eslint no-use-before-define: "error"*/
/*eslint-env es6*/
var a;
a = 10;
alert(a);
function f() {}
f(1);
var b = 1;
function g() {
return b;
}
// With blockBindings: true
{
let C;
c++;
}
Options
{
"no-use-before-define": ["error", { "functions": true, "classes": true }]
}
-
functions
(boolean
) - The flag which shows whether or not this rule checks function declarations. If this istrue
, this rule warns every reference to a function before the function declaration. Otherwise, ignores those references. Function declarations are hoisted, so it's safe. Default istrue
. -
classes
(boolean
) - The flag which shows whether or not this rule checks class declarations of upper scopes. If this istrue
, this rule warns every reference to a class before the class declaration. Otherwise, ignores those references if the declaration is in upper function scopes. Class declarations are not hoisted, so it might be danger. Default istrue
. -
variables
(boolean
) - This flag determines whether or not the rule checks variable declarations in upper scopes. If this istrue
, the rule warns every reference to a variable before the variable declaration. Otherwise, the rule ignores a reference if the declaration is in an upper scope, while still reporting the reference if it's in the same scope as the declaration. Default istrue
.
This rule accepts "nofunc"
string as an option.
"nofunc"
is the same as { "functions": false, "classes": true }
.
functions
Examples of correct code for the { "functions": false }
option:
/*eslint no-use-before-define: ["error", { "functions": false }]*/
f();
function f() {}
classes
Examples of incorrect code for the { "classes": false }
option:
/*eslint no-use-before-define: ["error", { "classes": false }]*/
/*eslint-env es6*/
new A();
class A {
}
Examples of correct code for the { "classes": false }
option:
/*eslint no-use-before-define: ["error", { "classes": false }]*/
/*eslint-env es6*/
function foo() {
return new A();
}
class A {
}
variables
Examples of incorrect code for the { "variables": false }
option:
/*eslint no-use-before-define: ["error", { "variables": false }]*/
console.log(foo);
var foo = 1;
Examples of correct code for the { "variables": false }
option:
/*eslint no-use-before-define: ["error", { "variables": false }]*/
function baz() {
console.log(foo);
}
var foo = 1;
Source: http://eslint.org/docs/rules/
Missing space before value for key 'SOFT_LIMIT'. Open
SOFT_LIMIT:SOFT_LIMIT,
- Read upRead up
- Exclude checks
enforce consistent spacing between keys and values in object literal properties (key-spacing)
This rule enforces spacing around the colon in object literal properties. It can verify each property individually, or it can ensure horizontal alignment of adjacent properties in an object literal.
Rule Details
This rule enforces consistent spacing between keys and values in object literal properties. In the case of long lines, it is acceptable to add a new line wherever whitespace is allowed.
Options
This rule has an object option:
-
"beforeColon": false
(default) disallows spaces between the key and the colon in object literals. -
"beforeColon": true
requires at least one space between the key and the colon in object literals. -
"afterColon": true
(default) requires at least one space between the colon and the value in object literals. -
"afterColon": false
disallows spaces between the colon and the value in object literals. -
"mode": "strict"
(default) enforces exactly one space before or after colons in object literals. -
"mode": "minimum"
enforces one or more spaces before or after colons in object literals. -
"align": "value"
enforces horizontal alignment of values in object literals. -
"align": "colon"
enforces horizontal alignment of both colons and values in object literals. -
"align"
with an object value allows for fine-grained spacing when values are being aligned in object literals. -
"singleLine"
specifies a spacing style for single-line object literals. -
"multiLine"
specifies a spacing style for multi-line object literals.
Please note that you can either use the top-level options or the grouped options (singleLine
and multiLine
) but not both.
beforeColon
Examples of incorrect code for this rule with the default { "beforeColon": false }
option:
/*eslint key-spacing: ["error", { "beforeColon": false }]*/
var obj = { "foo" : 42 };
Examples of correct code for this rule with the default { "beforeColon": false }
option:
/*eslint key-spacing: ["error", { "beforeColon": false }]*/
var obj = { "foo": 42 };
Examples of incorrect code for this rule with the { "beforeColon": true }
option:
/*eslint key-spacing: ["error", { "beforeColon": true }]*/
var obj = { "foo": 42 };
Examples of correct code for this rule with the { "beforeColon": true }
option:
/*eslint key-spacing: ["error", { "beforeColon": true }]*/
var obj = { "foo" : 42 };
afterColon
Examples of incorrect code for this rule with the default { "afterColon": true }
option:
/*eslint key-spacing: ["error", { "afterColon": true }]*/
var obj = { "foo":42 };
Examples of correct code for this rule with the default { "afterColon": true }
option:
/*eslint key-spacing: ["error", { "afterColon": true }]*/
var obj = { "foo": 42 };
Examples of incorrect code for this rule with the { "afterColon": false }
option:
/*eslint key-spacing: ["error", { "afterColon": false }]*/
var obj = { "foo": 42 };
Examples of correct code for this rule with the { "afterColon": false }
option:
/*eslint key-spacing: ["error", { "afterColon": false }]*/
var obj = { "foo":42 };
mode
Examples of incorrect code for this rule with the default { "mode": "strict" }
option:
/*eslint key-spacing: ["error", { "mode": "strict" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the default { "mode": "strict" }
option:
/*eslint key-spacing: ["error", { "mode": "strict" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the { "mode": "minimum" }
option:
/*eslint key-spacing: ["error", { "mode": "minimum" }]*/
call({
foobar: 42,
bat: 2 * 2
});
align
Examples of incorrect code for this rule with the { "align": "value" }
option:
/*eslint key-spacing: ["error", { "align": "value" }]*/
var obj = {
a: value,
bcde: 42,
fg : foo()
};
Examples of correct code for this rule with the { "align": "value" }
option:
/*eslint key-spacing: ["error", { "align": "value" }]*/
var obj = {
a: value,
bcde: 42,
fg: foo(),
h: function() {
return this.a;
},
ijkl: 'Non-consecutive lines form a new group'
};
var obj = { a: "foo", longPropertyName: "bar" };
Examples of incorrect code for this rule with the { "align": "colon" }
option:
/*eslint key-spacing: ["error", { "align": "colon" }]*/
call({
foobar: 42,
bat: 2 * 2
});
Examples of correct code for this rule with the { "align": "colon" }
option:
/*eslint key-spacing: ["error", { "align": "colon" }]*/
call({
foobar: 42,
bat : 2 * 2
});
align
The align
option can take additional configuration through the beforeColon
, afterColon
, mode
, and on
options.
If align
is defined as an object, but not all of the parameters are provided, undefined parameters will default to the following:
// Defaults
align: {
"beforeColon": false,
"afterColon": true,
"on": "colon",
"mode": "strict"
}
Examples of correct code for this rule with sample { "align": { } }
options:
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"one" : 1,
"seven" : 7
}
/*eslint key-spacing: ["error", {
"align": {
"beforeColon": false,
"afterColon": false,
"on": "value"
}
}]*/
var obj = {
"one": 1,
"seven":7
}
align and multiLine
The multiLine
and align
options can differ, which allows for fine-tuned control over the key-spacing
of your files. align
will not inherit from multiLine
if align
is configured as an object.
multiLine
is used any time an object literal spans multiple lines. The align
configuration is used when there is a group of properties in the same object. For example:
var myObj = {
key1: 1, // uses multiLine
key2: 2, // uses align (when defined)
key3: 3, // uses align (when defined)
key4: 4 // uses multiLine
}
Examples of incorrect code for this rule with sample { "align": { }, "multiLine": { } }
options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon":true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"myObjectFunction": function() {
// Do something
},
"one" : 1,
"seven" : 7
}
Examples of correct code for this rule with sample { "align": { }, "multiLine": { } }
options:
/*eslint key-spacing: ["error", {
"multiLine": {
"beforeColon": false,
"afterColon": true
},
"align": {
"beforeColon": true,
"afterColon": true,
"on": "colon"
}
}]*/
var obj = {
"myObjectFunction": function() {
// Do something
//
}, // These are two separate groups, so no alignment between `myObjectFuction` and `one`
"one" : 1,
"seven" : 7 // `one` and `seven` are in their own group, and therefore aligned
}
singleLine and multiLine
Examples of correct code for this rule with sample { "singleLine": { }, "multiLine": { } }
options:
/*eslint "key-spacing": [2, {
"singleLine": {
"beforeColon": false,
"afterColon": true
},
"multiLine": {
"beforeColon": true,
"afterColon": true,
"align": "colon"
}
}]*/
var obj = { one: 1, "two": 2, three: 3 };
var obj2 = {
"two" : 2,
three : 3
};
When Not To Use It
If you have another convention for property spacing that might not be consistent with the available options, or if you want to permit multiple styles concurrently you can safely disable this rule. Source: http://eslint.org/docs/rules/
Unexpected var, use let or const instead. Open
var _showHideElm;
- 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/
All 'var' declarations must be at the top of the function scope. Open
var html = c + '<span class="moreellipses">' +
- Read upRead up
- Exclude checks
Require Variable Declarations to be at the top of their scope (vars-on-top)
The vars-on-top
rule generates warnings when variable declarations are not used serially at the top of a function scope or the top of a program.
By default variable declarations are always moved (“hoisted”) invisibly to the top of their containing scope by the JavaScript interpreter.
This rule forces the programmer to represent that behaviour by manually moving the variable declaration to the top of its containing scope.
Rule Details
This rule aims to keep all variable declarations in the leading series of statements. Allowing multiple declarations helps promote maintainability and is thus allowed.
Examples of incorrect code for this rule:
/*eslint vars-on-top: "error"*/
// Variable declarations in a block:
function doSomething() {
var first;
if (true) {
first = true;
}
var second;
}
// Variable declaration in for initializer:
function doSomething() {
for (var i=0; i<10; i++) {}
}
/*eslint vars-on-top: "error"*/
// Variables after other statements:
f();
var a;
Examples of correct code for this rule:
/*eslint vars-on-top: "error"*/
function doSomething() {
var first;
var second; //multiple declarations are allowed at the top
if (true) {
first = true;
}
}
function doSomething() {
var i;
for (i=0; i<10; i++) {}
}
/*eslint vars-on-top: "error"*/
var a;
f();
/*eslint vars-on-top: "error"*/
// Directives may precede variable declarations.
"use strict";
var a;
f();
// Comments can describe variables.
function doSomething() {
// this is the first var.
var first;
// this is the second var.
var second
}
Further Reading
Infix operators must be spaced. Open
var poiStyles =[
- Read upRead up
- Exclude checks
require spacing around infix operators (space-infix-ops)
While formatting preferences are very personal, a number of style guides require spaces around operators, such as:
var sum = 1 + 2;
The proponents of these extra spaces believe it make the code easier to read and can more easily highlight potential errors, such as:
var sum = i+++2;
While this is valid JavaScript syntax, it is hard to determine what the author intended.
Rule Details
This rule is aimed at ensuring there are spaces around infix operators.
Options
This rule accepts a single options argument with the following defaults:
"space-infix-ops": ["error", {"int32Hint": false}]
int32Hint
Set the int32Hint
option to true
(default is false
) to allow write a|0
without space.
var foo = bar|0; // `foo` is forced to be signed 32 bit integer
Examples of incorrect code for this rule:
/*eslint space-infix-ops: "error"*/
/*eslint-env es6*/
a+b
a+ b
a +b
a?b:c
const a={b:1};
var {a=0}=bar;
function foo(a=0) { }
Examples of correct code for this rule:
/*eslint space-infix-ops: "error"*/
/*eslint-env es6*/
a + b
a + b
a ? b : c
const a = {b:1};
var {a = 0} = bar;
function foo(a = 0) { }
Source: http://eslint.org/docs/rules/
Unexpected dangling '_' in '_isShowingMore'. Open
var _isShowingMore = false;
- Read upRead up
- Exclude checks
disallow dangling underscores in identifiers (no-underscore-dangle)
As far as naming conventions for identifiers go, dangling underscores may be the most polarizing in JavaScript. Dangling underscores are underscores at either the beginning or end of an identifier, such as:
var _foo;
There is actually a long history of using dangling underscores to indicate "private" members of objects in JavaScript (though JavaScript doesn't have truly private members, this convention served as a warning). This began with SpiderMonkey adding nonstandard methods such as __defineGetter__()
. The intent with the underscores was to make it obvious that this method was special in some way. Since that time, using a single underscore prefix has become popular as a way to indicate "private" members of objects.
Whether or not you choose to allow dangling underscores in identifiers is purely a convention and has no effect on performance, readability, or complexity. It's purely a preference.
Rule Details
This rule disallows dangling underscores in identifiers.
Examples of incorrect code for this rule:
/*eslint no-underscore-dangle: "error"*/
var foo_;
var __proto__ = {};
foo._bar();
Examples of correct code for this rule:
/*eslint no-underscore-dangle: "error"*/
var _ = require('underscore');
var obj = _.contains(items, item);
obj.__proto__ = {};
var file = __filename;
Options
This rule has an object option:
-
"allow"
allows specified identifiers to have dangling underscores -
"allowAfterThis": false
(default) disallows dangling underscores in members of thethis
object -
"allowAfterSuper": false
(default) disallows dangling underscores in members of thesuper
object
allow
Examples of additional correct code for this rule with the { "allow": ["foo_", "_bar"] }
option:
/*eslint no-underscore-dangle: ["error", { "allow": ["foo_", "_bar"] }]*/
var foo_;
foo._bar();
allowAfterThis
Examples of correct code for this rule with the { "allowAfterThis": true }
option:
/*eslint no-underscore-dangle: ["error", { "allowAfterThis": true }]*/
var a = this.foo_;
this._bar();
allowAfterSuper
Examples of correct code for this rule with the { "allowAfterSuper": true }
option:
/*eslint no-underscore-dangle: ["error", { "allowAfterSuper": true }]*/
var a = super.foo_;
super._bar();
When Not To Use It
If you want to allow dangling underscores in identifiers, then you can safely turn this rule off. Source: http://eslint.org/docs/rules/
Missing trailing comma. Open
}
- Read upRead up
- Exclude checks
require or disallow trailing commas (comma-dangle)
Trailing commas in object literals are valid according to the ECMAScript 5 (and ECMAScript 3!) spec. However, IE8 (when not in IE8 document mode) and below will throw an error when it encounters trailing commas in JavaScript.
var foo = {
bar: "baz",
qux: "quux",
};
Trailing commas simplify adding and removing items to objects and arrays, since only the lines you are modifying must be touched. Another argument in favor of trailing commas is that it improves the clarity of diffs when an item is added or removed from an object or array:
Less clear:
var foo = {
- bar: "baz",
- qux: "quux"
+ bar: "baz"
};
More clear:
var foo = {
bar: "baz",
- qux: "quux",
};
Rule Details
This rule enforces consistent use of trailing commas in object and array literals.
Options
This rule has a string option or an object option:
{
"comma-dangle": ["error", "never"],
// or
"comma-dangle": ["error", {
"arrays": "never",
"objects": "never",
"imports": "never",
"exports": "never",
"functions": "ignore",
}]
}
-
"never"
(default) disallows trailing commas -
"always"
requires trailing commas -
"always-multiline"
requires trailing commas when the last element or property is in a different line than the closing]
or}
and disallows trailing commas when the last element or property is on the same line as the closing]
or}
-
"only-multiline"
allows (but does not require) trailing commas when the last element or property is in a different line than the closing]
or}
and disallows trailing commas when the last element or property is on the same line as the closing]
or}
Trailing commas in function declarations and function calls are valid syntax since ECMAScript 2017; however, the string option does not check these situations for backwards compatibility.
You can also use an object option to configure this rule for each type of syntax.
Each of the following options can be set to "never"
, "always"
, "always-multiline"
, "only-multiline"
, or "ignore"
.
The default for each option is "never"
unless otherwise specified.
-
arrays
is for array literals and array patterns of destructuring. (e.g.let [a,] = [1,];
) -
objects
is for object literals and object patterns of destructuring. (e.g.let {a,} = {a: 1};
) -
imports
is for import declarations of ES Modules. (e.g.import {a,} from "foo";
) -
exports
is for export declarations of ES Modules. (e.g.export {a,};
) -
functions
is for function declarations and function calls. (e.g.(function(a,){ })(b,);
)
functions
is set to"ignore"
by default for consistency with the string option.
never
Examples of incorrect code for this rule with the default "never"
option:
/*eslint comma-dangle: ["error", "never"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var arr = [1,2,];
foo({
bar: "baz",
qux: "quux",
});
Examples of correct code for this rule with the default "never"
option:
/*eslint comma-dangle: ["error", "never"]*/
var foo = {
bar: "baz",
qux: "quux"
};
var arr = [1,2];
foo({
bar: "baz",
qux: "quux"
});
always
Examples of incorrect code for this rule with the "always"
option:
/*eslint comma-dangle: ["error", "always"]*/
var foo = {
bar: "baz",
qux: "quux"
};
var arr = [1,2];
foo({
bar: "baz",
qux: "quux"
});
Examples of correct code for this rule with the "always"
option:
/*eslint comma-dangle: ["error", "always"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var arr = [1,2,];
foo({
bar: "baz",
qux: "quux",
});
always-multiline
Examples of incorrect code for this rule with the "always-multiline"
option:
/*eslint comma-dangle: ["error", "always-multiline"]*/
var foo = {
bar: "baz",
qux: "quux"
};
var foo = { bar: "baz", qux: "quux", };
var arr = [1,2,];
var arr = [1,
2,];
var arr = [
1,
2
];
foo({
bar: "baz",
qux: "quux"
});
Examples of correct code for this rule with the "always-multiline"
option:
/*eslint comma-dangle: ["error", "always-multiline"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var foo = {bar: "baz", qux: "quux"};
var arr = [1,2];
var arr = [1,
2];
var arr = [
1,
2,
];
foo({
bar: "baz",
qux: "quux",
});
only-multiline
Examples of incorrect code for this rule with the "only-multiline"
option:
/*eslint comma-dangle: ["error", "only-multiline"]*/
var foo = { bar: "baz", qux: "quux", };
var arr = [1,2,];
var arr = [1,
2,];
Examples of correct code for this rule with the "only-multiline"
option:
/*eslint comma-dangle: ["error", "only-multiline"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var foo = {
bar: "baz",
qux: "quux"
};
var foo = {bar: "baz", qux: "quux"};
var arr = [1,2];
var arr = [1,
2];
var arr = [
1,
2,
];
var arr = [
1,
2
];
foo({
bar: "baz",
qux: "quux",
});
foo({
bar: "baz",
qux: "quux"
});
functions
Examples of incorrect code for this rule with the {"functions": "never"}
option:
/*eslint comma-dangle: ["error", {"functions": "never"}]*/
function foo(a, b,) {
}
foo(a, b,);
new foo(a, b,);
Examples of correct code for this rule with the {"functions": "never"}
option:
/*eslint comma-dangle: ["error", {"functions": "never"}]*/
function foo(a, b) {
}
foo(a, b);
new foo(a, b);
Examples of incorrect code for this rule with the {"functions": "always"}
option:
/*eslint comma-dangle: ["error", {"functions": "always"}]*/
function foo(a, b) {
}
foo(a, b);
new foo(a, b);
Examples of correct code for this rule with the {"functions": "always"}
option:
/*eslint comma-dangle: ["error", {"functions": "always"}]*/
function foo(a, b,) {
}
foo(a, b,);
new foo(a, b,);
When Not To Use It
You can turn this rule off if you are not concerned with dangling commas. Source: http://eslint.org/docs/rules/