Skip to content

fix(46433): Unhandled Error for using "enum" as a parameter #46459

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

Merged
merged 1 commit into from
Oct 22, 2021

Conversation

a-tarasyuk
Copy link
Contributor

FIxes #46433

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Oct 21, 2021
@@ -2611,7 +2611,10 @@ namespace ts {
case ParsingContext.ObjectLiteralMembers: return parseErrorAtCurrentToken(Diagnostics.Property_assignment_expected);
case ParsingContext.ArrayLiteralMembers: return parseErrorAtCurrentToken(Diagnostics.Expression_or_comma_expected);
case ParsingContext.JSDocParameters: return parseErrorAtCurrentToken(Diagnostics.Parameter_declaration_expected);
case ParsingContext.Parameters: return parseErrorAtCurrentToken(Diagnostics.Parameter_declaration_expected);
case ParsingContext.Parameters:
return isKeyword(token())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a possible reason why we didn't do this in another PR? Maybe @JoshuaKGoldberg knows?

I'm suspicious that it had something to do with modifiers and/or contextual keywords (e.g. public/private/protected/readonly(/override?) on parameter properties).

Otherwise the change seems good except for the fact that we bail out and start trying to parse an enum.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't a case I considered in #43005, no.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... except for the fact that we bail out and start trying to parse an enum.

@DanielRosenwasser Yes, you are right, I thought about this case and decided to follow behavior similar to variable declarations - Playground. Where TS tries to parse the enum (or other keywords) keyword as a variable name and then tries to find a name for the enum. Do we need to change that?

@@ -10,4 +10,4 @@ var [debugger, if] = [1, 2];
enum void {}
function f(default: number) {}
class C { m(null: string) {} }

function f1(enum) {}
Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg Oct 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact, this PR also changes the error message for a parameter named class in a similar way: a better error message, then an attempt to parse a class.

function f2(class) {}
- Parameter declaration expected.
+ 'class' is not allowed as a parameter name.
'{' expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great. @a-tarasyuk can we include this as a test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewbranch Sure. I've updated the tests.

Copy link
Member

@andrewbranch andrewbranch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, I thought about this case and decided to follow behavior similar to variable declarations - Playground. Where TS tries to parse the enum (or other keywords) keyword as a variable name and then tries to find a name for the enum. Do we need to change that?

I wouldn't be comfortable making a bigger change at this point in the release cycle, but this change looks safe and 100% better than before.

@andrewbranch andrewbranch merged commit ce676d0 into microsoft:main Oct 22, 2021
mprobst pushed a commit to mprobst/TypeScript that referenced this pull request Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unhandled Error for using "enum" as a parameter
5 participants