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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,10 @@
"category": "Error",
"code": 1389
},
"'{0}' is not allowed as a parameter name.": {
"category": "Error",
"code": 1390
},
"An import alias cannot use 'import type'": {
"category": "Error",
"code": 1392
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?

? parseErrorAtCurrentToken(Diagnostics._0_is_not_allowed_as_a_parameter_name, tokenToString(token()))
: parseErrorAtCurrentToken(Diagnostics.Parameter_declaration_expected);
case ParsingContext.TypeParameters: return parseErrorAtCurrentToken(Diagnostics.Type_parameter_declaration_expected);
case ParsingContext.TypeArguments: return parseErrorAtCurrentToken(Diagnostics.Type_argument_expected);
case ParsingContext.TupleElementTypes: return parseErrorAtCurrentToken(Diagnostics.Type_expected);
Expand Down
1 change: 0 additions & 1 deletion tests/baselines/reference/reservedWords2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,4 @@ tests/cases/compiler/reservedWords2.ts(12,17): error TS1138: Parameter declarati
!!! error TS1359: Identifier expected. 'null' is a reserved word that cannot be used here.
~
!!! error TS1138: Parameter declaration expected.


1 change: 0 additions & 1 deletion tests/baselines/reference/reservedWords2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var [debugger, if] = [1, 2];
enum void {}
function f(default: number) {}
class C { m(null: string) {} }



//// [reservedWords2.js]
Expand Down
1 change: 0 additions & 1 deletion tests/baselines/reference/reservedWords2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,3 @@ class C { m(null: string) {} }
> : Symbol((Missing), Decl(reservedWords2.ts, 11, 12))
>string : Symbol(string, Decl(reservedWords2.ts, 11, 17))


1 change: 0 additions & 1 deletion tests/baselines/reference/reservedWords2.types
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,3 @@ class C { m(null: string) {} }
> : any
>string : any


45 changes: 45 additions & 0 deletions tests/baselines/reference/reservedWords3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
tests/cases/compiler/reservedWords3.ts(1,13): error TS1390: 'enum' is not allowed as a parameter name.
tests/cases/compiler/reservedWords3.ts(1,17): error TS2567: Enum declarations can only merge with namespace or other enum declarations.
tests/cases/compiler/reservedWords3.ts(1,17): error TS1003: Identifier expected.
tests/cases/compiler/reservedWords3.ts(2,13): error TS1390: 'class' is not allowed as a parameter name.
tests/cases/compiler/reservedWords3.ts(2,18): error TS1005: '{' expected.
tests/cases/compiler/reservedWords3.ts(3,13): error TS1390: 'function' is not allowed as a parameter name.
tests/cases/compiler/reservedWords3.ts(3,21): error TS2567: Enum declarations can only merge with namespace or other enum declarations.
tests/cases/compiler/reservedWords3.ts(3,21): error TS1003: Identifier expected.
tests/cases/compiler/reservedWords3.ts(4,13): error TS1390: 'while' is not allowed as a parameter name.
tests/cases/compiler/reservedWords3.ts(4,18): error TS1005: '(' expected.
tests/cases/compiler/reservedWords3.ts(5,13): error TS1390: 'for' is not allowed as a parameter name.
tests/cases/compiler/reservedWords3.ts(5,16): error TS1005: '(' expected.


==== tests/cases/compiler/reservedWords3.ts (12 errors) ====
function f1(enum) {}
~~~~
!!! error TS1390: 'enum' is not allowed as a parameter name.

!!! error TS2567: Enum declarations can only merge with namespace or other enum declarations.
~
!!! error TS1003: Identifier expected.
function f2(class) {}
~~~~~
!!! error TS1390: 'class' is not allowed as a parameter name.
~
!!! error TS1005: '{' expected.
function f3(function) {}
~~~~~~~~
!!! error TS1390: 'function' is not allowed as a parameter name.

!!! error TS2567: Enum declarations can only merge with namespace or other enum declarations.
~
!!! error TS1003: Identifier expected.
function f4(while) {}
~~~~~
!!! error TS1390: 'while' is not allowed as a parameter name.
~
!!! error TS1005: '(' expected.
function f5(for) {}
~~~
!!! error TS1390: 'for' is not allowed as a parameter name.
~
!!! error TS1005: '(' expected.

28 changes: 28 additions & 0 deletions tests/baselines/reference/reservedWords3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [reservedWords3.ts]
function f1(enum) {}
function f2(class) {}
function f3(function) {}
function f4(while) {}
function f5(for) {}


//// [reservedWords3.js]
function f1() { }
var ;
(function () {
})( || ( = {}));
{ }
function f2() { }
var default_1 = /** @class */ (function () {
function default_1() {
}
return default_1;
}());
{ }
function f3() { }
function () { }
{ }
function f4() { }
while () { }
function f5() { }
for (;;) { }
18 changes: 18 additions & 0 deletions tests/baselines/reference/reservedWords3.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/reservedWords3.ts ===
function f1(enum) {}
>f1 : Symbol(f1, Decl(reservedWords3.ts, 0, 0))
> : Symbol((Missing), Decl(reservedWords3.ts, 0, 12))

function f2(class) {}
>f2 : Symbol(f2, Decl(reservedWords3.ts, 0, 20))

function f3(function) {}
>f3 : Symbol(f3, Decl(reservedWords3.ts, 1, 21))
> : Symbol((Missing), Decl(reservedWords3.ts, 2, 12))

function f4(while) {}
>f4 : Symbol(f4, Decl(reservedWords3.ts, 2, 24))

function f5(for) {}
>f5 : Symbol(f5, Decl(reservedWords3.ts, 3, 21))

20 changes: 20 additions & 0 deletions tests/baselines/reference/reservedWords3.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/reservedWords3.ts ===
function f1(enum) {}
>f1 : () => any
> : (Missing)

function f2(class) {}
>f2 : () => any

function f3(function) {}
>f3 : () => any
> : () => any

function f4(while) {}
>f4 : () => any
> : any

function f5(for) {}
>f5 : () => any
> : any

1 change: 0 additions & 1 deletion tests/cases/compiler/reservedWords2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ var [debugger, if] = [1, 2];
enum void {}
function f(default: number) {}
class C { m(null: string) {} }

5 changes: 5 additions & 0 deletions tests/cases/compiler/reservedWords3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function f1(enum) {}
function f2(class) {}
function f3(function) {}
function f4(while) {}
function f5(for) {}