Bug 789239 - Unravel 30-line if condition in Parser.cpp. Part 2 - Rewrite IsValidForStatementLHS using early returns. r=Waldo.

This commit is contained in:
Jason Orendorff 2012-09-18 06:03:22 -05:00
parent 77138fa36b
commit a03774931c

View File

@ -2963,34 +2963,49 @@ Parser::matchInOrOf(bool *isForOfp)
static bool static bool
IsValidForStatementLHS(ParseNode *pn1, JSVersion version, bool forDecl, bool forEach, bool forOf) IsValidForStatementLHS(ParseNode *pn1, JSVersion version, bool forDecl, bool forEach, bool forOf)
{ {
return !(forDecl if (forDecl) {
? (pn1->pn_count > 1 || pn1->isOp(JSOP_DEFCONST) if (pn1->pn_count > 1)
return false;
if (pn1->isOp(JSOP_DEFCONST))
return false;
#if JS_HAS_DESTRUCTURING #if JS_HAS_DESTRUCTURING
|| (version == JSVERSION_1_7 && // In JS 1.7 only, for (var [K, V] in EXPR) has a special meaning.
!forEach && // Hence all other destructuring decls are banned there.
!forOf && if (version == JSVERSION_1_7 && !forEach && !forOf) {
(pn1->pn_head->isKind(PNK_OBJECT) || ParseNode *lhs = pn1->pn_head;
(pn1->pn_head->isKind(PNK_ARRAY) && if (lhs->isKind(PNK_ASSIGN))
pn1->pn_head->pn_count != 2) || lhs = lhs->pn_left;
(pn1->pn_head->isKind(PNK_ASSIGN) &&
(!pn1->pn_head->pn_left->isKind(PNK_ARRAY) || if (lhs->isKind(PNK_OBJECT))
pn1->pn_head->pn_left->pn_count != 2)))) return false;
if (lhs->isKind(PNK_ARRAY) && lhs->pn_count != 2)
return false;
}
#endif #endif
) return true;
: (!pn1->isKind(PNK_NAME) && }
!pn1->isKind(PNK_DOT) &&
switch (pn1->getKind()) {
case PNK_NAME:
case PNK_DOT:
case PNK_CALL:
case PNK_XMLUNARY:
case PNK_ELEM:
return true;
#if JS_HAS_DESTRUCTURING #if JS_HAS_DESTRUCTURING
((version == JSVERSION_1_7 && case PNK_ARRAY:
!forEach && case PNK_OBJECT:
!forOf) // In JS 1.7 only, for ([K, V] in EXPR) has a special meaning.
? (!pn1->isKind(PNK_ARRAY) || pn1->pn_count != 2) // Hence all other destructuring left-hand sides are banned there.
: (!pn1->isKind(PNK_ARRAY) && !pn1->isKind(PNK_OBJECT))) && if (version == JSVERSION_1_7 && !forEach && !forOf)
return pn1->isKind(PNK_ARRAY) && pn1->pn_count == 2;
return true;
#endif #endif
!pn1->isKind(PNK_CALL) &&
#if JS_HAS_XML_SUPPORT default:
!pn1->isKind(PNK_XMLUNARY) && return false;
#endif }
!pn1->isKind(PNK_ELEM)));
} }
ParseNode * ParseNode *