Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 64d7cad

Browse files
author
Marlow Payne
committed
Add eslint rule and doc for trailing commas
1 parent 2ee4826 commit 64d7cad

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

javascript/.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@
110110
"semi": 2,
111111

112112
// Disallow function definitions of the form 'function myFunc() {'
113-
"func-style": [2, "expression"]
113+
"func-style": [2, "expression"],
114+
115+
// Enforce trailing comma style
116+
"comma-style": [2, "last"]
114117
}
115118
}

javascript/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ a = b;
105105
(f());
106106
```
107107

108+
##Use trailing commas
109+
110+
For multi-line lists or object properties place the commas separating items at the end of the line of the previous item.
111+
112+
```javascript
113+
// bad
114+
var foo = ["apples"
115+
, "oranges"
116+
, "bananas"];
117+
118+
var foo = {
119+
"fruit": "apple"
120+
, "vegetable": "arugula"
121+
};
122+
123+
// good
124+
var foo = ["apples",
125+
"oranges",
126+
"bananas"];
127+
128+
var foo = {
129+
"fruit": "apple",
130+
"vegetable": "arugula"
131+
};
132+
```
133+
108134
##Use function expressions over function declarations
109135

110136
The function expression is clearly recognisable as what it really is (a variable with a function value). Additionally, it helps organize code so that all variable declarations appear at the top of a file, and invocations follow. This gives some predictablity when others are reading your code, allowing for a more consistent structure.

0 commit comments

Comments
 (0)