Skip to content

Commit 18a3d92

Browse files
mauricedblydell
authored andcommitted
Add lines-around-comment as a special rule (#33)
1 parent 0bfb266 commit 18a3d92

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

README.md

+62-1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,65 @@ Example configuration:
163163
}
164164
```
165165

166+
### [lines-around-comment]
167+
168+
**This rule can be used with certain options.**
169+
170+
This rule requires empty lines before and/or after comments. Prettier preserves
171+
blank lines, with two exceptions:
172+
173+
- Several blank lines in a row are collapsed into a single blank line. This is
174+
fine.
175+
- Blank lines at the beginning and end of blocks, objects and arrays are always
176+
removed. This may lead to conflicts.
177+
178+
By default, ESLint requires a blank line above the comment is this case:
179+
180+
```js
181+
if (result) {
182+
183+
/* comment */
184+
return result;
185+
}
186+
```
187+
188+
However, Prettier removes the blank line:
189+
190+
```js
191+
if (result) {
192+
/* comment */
193+
return result;
194+
}
195+
```
196+
197+
If you like this rule, it can be used just fine with Prettier as long as add
198+
extra configuration to allow comments at the start and end of blocks, objects
199+
and arrays.
200+
201+
Example configuration:
202+
203+
```json
204+
{
205+
"rules": {
206+
"lines-around-comment": [
207+
"error",
208+
{
209+
"beforeBlockComment": true,
210+
"afterBlockComment": true,
211+
"beforeLineComment": true,
212+
"afterLineComment": true,
213+
"allowBlockStart": true,
214+
"allowBlockEnd": true,
215+
"allowObjectStart": true,
216+
"allowObjectEnd": true,
217+
"allowArrayStart": true,
218+
"allowArrayEnd": true
219+
}
220+
]
221+
}
222+
}
223+
```
224+
166225
### [max-len]
167226

168227
**This rule requires special attention when writing code.**
@@ -319,6 +378,7 @@ Example configuration:
319378
}
320379
```
321380

381+
322382
## Contributing
323383

324384
eslint-config-prettier has been tested with:
@@ -391,17 +451,18 @@ several other npm scripts:
391451

392452
[MIT](LICENSE).
393453

454+
[Prettier]: https://github.com/prettier/prettier
394455
[curly]: https://eslint.org/docs/rules/curly
395456
[eslint-config-airbnb]: https://www.npmjs.com/package/eslint-config-airbnb
396457
[eslint-plugin-flowtype]: https://github.com/gajus/eslint-plugin-flowtype
397458
[eslint-plugin-prettier]: https://github.com/prettier/eslint-plugin-prettier
398459
[eslint-plugin-react]: https://github.com/yannickcr/eslint-plugin-react
399460
[eslint-plugin-standard]: https://github.com/xjamundx/eslint-plugin-standard
461+
[lines-around-comment]: https://eslint.org/docs/rules/lines-around-comment
400462
[max-len]: https://eslint.org/docs/rules/max-len
401463
[no-confusing-arrow]: https://eslint.org/docs/rules/no-confusing-arrow
402464
[no-mixed-operators]: https://eslint.org/docs/rules/no-mixed-operators
403465
[no-tabs]: https://eslint.org/docs/rules/no-tabs
404-
[Prettier]: https://github.com/prettier/prettier
405466
[quotes]: https://eslint.org/docs/rules/quotes
406467
[travis-badge]: https://travis-ci.org/prettier/eslint-config-prettier.svg?branch=master
407468
[travis]: https://travis-ci.org/prettier/eslint-config-prettier

bin/validators.js

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ module.exports = {
1010
return firstOption !== "multi-line" && firstOption !== "multi-or-nest";
1111
},
1212

13+
"lines-around-comment"(options) {
14+
if (options.length < 1) {
15+
return false;
16+
}
17+
18+
const firstOption = options[0];
19+
return Boolean(
20+
firstOption &&
21+
firstOption.allowBlockStart &&
22+
firstOption.allowBlockEnd &&
23+
firstOption.allowObjectStart &&
24+
firstOption.allowObjectEnd &&
25+
firstOption.allowArrayStart &&
26+
firstOption.allowArrayEnd
27+
);
28+
},
29+
1330
"no-confusing-arrow"(options) {
1431
if (options.length < 1) {
1532
return true;

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
// information. (These are marked with `0` instead of `"off"` so that a
77
// script can distinguish them.)
88
curly: 0,
9+
"lines-around-comment": 0,
910
"max-len": 0,
1011
"no-confusing-arrow": 0,
1112
"no-mixed-operators": 0,

test/cli.js

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ test(
136136
"quotes",
137137
"arrow-parens",
138138
"no-tabs",
139+
"lines-around-comment",
139140
"no-mixed-operators",
140141
["curly", "multi-or-nest", "consistent"],
141142
["no-confusing-arrow", { allowParens: true }],
@@ -154,6 +155,7 @@ test(
154155
https://github.com/prettier/eslint-config-prettier#special-rules
155156
156157
- curly
158+
- lines-around-comment
157159
- no-confusing-arrow
158160
- quotes
159161

test/validators.js

+33
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,39 @@ test("curly", t => {
2222
);
2323
});
2424

25+
test("lines-around-comment", t => {
26+
t.false(validators["lines-around-comment"]([]), "no options disallowed");
27+
t.true(
28+
validators["lines-around-comment"]([
29+
{
30+
allowBlockStart: true,
31+
allowBlockEnd: true,
32+
allowObjectStart: true,
33+
allowObjectEnd: true,
34+
allowArrayStart: true,
35+
allowArrayEnd: true
36+
}
37+
]),
38+
"allowing block/object/array start/end allowed"
39+
);
40+
t.false(
41+
validators["lines-around-comment"]([
42+
{
43+
allowBlockEnd: true,
44+
allowObjectStart: true,
45+
allowObjectEnd: true,
46+
allowArrayStart: true,
47+
allowArrayEnd: true
48+
}
49+
]),
50+
"missing one of the options disallowed"
51+
);
52+
t.false(
53+
validators["lines-around-comment"]([null]),
54+
"does not crash on bad input"
55+
);
56+
});
57+
2558
test("no-confusing-arrow", t => {
2659
t.true(validators["no-confusing-arrow"]([]), "no options allowed");
2760
t.true(

0 commit comments

Comments
 (0)