Skip to content

Commit 387e0f2

Browse files
sudo-suhaslydell
authored andcommitted
Add no-unexpected-multiline as a special rule (#34)
1 parent 230d595 commit 387e0f2

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

+70
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,75 @@ Example configuration:
352352
}
353353
```
354354

355+
### [no-unexpected-multiline]
356+
357+
**This rule requires special attention when writing code.**
358+
359+
This rule disallows confusing multiline expressions where a newline looks like
360+
it is ending a statement, but is not.
361+
362+
For example, the rule could warn about this:
363+
364+
```js
365+
var hello = "world"
366+
[1, 2, 3].forEach(addNumber)
367+
```
368+
369+
Prettier usually formats this in a way that makes it obvious that a semicolon
370+
was missing:
371+
372+
```js
373+
var hello = "world"[(1, 2, 3)].forEach(addNumber);
374+
```
375+
376+
However, there are cases where Prettier breaks things into several lines such
377+
that the `no-unexpected-multiline` conflicts.
378+
379+
```js
380+
const value = text.trim().split("\n")[position].toLowerCase();
381+
```
382+
383+
Prettier breaks it up into several lines, though, causing a conflict:
384+
385+
```js
386+
const value = text
387+
.trim()
388+
.split("\n")
389+
[position].toLowerCase();
390+
```
391+
392+
If you like this rule, it can usually be used with Prettier without problems,
393+
but occasionally you might need to either temporarily disable the rule or
394+
refactor your code.
395+
396+
```js
397+
const value = text
398+
.trim()
399+
.split("\n")
400+
// eslint-disable-next-line no-unexpected-multiline
401+
[position].toLowerCase();
402+
403+
// Or:
404+
405+
const lines = text.trim().split("\n");
406+
const value = lines[position].toLowerCase();
407+
```
408+
409+
**Note:** If you _do_ enable this rule, you have to run ESLint and Prettier as
410+
two separate steps (and ESLint first) in order to get any value out of it.
411+
Otherwise Prettier might reformat your code in such a way that ESLint never gets
412+
a chance to report anything (as seen in the first example).
413+
414+
Example configuration:
415+
416+
```json
417+
{
418+
"rules": {
419+
"no-unexpected-multiline": "error"
420+
}
421+
}
422+
```
423+
355424
### [quotes]
356425

357426
**This rule requires certain options.**
@@ -454,6 +523,7 @@ several other npm scripts:
454523
[no-confusing-arrow]: https://eslint.org/docs/rules/no-confusing-arrow
455524
[no-mixed-operators]: https://eslint.org/docs/rules/no-mixed-operators
456525
[no-tabs]: https://eslint.org/docs/rules/no-tabs
526+
[no-unexpected-multiline]: https://eslint.org/docs/rules/no-unexpected-multiline
457527
[quotes]: https://eslint.org/docs/rules/quotes
458528
[travis-badge]: https://travis-ci.org/prettier/eslint-config-prettier.svg?branch=master
459529
[travis]: https://travis-ci.org/prettier/eslint-config-prettier

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
"no-confusing-arrow": 0,
1212
"no-mixed-operators": 0,
1313
"no-tabs": 0,
14+
"no-unexpected-multiline": 0,
1415
quotes: 0,
1516
// The rest are rules that you never need to enable when using Prettier.
1617
"array-bracket-newline": "off",

test/cli.js

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ test(
137137
"arrow-parens",
138138
"no-tabs",
139139
"lines-around-comment",
140+
"no-unexpected-multiline",
140141
"no-mixed-operators",
141142
["curly", "multi-or-nest", "consistent"],
142143
["no-confusing-arrow", { allowParens: true }],
@@ -165,6 +166,7 @@ test(
165166
- max-len
166167
- no-mixed-operators
167168
- no-tabs
169+
- no-unexpected-multiline
168170
`,
169171
2
170172
);

0 commit comments

Comments
 (0)