The only reason few people are aware that you only need semis before lines beginning with ( and [ is because instead of educate people about the actual language all we seem to do is spread misinformation and FUD about semicolons in ES. It's so incredibly easy. That anyone thinks it's a lot to remember is utterly ridiculous. Doubly so in the context of software development. 1 measly rule, that's it.
Instead of saying "Sure use semis if you want, but be aware of ASI and that it may surprise you. Or leave them out except on lines beginning with ( and [, and be aware that ASI may surprise you sometimes. The canonical surprise cited is return\n{ but in practice nobody actually writes code like that so take the warning with a grain of salt.", we instead support this theory.
because instead of educate people about the actual language all we seem to do is spread misinformation and FUD about semicolons in ES. It's so incredibly easy.
It's easy? Let me quote the specification for you:
Rules of Automatic Semicolon Insertion
When, as the program is parsed from left to right, a token (called the offending token) is encountered that is not allowed by any production of the grammar, then a semicolon is automatically inserted before the offending token if one or more of the following conditions is true:
The offending token is separated from the previous token by at least one LineTerminator.
The offending token is }.
When, as the program is parsed from left to right, the end of the input stream of tokens is encountered and the parser is unable to parse the input token stream as a single complete ECMAScript Program, then a semicolon is automatically inserted at the end of the input stream.
When, as the program is parsed from left to right, a token is encountered that is allowed by some production of the grammar, but the production is a restricted production and the token would be the first token for a terminal or nonterminal immediately following the annotation “[no LineTerminator here]” within the restricted production (and therefore such a token is called a restricted token), and the restricted token is separated from the previous token by at least one LineTerminator, then a semicolon is automatically inserted before the restricted token.
However, there is an additional overriding condition on the preceding rules: a semicolon is never inserted automatically if the semicolon would then be parsed as an empty statement or if that semicolon would become one of the two semicolons in the header of a for statement (section 12.6.3).
It's also not easy to use. Some of the rules are completely backwards. Easy to use would be: "a newline outside of braces or parentheses ends a statement unless the newline was escaped with a backslash. Alternatively a semicolon might be used to end a statement."
Instead JavaScript is specified as: "semicolons are mandatory. However if you forget, keep the following 6 special syntax rules in mind and under the following conditions we will add one for you". That's just not very friendly for developers.
0
u/sjs Feb 06 '11
The only reason few people are aware that you only need semis before lines beginning with ( and [ is because instead of educate people about the actual language all we seem to do is spread misinformation and FUD about semicolons in ES. It's so incredibly easy. That anyone thinks it's a lot to remember is utterly ridiculous. Doubly so in the context of software development. 1 measly rule, that's it.
Instead of saying "Sure use semis if you want, but be aware of ASI and that it may surprise you. Or leave them out except on lines beginning with ( and [, and be aware that ASI may surprise you sometimes. The canonical surprise cited is return\n{ but in practice nobody actually writes code like that so take the warning with a grain of salt.", we instead support this theory.