Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move 'throw' to Expression #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

rbuckton
Copy link
Collaborator

@rbuckton rbuckton commented Jan 17, 2024

This is one of two alternative approaches to define throw expressions such that their operand has the same precedence as the operand to a ThrowStatement. For the alternative approach, see #22.

In this variant, ThrowExpression is a direct descendant of Expression, but is mutually exclusive with the , operator. This means that ThrowExpression will almost always require parentheses, though it is arguable as to whether the few places where parentheses can be elided are useful:

// moderately useful when paired with `break`, allowed without needing extra parenthesis:
do { } while (throw a); 
for (; throw a;) {}
for (;; throw b) {}

// nonsensical, but allowed without needing extra parentheses:
if (throw a) { }
while (throw a) { }
for (throw a;;) { }
for (let x in throw a) { }
switch (throw a) { }
switch (a) { case throw b: }
return throw a;
throw throw a;
`${throw a}`;
a[throw b];
a?.[throw b];

Fixes #23

Copy link

A preview of this PR can be found at https://tc39.es/proposal-throw-expressions/pr/21.

@rbuckton
Copy link
Collaborator Author

@bakkot, @michaelficarra, @nicolo-ribaudo: could you take a look?

@michaelficarra
Copy link
Member

michaelficarra commented Jan 17, 2024

Doesn't this introduce an ambiguous parse between throw statements and throw expressions in statement position?

edit: I didn't notice the lookahead since it wasn't part of the diff. Shouldn't be an issue, then.

@nicolo-ribaudo
Copy link
Member

I very much prefer #22 as it makes the rule for parentheses easier, but the spec text here looks good if we were to choose this syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Require parentheses around throw expressions
3 participants