]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # disallow confusing multiline expressions (no-unexpected-multiline) |
2 | ||
3 | Semicolons are usually optional in JavaScript, because of automatic semicolon insertion (ASI). You can require or disallow semicolons with the [semi](./semi.md) rule. | |
4 | ||
5 | The rules for ASI are relatively straightforward: As once described by Isaac Schlueter, a newline character always ends a statement, just like a semicolon, **except** where one of the following is true: | |
6 | ||
7 | * The statement has an unclosed paren, array literal, or object literal or ends in some other way that is not a valid way to end a statement. (For instance, ending with `.` or `,`.) | |
8 | * The line is `--` or `++` (in which case it will decrement/increment the next token.) | |
9 | * It is a `for()`, `while()`, `do`, `if()`, or `else`, and there is no `{` | |
10 | * The next line starts with `[`, `(`, `+`, `*`, `/`, `-`, `,`, `.`, or some other binary operator that can only be found between two tokens in a single expression. | |
11 | ||
12 | In the exceptions where a newline does **not** end a statement, a typing mistake to omit a semicolon causes two unrelated consecutive lines to be interpreted as one expression. Especially for a coding style without semicolons, readers might overlook the mistake. Although syntactically correct, the code might throw exceptions when it is executed. | |
13 | ||
14 | ## Rule Details | |
15 | ||
16 | This rule disallows confusing multiline expressions where a newline looks like it is ending a statement, but is not. | |
17 | ||
18 | Examples of **incorrect** code for this rule: | |
19 | ||
20 | ```js | |
21 | /*eslint no-unexpected-multiline: "error"*/ | |
22 | ||
23 | var foo = bar | |
24 | (1 || 2).baz(); | |
25 | ||
26 | var hello = 'world' | |
27 | [1, 2, 3].forEach(addNumber); | |
28 | ||
29 | let x = function() {} | |
30 | `hello` | |
31 | ||
32 | let x = function() {} | |
33 | x | |
34 | `hello` | |
35 | ||
36 | let x = foo | |
37 | /regex/g.test(bar) | |
38 | ``` | |
39 | ||
40 | Examples of **correct** code for this rule: | |
41 | ||
42 | ```js | |
43 | /*eslint no-unexpected-multiline: "error"*/ | |
44 | ||
45 | var foo = bar; | |
46 | (1 || 2).baz(); | |
47 | ||
48 | var foo = bar | |
49 | ;(1 || 2).baz() | |
50 | ||
51 | var hello = 'world'; | |
52 | [1, 2, 3].forEach(addNumber); | |
53 | ||
54 | var hello = 'world' | |
55 | void [1, 2, 3].forEach(addNumber); | |
56 | ||
57 | let x = function() {}; | |
58 | `hello` | |
59 | ||
60 | let tag = function() {} | |
61 | tag `hello` | |
62 | ``` | |
63 | ||
64 | ## When Not To Use It | |
65 | ||
66 | You can turn this rule off if you are confident that you will not accidentally introduce code like this. | |
67 | ||
68 | Note that the patterns considered problems are **not** flagged by the [semi](semi.md) rule. | |
69 | ||
70 | ## Related Rules | |
71 | ||
72 | * [func-call-spacing](func-call-spacing.md) | |
73 | * [semi](semi.md) | |
74 | * [space-unary-ops](space-unary-ops.md) |