Avoid unexpected multiline expressions (no-unexpected-multiline)
The rules for ASI are relatively straightforward: In short, as once described by Isaac Schlueter, a
\n character always ends a statement (just like a semicolon) unless one of the following is true:
- 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
- The line is
++(in which case it will decrement/increment the next token.)
- It is a
else, and there is no
- The next line starts with
., or some other binary operator that can only be found between two tokens in a single expression.
This particular rule aims to spot scenarios where a newline looks like it is ending a statement, but is not.
This rule is aimed at ensuring that two unrelated consecutive lines are not accidentially interpreted as a single expression.
The following patterns are considered problems:
/*eslint no-unexpected-multiline: 2*/ var foo = bar (1 || 2).baz(); /*error Unexpected newline between function and ( of function call.*/ var hello = 'world' [1, 2, 3].forEach(addNumber); /*error Unexpected newline between object and [ of property access.*/
The following patterns are not considered problems:
/*eslint no-unexpected-multiline: 2*/ var foo = bar; (1 || 2).baz(); var foo = bar ;(1 || 2).baz() var hello = 'world'; [1, 2, 3].forEach(addNumber); var hello = 'world' void [1, 2, 3].forEach(addNumber);
When Not To Use It
You can turn this rule off if you are confident that you will not accidentally introduce code like this.
Note that the patterns considered problems are not flagged by the semi rule.
This rule was introduced in ESLint 0.24.0.