Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is CoverInitializedName[yield] in ECMA-2015 syntax grammar

I'm writting my own ES6 parser. But I don't understand what CoverInitializedName is in ObjectLiteral.

In the section about this in the ECMA-2015 ObjectLiteral spec I see

PropertyDefinition[Yield] :
    IdentifierReference[?Yield]
    CoverInitializedName[?Yield] <-- this is what I dont understand
    PropertyName[?Yield] : AssignmentExpression[In, ?Yield]
    MethodDefinition[?Yield]

Then I look up the definition of CoverInitializedName.

CoverInitializedName[Yield] :
    IdentifierReference[?Yield] Initializer[In, ?Yield]

IdentifierReference[Yield] :
    Identifier
    [~Yield] yield

Initializer[In, Yield] :
    = AssignmentExpression[?In, ?Yield]

Initializer starts with = sign.

Which means I can assign property using assign operator like this.

let o = { prop = value };

If I execute this code it will throw SyntaxError: Invalid shorthand property initializer

I confused and look in the MDN Object initializer docs. There is no such thing. So what is this CoverInitializedName?

[EDIT]

loganfsmyth's answer was

({ prop = value } = {}); // valid destructuring

In this script, the left-hand side { prop = value } is not an ObjectLiteral. Which is ObjectBindingPattern assigning default value 13.3.3 Destructuring Binding Patterns. I think ObjectLiteral is rValue isn't it? So my question remains: what is CoverInitializedName in ObjectLiteral? Or did I misunderstand something?

like image 514
jeefo Avatar asked Aug 21 '19 02:08

jeefo


Video Answer


1 Answers

You are right that let o = { prop = value }; is a syntax error. The key thing to take into account however is that there are other cases where it is allowed, depending on the context. For instance

({ prop = value }); // syntax error
({ prop = value } = {}); // valid destructuring

Since you cannot know whether prop = value is allowed until you finish parsing the object and reach the =, the CoverInitializedName rule is required to parse this code. If you look further down in the section you linked, you'll see:

NOTE 3: In certain contexts, ObjectLiteral is used as a cover grammar for a more restricted secondary grammar. The CoverInitializedName production is necessary to fully cover these secondary grammars. However, use of this production results in an early Syntax Error in normal contexts where an actual ObjectLiteral is expected.

which attempts to answer just the question you are asking.

In the case of an Object literal, like the snippet you posted, and the syntax error case I posted, the early error that applies is 12.2.6.1 Static Semantics: Early Errors which states

PropertyDefinition : CoverInitializedName

  Always throw a Syntax Error if code matches this production.

whereas ({ prop = value} = {}) will parse successfully based on the grammar defined in 12.14.5 Destructuring Assignment which has

AssignmentProperty[Yield] : 
  IdentifierReference[?Yield] Initializer[In,?Yield]opt
  PropertyName : AssignmentElement[?Yield]

which does allow for prop = value.

like image 166
loganfsmyth Avatar answered Oct 20 '22 01:10

loganfsmyth