proposal-function.sent icon indicating copy to clipboard operation
proposal-function.sent copied to clipboard

function.sent as an expression statement.

Open nicolo-ribaudo opened this issue 8 years ago • 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.

nicolo-ribaudo avatar Jul 07 '17 00:07 nicolo-ribaudo

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] ;

allenwb avatar Jul 07 '17 01:07 allenwb

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] ;

nicolo-ribaudo avatar Jul 07 '17 08:07 nicolo-ribaudo

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

allenwb avatar Jul 07 '17 17:07 allenwb