Seedrs Javascript eslint config
This is the Seedrs Javascript eslint base configuration. This configuration should be used in all Javascript project and can be extended if needed i.e. It doesn't contain any rules for React. The configuration targets es6 and newer versions of Javascript. It borrows heavily from airbnbs javascript style guide but with some differences.
Rules
- 1 Use
const
for all of your references. Do not use var. e.x.
// badvar myVariable = 1; // goodconst myVariable = 2;
- 2 If you need to reassign a variable then use
let
instead.
// badvar myVariable = 1;iftrue myVariable = 2; // goodlet myVariable = 1;iftrue myVariable = 2;
- 3 Use the literal syntax for object creation.
// badconst myObject = ; // goodconst myObject = {};
- 4 Use object shorthand.
// badconst value = 1;const myObject = value: value; // goodconst value = 1;const myObject = value;
- 5 Only quote object keys that are invalid identifiers.
// badconst myObject = 'key': 1 'key-one': 2; // goodconst myObject = key: 1 'key-one': 2;
- 6 Use the spread operator over
Object.assign
.
// badconst original = a: 1 b: 2 ;const copy = Object; // goodconst copy = ...original c: 3 ;
- 7 Use the literal syntax for creating new arrays.
// badconst myArray = ; // goodconst myArray = ;
- 8 Use
return
statements in array method callbacks.
// bad - memo is undefined after the first callback1 2 3; // good1 2 3;
- 9 Use object destructuring when using multiple properties from an object.
// badconst fullName = { const firstName = userfirstName; const lastName = userlastName; return ` `;}; // goodconst fullName = ` `;
- 10 Use array destructuring when you want values from specific positions in an array.
// badconst myArray = 1 2 3;const first = myArray0;const second = myArray1; // goodconst myArray = 1 2 3;const first second = myArray;
- 11 Use single quotes for strings
// badconst name = "Pikachu"; // badconst name = `Pikachu`; // goodconst name = 'Pikachu';
- 12 Use template literals to build up strings instead of concatenation.
// badconst greeter = { return 'Hello ' + name;}; // goodconst greeter = { return `Hello `;};
- 13 Do not unnecessarily escape characters in strings.
// badconst example = '\'this\' is \"quoted\"'; // goodconst example = '\this\' is "quoted"';
- 14 Do not use the Function constructor to create a new Function - This has the potential to open vulnerabilities.
// badconst addOne = 'a' 'return a + 1'; // still badconst addOne = Function'a' 'return a + 1';
- 15 Use spacing around parens and before blocks.
// badconst a = {};const b = {};const c = {}; //goodconst a = {};const b = {};
- 16 Do not reassign parameters - you can mutate the values in the original caller leading to unwanted side-effects.
// bad { obja = 2;}; // good { const key = ObjectprototypehasOwnProperty ? obja : 1; // ...}; // bad { if!a a = 1; // ...}; // good { const b = a || 1; // ...}; { // ...};
- 17 Use arrow functions when you need to use an anonymous function.
// bad1 2 3; // good1 2 3;
- 18 For functions that return a single expression without side-effects then use the implicit return.
// bad1 2 3; //good1 2 3; //good1 2 3;
- 19 When an arrow function only has one argument and does not have a block drop the parantheses.
// badconst a = 1 2 3; // goodconst a = 1 2 3; // badconst a = 1 2 3; // goodconst a = 1 2 3;
- 20 No confusing arrow syntax without comparison ops.
// badconst itemHeight = itemheight > 256 ? itemlargeSize: itemsmallSize; // goodconst itemHeight = itemheight > 256 ? itemlargeSize: itemsmallSize
- 21 Use import/export over non-standard module systems. Modules are the future standard.
// badconst Seedrs = ;moduleexports = Abc; // good; ;
- 22 Only import from a path once group all of you imports from a single path.
// bad;; // good;;
- 23 Do not export mutable values
// badlet foo = 3;; // goodconst foo = 3;;
- 24 When a module only has a single export make that export the default.
// badconst foo = {}; // good {};
- 25 Place
import
statements at the top of the file.
// bad;;; // good;; ;
- 26 Generator star spacing
// bad { 'a';} // good { 'a';}
- 27 Use dot notation to access object properties. Except when accessing properties with a variable.
const user = firstName: 'Arnold' lastName: 'Schwarzenegger' awesome: true; // badconst isAwesome = user'awesome'; // goodconst key = 'firstName';const isAwesome = userawesome;const firstName = userkey;
- 28 Always use the type comparision operator.
const a = 1;const b = '1'; // badifa == b // ... // goodifa === b // ...
- 29 Always have space before blocks
// bad if a ; {} for ;; ; try {} catcha{} {} // goodif a ; if a ; else /*no error. this is checked by `keyword-spacing` rule.*/ ; {} for ;; ; try {} catcha {}
- 30 Always have spaces around keywords
// badifa === b // ... // goodif a === b // ... else // ...
- 31 Add space around infix operators
// bada+b a+ b a +b // gooda + b
- 32 Add a newline at the end of a file
// badconst a = 'a'; // goodconst a = 'a';\n
- 33 Enforce consistent spacing inside brackets
// badconst arr = 'foo' 'bar' ;const arr = 'foo' 'bar' ;const x y = z;const xy = z;const x ...y = z;const x = z; // goodconst arr = ;const arr = 'foo' 'bar' 'baz';const arr = 'foo' 'bar'; const x y = z;const xy = z;const x ...y = z;const x = z;
- 34 Always use spaces inside object definitions.
// badconst obj = 'foo': 'bar';const obj = 'foo': 'bar' ; // goodconst obj = {};const obj = 'foo': 'bar' ;
-
35 Split lines over 100 characters long onto multiple lines.
-
36 Add commas after and on the same line as an array element, object property.
// badconst arr = '1' '2'; // goodconst arr = '1' '2';
- 37 Do not have dangling commas in array and object definitions.
// badconst arr = '1' '2'; const obj = a: 'a' b: 'b'; // goodconst arr = '1' '2'; const obj = a: 'a' b: 'b'
- 38 Require semi colons
// badconst a = 1 // goodconst a = 1;