]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-unexpected-multiline.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-unexpected-multiline.md
1 ---
2 title: no-unexpected-multiline
3 layout: doc
4 rule_type: problem
5 related_rules:
6 - func-call-spacing
7 - semi
8 - space-unary-ops
9 ---
10
11
12
13 Semicolons are usually optional in JavaScript, because of automatic semicolon insertion (ASI). You can require or disallow semicolons with the [semi](./semi) rule.
14
15 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:
16
17 * 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 `,`.)
18 * The line is `--` or `++` (in which case it will decrement/increment the next token.)
19 * It is a `for()`, `while()`, `do`, `if()`, or `else`, and there is no `{`
20 * The next line starts with `[`, `(`, `+`, `*`, `/`, `-`, `,`, `.`, or some other binary operator that can only be found between two tokens in a single expression.
21
22 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.
23
24 ## Rule Details
25
26 This rule disallows confusing multiline expressions where a newline looks like it is ending a statement, but is not.
27
28 Examples of **incorrect** code for this rule:
29
30 ::: incorrect
31
32 ```js
33 /*eslint no-unexpected-multiline: "error"*/
34
35 var foo = bar
36 (1 || 2).baz();
37
38 var hello = 'world'
39 [1, 2, 3].forEach(addNumber);
40
41 let x = function() {}
42 `hello`
43
44 let x = function() {}
45 x
46 `hello`
47
48 let x = foo
49 /regex/g.test(bar)
50 ```
51
52 :::
53
54 Examples of **correct** code for this rule:
55
56 ::: correct
57
58 ```js
59 /*eslint no-unexpected-multiline: "error"*/
60
61 var foo = bar;
62 (1 || 2).baz();
63
64 var foo = bar
65 ;(1 || 2).baz()
66
67 var hello = 'world';
68 [1, 2, 3].forEach(addNumber);
69
70 var hello = 'world'
71 void [1, 2, 3].forEach(addNumber);
72
73 let x = function() {};
74 `hello`
75
76 let tag = function() {}
77 tag `hello`
78 ```
79
80 :::
81
82 ## When Not To Use It
83
84 You can turn this rule off if you are confident that you will not accidentally introduce code like this.
85
86 Note that the patterns considered problems are **not** flagged by the [semi](semi) rule.