An ESLint config for geniuses.
But you can continue to be standard if you want.
This is a combination of best practices, styles that have objective benefits, and my own (let's face it, correct) opinions. I try to provide my rationale for the more contentious decisions.
{
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
}
}
"error"
Require camelCase
for variables and object properties.
[
"error",
"always-multiline"
]
Require multiline list items to have a dangling comma.
- Makes adding and reordering items a whole lot less fiddly.
- Simplifies diffs and makes them clearer. See the ESLint documentation for a good example of this.
const things = [
'foo',
'bar'
];
const data = {
foo: 'bar',
baz: 'bat'
}
const things = [
'foo',
'bar',
];
const data = {
foo: 'bar',
baz: 'bat',
};
"error"
Space after commas, but not before.
"error"
Require a comma after and on the same line as an array element, object property, or variable declaration
"error"
Blocks require curly braces.
- More consistent.
- Easier and less error-prone to refactor a block later to include more lines.
if (foo) alert(foo);
if (foo)
alert(foo);
alert(bar); // not inside the if block!
if (foo) {
alert(foo);
}
if (foo) {
alert(foo);
}
alert(bar); // obviously not inside the if block.
"error"
Require a newline at the end of files.
"error"
Always require ===
and !==
, even for literals on both sides and even when null
is involved.
[
"error",
"never"
]
Disallow whitespace between a function call and its parens.
[
"error",
"smart-tabs"
]
Require tabs for indentation, but spaces for alignment. Combining them in this specific way provides the best of both.
- Tabs maintain their intended purpose of indentation, and nothing else.
- Code can always be viewed using the tab width the reader is most comfortable with.
- Lined-up code will stay lined-up no matter what.
if (foo) {
/*ss*/alert(foo);
}
const data = {
/*tab*/foo: 'barbaz',/*tab*/ // Some descriptive text
/*tab*//*tab*//*tab*//*tab*/ // explaining this item
/*tab*/quz: 'qux',
};
if (foo) {
/*tab*/alert(foo);
}
const data = {
/*tab*/foo: 'barbaz', // Some descriptive text
/*tab*//*ssssssssss*/ // explaining this item
/*tab*/quz: 'qux',
};
"error"
Do not allow trailing whitespace, even on blank lines.
"error"
Always require let
or const
as opposed to var
.
"error"
Always prefer const
as opposed to let
when possible.
"error"
Always require semicolons.
"error"
Require a space before the opening brace of a block.
if (a){
b();
}
function a(){}
try {} catch(a){}
class Foo{
constructor(){}
}
if (a) {
b();
}
function a() {}
try {} catch(a) {}
class Foo {
constructor() {}
}
[
"error",
{
"anonymous": "always",
"named": "never",
"asyncArrow": "always"
}
]
Require a space after anonymous and arrow function parens and disallow for named function parens.
[
"error",
"never"
]
Disallow spaces padding the insides of parens.
[
"error",
{
"words": true,
"nonwords": false,
"overrides": {
"!": true,
"!!": true
}
}
]
Require a space after !
and !!
, but not other non-word unary operators.
- It can be very easy to miss a
!
(and, to a lesser extent,!!
) in front of a variable when scanning code. A space instantly makes things clearer at a glance. - Other unary operators are much easier to see.
if (!foo) {
badFooCount ++;
}
foo = !bar;
if (! foo) {
badFooCount++;
}
foo = ! bar;