Why quit limited production to automatically set semicolons?

Part of the JavaScript auto-install algorithm is the so-called "limited production". These are syntactic forms that prohibit the appearance of a newline at a specific point. To specify the ECMAScript 2015 specification:

If the phrase “[No LineTerminator here]” appears on the right side of the syntax grammar production, this indicates that the production is a limited production: it cannot be used if the LineTerminator stream is present at the input in the specified position.

In the ECMAScript 2015 specification, there are 10 limited productions:

  • PostfixExpression [Yield] : LeftHandSideExpression [? Yield] [no LineTerminator here] ++
  • PostfixExpression [Yield] : LeftHandSideExpression [? Yield] [no LineTerminator here] --
  • ContinueStatement [Yield] : continue [no LineTerminator here] LabelIdentifier [? Yield] ;
  • BreakStatement [Yield] : break [no LineTerminator here] LabelIdentifier [? Yield] ;
  • ReturnStatement [Yield] : return [no LineTerminator here] Expression ;
  • ReturnStatement [Yield] : return [no LineTerminator here] Expression [In ,? Yield] ;
  • ThrowStatement [Yield] : throw [no LineTerminator here] Expression [In ,? Yield] ;
  • ArrowFunction [In, Yield] : ArrowParameters [? Yield] [no LineTerminator here] => ConciseBody [? In] sub>
  • YieldExpression [In] : yield [no LineTerminator here] * Assignment allocation [? In, Yield]
  • YieldExpression [In] : yield [no LineTerminator here] AssignmentExpression [? In, Yield]

From these productions, I understand that most of them are limited. Production for PostfixExpression is limited to preventing disambiguation using PrefixExpression. ContinueStatement, BreakStatement and ReturnStatement are limited to production because there are corresponding derivatives where break and continue do not accept labels, and return does not accept expressions. I can’t say that I know enough about the functions of the arrows or I am developing expressions to know why they are limited, but I assume that this prevents some kind of similar intelligibility.

The production I don’t understand is ThrowExpression. As far as I can tell, there is no parsing parsing in using throw , as it is when using break , return and continue : after all, throw; JavaScript is not valid. I thought it might be for historical reasons, but as far as I can tell, throw; never allowed in any JavaScript specification.

The practical consequence of this is that, as with return , you cannot put an expression to be selected on the next line, for example. it is not right:

 throw new Error("some error"); 

However, unlike return , this has no other behavior than placing new Error() on the same line. This is just a syntax error: Chrome reports this as

Uncaught SyntaxError: illegal new line after throw

Is production for ThrowExpression limited only to ensure consistency with similar structures? Or is there some kind of ambiguity that I don't see?

+6
source share
3 answers

When throw was added to the language in 1998 or so, a discussion arose about whether the throw expression is required for the expression or not. (An alternative would be if throw without expression redraws the current exception object, as in some other languages .)

I cannot find any notes about this discussion or final resolution - although we know what the resolution is, it is mentioned in the Record of the meetings of TC39 at its meeting on February 19, 1998. I believe that the purpose of the restriction was to leave the syntax space open if the solution ever changes.

+4
source

We can only guess why the authors of the language decided to do it this way, but I would say that this corresponds to the return sequence. Both return ¶ value and throw ¶ value not allowed if you want to complete with value . That there would be no ambiguity for throw does not matter in any case.

+3
source

I think the adeneo comment is on the right track: for example return , the throw operand is usually a valid expression. Problem with return \n "valid on its own"; is one of the most common examples of ASI problems, so restricting return and any other statements that take this value is a reasonable guarantee.

Looking at the list, most of them are unary operators (or similar to one). They all follow the same general form, with the exception of the arrow functions. This form is the cause of ASI errors, so limiting the entire category is more predictable than just return .

+2
source

All Articles