What are the criteria for automatic semicolon inse

2019-01-27 06:12发布

问题:

Possible Duplicate:
What are the rules for Javascript's automatic semicolon insertion?

JavaScript befuddles me with its implicit line termination. It's a very C-like language, except that ending lines in a semi-colon is often optional.

So how does it decide when to assume an end-of-line?

Consider this example:

var x = 1 + 2
-3 + 3 == 0 ? alert('0') : alert('3')

Punching that into an HTML file and opening it in Safari popped up 3. If you stick a semicolon on the end of the first line, it changes to 0.

The algorithms and logic are all straightforward; what interests me is by what criteria JavaScript decided, in this instance, not to assume an end-of-line after the first line. Is it that it only waits for an error scenario before assuming an EOL? Or is there a more definite criteria?

I'm very curious. I haven't researched this much; I want to see what the S/O community has to say about it. I always end my lines with semicolons anyway, but I have some JS compression code that trips on the semicolon issue from time to time when I inadvertently leave one out.

Edit

OK just to clarify what the actual question is here: Can anybody describe, in non-abstract terms, when JavaScript will and won't automatically insert semi-colons.

This is NOT a duplicate. I'm aware that the rules for autmoatic semicolon insertion are well established and concisely documented. They're also long winded and confusing because they are generally abstract. In my experience, high level programmers don't digest low level documentation as well as simple end results, which is what I'm looking for.

If no one is interested, then I'll have a crack at answering myself when I get a chance. In the meantime, please leave the guns in the holsters for a couple of days. Thanks.

回答1:

The ECMA specification (ch. 7.9.1, page 26) states:

There are three basic rules of semicolon insertion:

  1. 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 }.
  2. 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.
  3. 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.

I think this implementation has to do with the second point where:

var x = 1 + 2
-3 + 3 == 0 ? alert('0') : alert('3')

can be parsed as a single complete ECMAScript Program

Because it's not always clear how the parser will insert semi-colons, it's advisable to not leave it to the parser (i.e. always insert the semi-colons yourself).

In ch. 7.9.2 (Examples of Automatic Semicolon Insertion) of the same specs this example looks like your situation:

The source

a = b + c  
(d + e).print()

is not transformed by automatic semicolon insertion, because the parenthesised expression that begins the second line can be interpreted as an argument list for a function call:
a = b + c(d + e).print()



回答2:

Don't leave it to the compiler, always put semicolons at the end of statements.