Always use spaces characters where two (2) spaces are used for indentation. The usage of tab characters is disallowed. A tab could be a different number of columns depending on the environment, but a space is always one column. Adhering to this rule increases the code readability and maintainability significantly.
ESLint: indent
Note: The »
character represents a tab.
⇣ Incorrect code for this rule:
function winter() {
» let snow;
}
function winter() {
let snow;
}
function winter() {
let snow;
}
⇡ Correct code for this rule:
function winter() {
let snow;
}
Place one (1) space before the leading brace.
ESLint: space-before-blocks
⇣ Incorrect code for this rule:
function snow(){
console.log("snow");
}
winter.set("snow",{
density: 20,
frozen: false
});
⇡ Correct code for this rule:
function snow() {
console.log("snow");
}
winter.set("snow", {
density: 20,
frozen: false,
});
Place one (1) space before the opening parenthesis in control statements (if
, while
etc.). Place no space between the argument list and the function name in function calls and declarations.
ESLint: keyword-spacing
⇣ Incorrect code for this rule:
if(isWinter) {
snow ();
}
function snow () {
console.log ("falling");
}
⇡ Correct code for this rule:
if (isWinter) {
snow();
}
function snow() {
console.log("falling");
}
Set off operators with spaces.
ESLint: space-infix-ops
⇣ Incorrect code for this rule:
const snowflakes=snow+5;
⇡ Correct code for this rule:
const snowflakes = snow + 5;
End files with a single newline character. Prefer the LF control character (*nix based OS) and avoid the usage of CRLF characters (mostly Microsoft Windows based OS).
ESlint: eol-last
⇣ Incorrect code for this rule:
import { snow } from "./Winter";
export default snow;
import { snow } from "./Winter";
export default snow;¬
¬
⇡ Correct code for this rule:
import { snow } from "./Winter";
export default snow;¬
Use indentation when making long method chains. Use a leading dot, which emphasizes that the line is a method call, not a new statement.
ESLint: newline-per-chained-call and no-whitespace-before-property
⇣ Incorrect code for this rule:
const elements = ["snow", "frost", "ice"];
elements.map(element => `sparkling ${element}`).find("snow").highlight().tokenize(2).end().updateCount();
const elements = ["snow", "frost", "ice"];
elements.map(element => `sparkling ${element}`).
find("snow").
highlight().
tokenize(2).
end().
updateCount();
const elements = ["snow", "frost", "ice"];
elements.map(element => `sparkling ${element}`).find("snow").highlight().tokenize(2).find("frost")
tokenize(3).highlight(false).find("ice").tokenize(1)
.find("wind").highlight().tokenize().updateCount()
.toString();
⇡ Correct code for this rule:
const elements = ["snow", "frost", "ice"];
elements
.map((element) => `sparkling ${element}`)
.find("snow")
.highlight()
.tokenize(2)
.end()
.updateCount();
const elements = ["snow", "frost", "ice"];
elements
.map((element) => `sparkling ${element}`)
.tokenize(2)
.updateCount();
Leave a blank line after blocks and before the next statement.
⇣ Incorrect code for this rule:
if (winter) {
return snow;
}
return frost;
const winter = {
snow() {},
frost() {},
};
return winter;
const winter = [
function snow() {},
function frost() {}
];
return winter;
⇡ Correct code for this rule:
if (winter) {
return snow;
}
return frost;
const winter = {
snow() {},
frost() {},
};
return winter;
const winter = [function snow() {}, function frost() {}];
return winter;
Do not pad blocks with blank lines.
ESLint: padded-blocks
⇣ Incorrect code for this rule:
function winter() {
console.log(snow);
}
if (winter) {
console.log(snow);
} else {
console.log(frost);
}
class Winter {
constructor(snow) {
this.snow = snow;
}
}
⇡ Correct code for this rule:
function winter() {
console.log(snow);
}
if (winter) {
console.log(snow);
} else {
console.log(frost);
}
class Winter {
constructor(snow) {
this.snow = snow;
}
}
Do not add spaces inside parentheses.
ESLint: space-in-parens
⇣ Incorrect code for this rule:
function winter( element ) {
return element;
}
if ( winter ) {
console.log(snow);
}
⇡ Correct code for this rule:
function winter(element) {
return element;
}
if (winter) {
console.log(snow);
}
Do not add spaces inside brackets.
ESLint: array-bracket-spacing
⇣ Incorrect code for this rule:
const winter = [ "snow", "frost", "ice" ];
console.log(winter[ 0 ]);
⇡ Correct code for this rule:
const winter = ["snow", "frost", "ice"];
console.log(winter[0]);
Add spaces inside curly braces.
ESlint: object-curly-spacing
⇣ Incorrect code for this rule:
const season = {name: "winter"};
⇡ Correct code for this rule:
const season = { name: "winter" };
Avoid having lines of code that are longer than 100 characters including whitespaces. Adhering to this rule increases the code readability and maintainability significantly.
Note that the rule for long strings is exempt from this rule, and should not be broken up.
ESLint: max-len
⇣ Incorrect code for this rule:
const season = winter && winter.elements && winter.elements.snow && winter.elements.snow.state && winter.elements.snow.state.temperature && winter.elements.snow.state.temperature.celsius;
season({ name: "winter", elements: ["snow", "frost"] }).load(() => console.log("Sparkling")).catch(() => console.log("Melting"));
⇡ Correct code for this rule:
const season =
winter &&
winter.elements &&
winter.elements.snow &&
winter.elements.snow.state &&
winter.elements.snow.state.temperature &&
winter.elements.snow.state.temperature.celsius;
season({
name: "winter",
elements: ["snow", "frost"],
})
.load(() => console.log("Sparkling"))
.catch(() => console.log("Melting"));
Use consistent spacing inside an open block token and the next token on the same line. This rule also enforces consistent spacing inside a close block token and previous token on the same line.
ESLint: block-spacing
⇣ Incorrect code for this rule:
function snow() {return true;}
if (snow) { flakes = 0;}
⇡ Correct code for this rule:
function snow() { return true; }
if (snow) { flakes = 0; }
No spaces before commas and require a space after commas.
ESLint: comma-spacing
⇣ Incorrect code for this rule:
var snow = 1,flakes = 2;
var winter = [1 , 2];
⇡ Correct code for this rule:
var snow = 1, flakes = 2;
var winter = [1, 2];
Use spacing inside of computed property brackets.
ESLint: computed-property-spacing
⇣ Incorrect code for this rule:
winter[snow ];
winter[ "snow"];
var flakes = {[ frost ]: cold};
winter[snow[ ice ]];
⇡ Correct code for this rule:
obj[snow];
obj["snow"];
var flakes = { [frost]: cold };
winter[snow[ice]];
No spaces between functions and their invocations.
ESLint: func-call-spacing
⇣ Incorrect code for this rule:
func ();
func
();
⇡ Correct code for this rule:
func();
Use spacing between keys and values in object literal properties.
ESLint: key-spacing
⇣ Incorrect code for this rule:
var winter = { "snow" : 42 };
var arctic = { "ice":42 };
⇡ Correct code for this rule:
var winter = { "snow": 42 };
No trailing spaces at the end of lines.
ESLint: no-trailing-spaces
No multiple empty lines, only allow one (1) newline at the end of files, and no newline at the beginning of files. Also do not use multiple blank lines to pad code.
ESLint: no-multiple-empty-lines
⇣ Incorrect code for this rule:
var winter = 1;
var snow = 2;
// 2+ newlines at end of file.
var winter = 1;
var snow = 2;
// 1+ newline(s) at beginning of file.
var winter = 1;
var snow = 2;
const snow = winter => {
// code padded with 2+ newlines
const snow = winter.getSnow();
const frost = winter.getFrost();
const ice = winter.getIce();
}
⇡ Correct code for this rule:
var winter = 1;
var snow = 2;
const snow = (winter) => {
// code padded with 2+ newlines
const snow = winter.getSnow();
const frost = winter.getFrost();
const ice = winter.getIce();
};