6 - no-unexpected-multiline
9 - https://blog.izs.me/2010/12/an-open-letter-to-javascript-leaders-regarding/
10 - https://web.archive.org/web/20200420230322/http://inimino.org/~inimino/blog/javascript_semicolons
15 JavaScript doesn't require semicolons at the end of each statement. In many cases, the JavaScript engine can determine that a semicolon should be in a certain spot and will automatically add it. This feature is known as **automatic semicolon insertion (ASI)** and is considered one of the more controversial features of JavaScript. For example, the following lines are both valid:
19 var website = "eslint.org";
22 On the first line, the JavaScript engine will automatically insert a semicolon, so this is not considered a syntax error. The JavaScript engine still knows how to interpret the line and knows that the line end indicates the end of the statement.
24 In the debate over ASI, there are generally two schools of thought. The first is that we should treat ASI as if it didn't exist and always include semicolons manually. The rationale is that it's easier to always include semicolons than to try to remember when they are or are not required, and thus decreases the possibility of introducing an error.
26 However, the ASI mechanism can sometimes be tricky to people who are using semicolons. For example, consider this code:
35 This may look like a `return` statement that returns an object literal, however, the JavaScript engine will interpret this code as:
44 Effectively, a semicolon is inserted after the `return` statement, causing the code below it (a labeled literal inside a block) to be unreachable. This rule and the [no-unreachable](no-unreachable) rule will protect your code from such cases.
46 On the other side of the argument are those who say that since semicolons are inserted automatically, they are optional and do not need to be inserted manually. However, the ASI mechanism can also be tricky to people who don't use semicolons. For example, consider this code:
49 var globalCounter = { }
53 globalCounter.increment = function () {
59 In this example, a semicolon will not be inserted after the first line, causing a run-time error (because an empty object is called as if it's a function). The [no-unexpected-multiline](no-unexpected-multiline) rule can protect your code from such cases.
61 Although ASI allows for more freedom over your coding style, it can also make your code behave in an unexpected way, whether you use semicolons or not. Therefore, it is best to know when ASI takes place and when it does not, and have ESLint protect your code from these potentially unexpected cases. 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:
63 1. 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 `,`.)
64 1. The line is `--` or `++` (in which case it will decrement/increment the next token.)
65 1. It is a `for()`, `while()`, `do`, `if()`, or `else`, and there is no `{`
66 1. The next line starts with `[`, `(`, `+`, `*`, `/`, `-`, `,`, `.`, or some other binary operator that can only be found between two tokens in a single expression.
70 This rule enforces consistent use of semicolons.
74 This rule has two options, a string option and an object option.
78 * `"always"` (default) requires semicolons at the end of statements
79 * `"never"` disallows semicolons at the end of statements (except to disambiguate statements beginning with `[`, `(`, `/`, `+`, or `-`)
81 Object option (when `"always"`):
83 * `"omitLastInOneLineBlock": true` disallows the last semicolon in a block in which its braces (and therefore the content of the block) are in the same line
84 * `"omitLastInOneLineClassBody": true` disallows the last semicolon in a class body in which its braces (and therefore the content of the class body) are in the same line
86 Object option (when `"never"`):
88 * `"beforeStatementContinuationChars": "any"` (default) ignores semicolons (or lacking semicolon) at the end of statements if the next line starts with `[`, `(`, `/`, `+`, or `-`.
89 * `"beforeStatementContinuationChars": "always"` requires semicolons at the end of statements if the next line starts with `[`, `(`, `/`, `+`, or `-`.
90 * `"beforeStatementContinuationChars": "never"` disallows semicolons at the end of statements if it doesn't make ASI hazard even if the next line starts with `[`, `(`, `/`, `+`, or `-`.
92 **Note:** `beforeStatementContinuationChars` does not apply to class fields because class fields are not statements.
96 Examples of **incorrect** code for this rule with the default `"always"` option:
101 /*eslint semi: ["error", "always"]*/
105 object.method = function() {
116 Examples of **correct** code for this rule with the default `"always"` option:
121 /*eslint semi: "error"*/
125 object.method = function() {
136 #### omitLastInOneLineBlock
138 Examples of additional **correct** code for this rule with the `"always", { "omitLastInOneLineBlock": true }` options:
143 /*eslint semi: ["error", "always", { "omitLastInOneLineBlock": true}] */
147 if (foo) { bar(); baz() }
149 function f() { bar(); baz() }
152 foo() { bar(); baz() }
154 static { bar(); baz() }
160 #### omitLastInOneLineClassBody
162 Examples of additional **correct** code for this rule with the `"always", { "omitLastInOneLineClassBody": true }` options:
167 /*eslint semi: ["error", "always", { "omitLastInOneLineClassBody": true}] */
169 export class SomeClass{
171 console.log(this.type);
172 console.log(this.anotherType);
176 export class Variant1 extends SomeClass{type=1}
177 export class Variant2 extends SomeClass{type=2; anotherType=3}
184 Examples of **incorrect** code for this rule with the `"never"` option:
189 /*eslint semi: ["error", "never"]*/
193 object.method = function() {
204 Examples of **correct** code for this rule with the `"never"` option:
209 /*eslint semi: ["error", "never"]*/
213 object.method = function() {
240 #### beforeStatementContinuationChars
242 Examples of additional **incorrect** code for this rule with the `"never", { "beforeStatementContinuationChars": "always" }` options:
247 /*eslint semi: ["error", "never", { "beforeStatementContinuationChars": "always"}] */
257 Examples of additional **incorrect** code for this rule with the `"never", { "beforeStatementContinuationChars": "never" }` options:
262 /*eslint semi: ["error", "never", { "beforeStatementContinuationChars": "never"}] */
272 ## When Not To Use It
274 If you do not want to enforce semicolon usage (or omission) in any particular way, then you can turn this rule off.