An ESLint Shareable Config for javascript postmodern style
npm install eslint-config-postmodern
Shareable configs are designed to work with the extends
feature of .eslintrc
files.
You can learn more about
Shareable Configs on the
official ESLint website.
To use the javascript postmodern style shareable config, first run this:
npm install --save-dev eslint-config-postmodern eslint-plugin-promise eslint-plugin-import eslint-plugin-node
Then, add this to your .eslintrc file:
{
"extends": "postmodern"
}
You can override settings from the shareable config by adding them directly into your
.eslintrc
file.
So, what exactly does postmodern give you? First and foremost, semicolons are required to enforce readability. Beyond that, below is a visual guide to the rules. Code samples are taken straight from eslint.org. Second, postmodern follows eslint's recommended rules and, as such, those will not be listed below.
Require a space before/after arrow function's arrow:
(a) => {}
Postmodern follows the one true brace style where the opening brace of a block is placed on the same line as its corresponding statement of declaration.
if (foo) {
bar();
} else {
baz();
}
Postmodern follows camelcase (camelCase) rather than snake case (snake_case) for functions and variables. However, errors are not thrown if an object property is snake case as sometimes that is unavoidable.
function myNewFunction() {
...
}
const myVariable;
Postmodern disallows comma dangles for arrays, objects, imports, exports, and functions.
const foo = {
bar: 'baz',
qux: 'quux' // No comma!
};
const arr = ['one', 'two'];
Requires a space after a comma
foo(a, b);
Requies a comma after and on the same line as an array element, or object property.
const obj = {
a: 1, // Comma is required to be on this line and not the following
b: 2
};
Requires curly braces in cases where they can be omitted.
if (foo) {
foo++;
}
// Does not allow for if (foo) foo++;
Enforced for objects and properties, requires the dot should be on the same line.
const foo = obj.property;
Requires a newline at the end of a file
Requires === and !== in lieu of == and != to take advantage of type safety.
if (x === 42) { // x == 42 throws an error
...
};
if (y !== x) {
...
};
Disallows a space between function identifiers and their invocations.
new Date(); // new Date (); throws an error
In node.js, a common pattern for dealing with asynchronous behavior is called the callback pattern. This pattern expects an Error object or null as the first argument of the callback.
function loadData(err, data) {
...
}
Requires consistent indentation of code blocks. Postmodern requires two spaces.
if (a) {
b = c;
const foo = (d) => {
e = f;
};
}
switch(a) {
case 1:
b = c;
break;
case 2:
c = d;
break;
default:
d = e;
break;
}
Requires a space after a colon.
const obj = {
foo: 42 // Space required here
};
Requires a space before and after syntax keywords such as as, async, await, break, case, catch, class, const, etc.
Bad formatting:
if(foo) { // Error
...
}else{ // Error
...
}
Correct:
if (foo) {
...
} else {
...
}
const a = 'b';
await Promise.all(promises);
let a = [100, this.foo, this.bar];
Requires all new operators to be called with uppercase-started functions
const friend = new Person();
Disallows the omission of parentheses when invoking a function via the new keyword.
const person = new Person(); // Calling new Person results in an error
Gives a warning on the use of console.log, console.warn, console.error, etc.
When a statement is too long to fit onto a single line, the operator follows on the next line.
const fullHeight = borderTop
+ innerHeight
+ boderBottom;
Block statements will never begin or end with a newline.
if (a) {
b();
}
Only single quotes are accepted as valid.
Disallows spacing the spread operator and its expression.
let {x, y, ...z} = {x: 1, y: 2, a: 3, b: 4};
Disallows spaces before semicolons but enforces them after.
for (let i = 0; i < 10; i++) {
...
}
Blocks must always have at least one preceding space.
if (a) {
b();
}
function a() {
...
}
try {
...
} catch(e) {
...
}
Disallows spaces before the parentheses of a function
function withoutSpace(x) { // Cannot use withSpace (a)
...
}
Disallows spaces within parentheses
foo('bar'); \\ Not foo( 'bar' );
const foo = (1 + 2) * 3; // Not ( 1 + 2 )
Requires spacing around operators
const sum = 1 + 2; // Not 1+2
Requires spaces for words new, delete, typeof, void, yield
//
must be followed by a space.
// See, the comment marker is followed by a space
Disallows spaces within ${}
const template = `Disallow spaces within ${tempVar}`;