]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-unexpected-multiline.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-unexpected-multiline.md
CommitLineData
eb39fafa
DC
1# disallow confusing multiline expressions (no-unexpected-multiline)
2
3Semicolons are usually optional in JavaScript, because of automatic semicolon insertion (ASI). You can require or disallow semicolons with the [semi](./semi.md) rule.
4
5The 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
12In 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
16This rule disallows confusing multiline expressions where a newline looks like it is ending a statement, but is not.
17
18Examples of **incorrect** code for this rule:
19
20```js
21/*eslint no-unexpected-multiline: "error"*/
22
23var foo = bar
24(1 || 2).baz();
25
26var hello = 'world'
27[1, 2, 3].forEach(addNumber);
28
29let x = function() {}
30`hello`
31
32let x = function() {}
33x
34`hello`
35
36let x = foo
37/regex/g.test(bar)
38```
39
40Examples of **correct** code for this rule:
41
42```js
43/*eslint no-unexpected-multiline: "error"*/
44
45var foo = bar;
46(1 || 2).baz();
47
48var foo = bar
49;(1 || 2).baz()
50
51var hello = 'world';
52[1, 2, 3].forEach(addNumber);
53
54var hello = 'world'
55void [1, 2, 3].forEach(addNumber);
56
57let x = function() {};
58`hello`
59
60let tag = function() {}
61tag `hello`
62```
63
64## When Not To Use It
65
66You can turn this rule off if you are confident that you will not accidentally introduce code like this.
67
68Note 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)