tc39/proposal-function.sent

function.sent as an expression statement.

Opened this issue · 3 comments

Hi, while implementing function.sent support in Babel I got a doubt: is this code valid?

function* foo() {
  function.sent;
}

The current specification disallows it (an ExpressionStatement can't start with function):
https://tc39.github.io/ecma262/#prod-ExpressionStatement

ExpressionStatement[Yield, Await]:
  [lookahead ∉ { {, function, async [no LineTerminator here] function, class, let [ }] Expression[+In, ?Yield, ?Await] ;

I couldn't find in this proposal a relaxation of that grammar, but I don't see why it should be disallowed.

Good catch.

Yes, it seems reasonable that an expression statement could start with function . sent. For example, that would be useful for making recursive function calls. So, you should consider that to be part of the proposal.

But, actually specify that via the current ES grammar notation looks like it may be tricky and might even require extending the notation. FunctionDeclaration probably will need a [lookahead ∉ { function . }] guard, but that's not enough. We also need to find a way for ExpressionStatement to express a conditional multi-token lookahead that will allow function .. Maybe something like:

ExpressionStatement[Yield, Await]:
  [lookahead ∉ { {, function [lookahead ∉ { . }], async [no LineTerminator here] function, class, let [ }] Expression[+In, ?Yield, ?Await] ;

What about using positive lookahead?

ExpressionStatement[Yield, Await]:
  [lookahead ∈ { function . }] Expression[+In, ?Yield, ?Await] ;
  [lookahead ∉ { {, function, async [no LineTerminator here] function, class, let [ }] Expression[+In, ?Yield, ?Await] ;

yes, adding positive lookahead to the notation is probably the better way to go