Within a generator body Arrow function formals treat yield as keyword but function declarations and expressions do not

Ian Halliday Ian.Halliday at microsoft.com
Fri Mar 27 18:45:47 UTC 2015


Is this intentional?

14.1 Function Definitions
---
FunctionDeclaration[Yield, Default]  :
function BindingIdentifier[?Yield] ( FormalParameters ) { FunctionBody }
[+Default] function ( FormalParameters ) { FunctionBody }

FunctionExpression :
function BindingIdentifieropt ( FormalParameters ) { FunctionBody }
---

14.2 Arrow Function Definitions
---
When the production
	ArrowParameters[Yield]  : CoverParenthesizedExpressionAndArrowParameterList[?Yield]
is recognized the following grammar is used to refine the interpretation of CoverParenthesizedExpressionAndArrowParameterList:
ArrowFormalParameters[Yield, GeneratorParameter] :
 ( StrictFormalParameters[?Yield, ?GeneratorParameter] )
---

Following the production rules for StrictFormalParameters and FormalParameters you can end up at SingleNameBinding

SingleNameBinding[Yield, GeneratorParameter]  :
[+GeneratorParameter] BindingIdentifier[Yield] Initializer[In]opt
[~GeneratorParameter] BindingIdentifier[?Yield] Initializer[In, ?Yield]opt

This looks like it suggests following:

var yield;
function* gf() {
    function f(yield = yield) { } // valid parse, both yields are identifiers, second binds to the global var
    var f = function (yield = yield) { }; // same deal
    var a = (yield = yield) => { }; // first yield is treated as keyword, doesn't parse, second is treated as identifier and again binds to the global var
}

Binding to the global var in the default argument expressions is weird and perhaps confusing but acceptable I suppose.

The inconsistency of the formal name treatment between functions and arrow functions feels wrong to me given that the formal names are created in non-generator scopes.

Ian


More information about the es-discuss mailing list