Today, I modified the previous JS syntax analysis and reviewed the rules for automatically inserting semicolons. Probably as follows (ecma262 V5, 7.9 ):
1. WhenCodeDuring parsing, an invalid token is found (this token is called an offending token at this time). In the following cases, a semicolon is automatically inserted:
- There is a line break before the token
- This token is}
2. An unexpected end will be inserted with a semicolon.
3. A valid token appears, but this location is in a special statement environment (in the ecma262 syntax description, this location is called [No lineterminator here]), and there is a line break before this token, in this case, a semicolon is inserted after the previous token.
Postfixexpression:
Lefthandsideexpression [No lineterminator here] ++
Lefthandsideexpression [No lineterminator here] --
Continuestatement:
Continue [No lineterminator here];
Breakstatement:
Break [No lineterminator here];
Returnstatement:
Return [No lineterminator here] expression;
Throwstatement:
Throw [No lineterminator here] expression;
HoweverIf the inserted Semicolon is an empty statement or a part of the for header, the semicolon is not inserted.
Finally, although the JS engine will automatically insert a semicolon, it is best not to use this feature. Instead, write the semicolon in full, so there are not many.