-
Notifications
You must be signed in to change notification settings - Fork 23
Add grammar flags to disallow mixing ?? with || and &&. #39
Add grammar flags to disallow mixing ?? with || and &&. #39
Conversation
The current changes were adapted from https://gist.github.com/bakkot/332ddeb26978c41613abc092120448b9, but I think there might have been a mistake in that grammar. Should it be the following? NullishExpression[Nullish]:
LogicalORExpression[?Nullish]
- LogicalORExpression[+Nullish] ?? NullishExpression[+Nullish]
+ NullishExpression[+Nullish] ?? LogicalORExpression[+Nullish] |
(Note: This is spill-over from #38. Sorry in advance for the full-throttle teacher mode. 😅) I believe this change is motivated by a view of the differing precedence of
So we've got stuff like:
But more importantly, we have short-circuiting:
And thankfully, that fact doesn't become less useful when we're dealing with truthiness—that's why folks have been rightfully able to write stuff like the following up 'til now: const isValid =
options.alreadyValidated ||
hasValidFoo() && hasValidBar() ||
GLOBAL_OVERRIDE; I understand that people make mistakes and that bugs can be costly. It's important that we have lint rules and even compiler warnings for such things. But I don't think |
There is a significant difference in that nearly everyone is taught some form of PEMDAS in school, but many people have never seen && or || prior to programming - it’s why the airbnb style guide requires parens when mixing && and ||, but not when mixing * + / -. |
Note that, if we do want to disallow mixing Anyway, introducing such rules at the language level is not common. (Not that I am against that.) PR #40 (giving |
@rkirsling beyond what @ljharb says about
is a fact not frequently not taught to or understood by even to people who have university degrees in computer science (who themselves not that large a fraction of JS programmers). |
@bakkot That is fair, and I'm not in the least suggesting that people "should know better" or something (though I do get excited to share it with others 'cause I think it's really nifty 😂); I only mean to argue that syntax errors are not something to impose lightly. (Now, if we wanted to introduce syntax warnings into the spec... 😉) @ljharb I actually don't take your comment as a point of disagreement—I've been an enthusiastic advocate of the Airbnb style guide at my workplace for years. 😄 |
I've included updates to uses or LogicalORExpression and ConditionalExpression, and included NullishExpression in the appropriate places. The spec diff isn't as tiny as it was before, but by-and-large it's not too bad when rendered. |
Hey all, I've removed the redundant clauses now that |
Summary: updates parsing of the nullish coalescing operator (`??`) based on the updated stage 3 draft (tc39/ecma262#1644). this specific change happened in tc39/proposal-nullish-coalescing#39. Changelog: fix(parser): Updated parsing of the experimental nullish coalescing `??` operator. It now has a lower precedence than `||` and `&&`, and parentheses are required to nest it with them. Reviewed By: gabelevi Differential Revision: D16561382 fbshipit-source-id: 099ef58bc6cfa90ff3a3153636363986aab2f158
About associativity: https://gist.github.com/bakkot/332ddeb26978c41613abc092120448b9#gistcomment-3071704 |
Alternative to #38. Instead of using early errors, this change disallows combining the nullish coalescing operator (
??
) with both the logical AND (&&
) and logical OR (||
) operators directly through the grammar.