Skip to content

Commit a3a55ca

Browse files
Improve error message when reserved word is used as identifier (#33702)
Improve error message when reserved word is used as identifier
2 parents 29becf0 + ab87993 commit a3a55ca

14 files changed

+79
-31
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,10 @@
10431043
"category": "Error",
10441044
"code": 1358
10451045
},
1046+
"Identifier expected. '{0}' is a reserved word that cannot be used here.": {
1047+
"category": "Error",
1048+
"code": 1359
1049+
},
10461050

10471051
"The types of '{0}' are incompatible between these types.": {
10481052
"category": "Error",

src/compiler/parser.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,14 @@ namespace ts {
14011401
// Only for end of file because the error gets reported incorrectly on embedded script tags.
14021402
const reportAtCurrentPosition = token() === SyntaxKind.EndOfFileToken;
14031403

1404-
return createMissingNode<Identifier>(SyntaxKind.Identifier, reportAtCurrentPosition, diagnosticMessage || Diagnostics.Identifier_expected);
1404+
const isReservedWord = scanner.isReservedWord();
1405+
const msgArg = scanner.getTokenText();
1406+
1407+
const defaultMessage = isReservedWord ?
1408+
Diagnostics.Identifier_expected_0_is_a_reserved_word_that_cannot_be_used_here :
1409+
Diagnostics.Identifier_expected;
1410+
1411+
return createMissingNode<Identifier>(SyntaxKind.Identifier, reportAtCurrentPosition, diagnosticMessage || defaultMessage, msgArg);
14051412
}
14061413

14071414
function parseIdentifier(diagnosticMessage?: DiagnosticMessage): Identifier {

tests/baselines/reference/aliasErrors.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tests/cases/compiler/aliasErrors.ts(11,12): error TS2503: Cannot find namespace
22
tests/cases/compiler/aliasErrors.ts(12,13): error TS2503: Cannot find namespace 'no'.
33
tests/cases/compiler/aliasErrors.ts(13,12): error TS1003: Identifier expected.
44
tests/cases/compiler/aliasErrors.ts(14,12): error TS1003: Identifier expected.
5-
tests/cases/compiler/aliasErrors.ts(15,12): error TS1003: Identifier expected.
5+
tests/cases/compiler/aliasErrors.ts(15,12): error TS1359: Identifier expected. 'null' is a reserved word that cannot be used here.
66
tests/cases/compiler/aliasErrors.ts(16,12): error TS2503: Cannot find namespace 'undefined'.
77
tests/cases/compiler/aliasErrors.ts(26,15): error TS2694: Namespace 'foo.bar.baz' has no exported member 'bar'.
88

@@ -32,7 +32,7 @@ tests/cases/compiler/aliasErrors.ts(26,15): error TS2694: Namespace 'foo.bar.baz
3232
!!! error TS1003: Identifier expected.
3333
import q = null;
3434
~~~~
35-
!!! error TS1003: Identifier expected.
35+
!!! error TS1359: Identifier expected. 'null' is a reserved word that cannot be used here.
3636
import r = undefined;
3737
~~~~~~~~~
3838
!!! error TS2503: Cannot find namespace 'undefined'.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodThisParameter.ts(4,17): error TS1003: Identifier expected.
1+
tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodThisParameter.ts(4,17): error TS1359: Identifier expected. 'this' is a reserved word that cannot be used here.
22
tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodThisParameter.ts(4,17): error TS2680: A 'this' parameter must be the first parameter.
33

44

@@ -8,7 +8,7 @@ tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethod
88
class C {
99
method(@dec this: C) {}
1010
~~~~
11-
!!! error TS1003: Identifier expected.
11+
!!! error TS1359: Identifier expected. 'this' is a reserved word that cannot be used here.
1212
~~~~~~~
1313
!!! error TS2680: A 'this' parameter must be the first parameter.
1414
}

tests/baselines/reference/destructuringParameterDeclaration6.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(
88
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(9,26): error TS2304: Cannot find name 'public'.
99
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(9,32): error TS1005: ';' expected.
1010
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(9,33): error TS1128: Declaration or statement expected.
11-
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(10,16): error TS1003: Identifier expected.
11+
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(10,16): error TS1359: Identifier expected. 'while' is a reserved word that cannot be used here.
1212
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(10,21): error TS1005: '(' expected.
1313
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(12,13): error TS2370: A rest parameter must be of an array type.
1414

@@ -45,7 +45,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(
4545
!!! error TS1128: Declaration or statement expected.
4646
function a5(...while) { }
4747
~~~~~
48-
!!! error TS1003: Identifier expected.
48+
!!! error TS1359: Identifier expected. 'while' is a reserved word that cannot be used here.
4949
~
5050
!!! error TS1005: '(' expected.
5151
function a6(...public) { }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/conformance/es6/destructuring/objectBindingPatternKeywordIdentifiers02.ts(1,14): error TS1003: Identifier expected.
1+
tests/cases/conformance/es6/destructuring/objectBindingPatternKeywordIdentifiers02.ts(1,14): error TS1359: Identifier expected. 'while' is a reserved word that cannot be used here.
22
tests/cases/conformance/es6/destructuring/objectBindingPatternKeywordIdentifiers02.ts(1,20): error TS1005: ':' expected.
33

44

55
==== tests/cases/conformance/es6/destructuring/objectBindingPatternKeywordIdentifiers02.ts (2 errors) ====
66
var { while: while } = { while: 1 }
77
~~~~~
8-
!!! error TS1003: Identifier expected.
8+
!!! error TS1359: Identifier expected. 'while' is a reserved word that cannot be used here.
99
~
1010
!!! error TS1005: ':' expected.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/conformance/es6/destructuring/objectBindingPatternKeywordIdentifiers04.ts(1,16): error TS1003: Identifier expected.
1+
tests/cases/conformance/es6/destructuring/objectBindingPatternKeywordIdentifiers04.ts(1,16): error TS1359: Identifier expected. 'while' is a reserved word that cannot be used here.
22
tests/cases/conformance/es6/destructuring/objectBindingPatternKeywordIdentifiers04.ts(1,22): error TS1005: ':' expected.
33

44

55
==== tests/cases/conformance/es6/destructuring/objectBindingPatternKeywordIdentifiers04.ts (2 errors) ====
66
var { "while": while } = { while: 1 }
77
~~~~~
8-
!!! error TS1003: Identifier expected.
8+
!!! error TS1359: Identifier expected. 'while' is a reserved word that cannot be used here.
99
~
1010
!!! error TS1005: ':' expected.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration4.ts(1,6): error TS1003: Identifier expected.
1+
tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration4.ts(1,6): error TS1359: Identifier expected. 'void' is a reserved word that cannot be used here.
22

33

44
==== tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration4.ts (1 errors) ====
55
enum void {
66
~~~~
7-
!!! error TS1003: Identifier expected.
7+
!!! error TS1359: Identifier expected. 'void' is a reserved word that cannot be used here.
88
}

tests/baselines/reference/reservedWords2.errors.txt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ tests/cases/compiler/reservedWords2.ts(1,16): error TS2580: Cannot find name 're
44
tests/cases/compiler/reservedWords2.ts(1,31): error TS1005: ')' expected.
55
tests/cases/compiler/reservedWords2.ts(2,12): error TS2300: Duplicate identifier '(Missing)'.
66
tests/cases/compiler/reservedWords2.ts(2,12): error TS2567: Enum declarations can only merge with namespace or other enum declarations.
7-
tests/cases/compiler/reservedWords2.ts(2,14): error TS1003: Identifier expected.
7+
tests/cases/compiler/reservedWords2.ts(2,14): error TS1359: Identifier expected. 'while' is a reserved word that cannot be used here.
88
tests/cases/compiler/reservedWords2.ts(2,20): error TS1005: '(' expected.
99
tests/cases/compiler/reservedWords2.ts(2,20): error TS2304: Cannot find name 'from'.
1010
tests/cases/compiler/reservedWords2.ts(2,25): error TS1005: ')' expected.
1111
tests/cases/compiler/reservedWords2.ts(4,5): error TS1134: Variable declaration expected.
1212
tests/cases/compiler/reservedWords2.ts(4,12): error TS1109: Expression expected.
1313
tests/cases/compiler/reservedWords2.ts(5,9): error TS2300: Duplicate identifier '(Missing)'.
1414
tests/cases/compiler/reservedWords2.ts(5,9): error TS2567: Enum declarations can only merge with namespace or other enum declarations.
15-
tests/cases/compiler/reservedWords2.ts(5,10): error TS1003: Identifier expected.
15+
tests/cases/compiler/reservedWords2.ts(5,10): error TS1359: Identifier expected. 'throw' is a reserved word that cannot be used here.
1616
tests/cases/compiler/reservedWords2.ts(5,18): error TS1005: '=>' expected.
1717
tests/cases/compiler/reservedWords2.ts(6,1): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
1818
tests/cases/compiler/reservedWords2.ts(6,8): error TS1005: ';' expected.
@@ -29,10 +29,13 @@ tests/cases/compiler/reservedWords2.ts(9,14): error TS1005: ';' expected.
2929
tests/cases/compiler/reservedWords2.ts(9,18): error TS1005: '(' expected.
3030
tests/cases/compiler/reservedWords2.ts(9,20): error TS1128: Declaration or statement expected.
3131
tests/cases/compiler/reservedWords2.ts(10,5): error TS2567: Enum declarations can only merge with namespace or other enum declarations.
32-
tests/cases/compiler/reservedWords2.ts(10,6): error TS1003: Identifier expected.
32+
tests/cases/compiler/reservedWords2.ts(10,6): error TS1359: Identifier expected. 'void' is a reserved word that cannot be used here.
33+
tests/cases/compiler/reservedWords2.ts(11,12): error TS1359: Identifier expected. 'default' is a reserved word that cannot be used here.
34+
tests/cases/compiler/reservedWords2.ts(12,13): error TS1359: Identifier expected. 'null' is a reserved word that cannot be used here.
35+
tests/cases/compiler/reservedWords2.ts(12,17): error TS1138: Parameter declaration expected.
3336

3437

35-
==== tests/cases/compiler/reservedWords2.ts (32 errors) ====
38+
==== tests/cases/compiler/reservedWords2.ts (35 errors) ====
3639
import while = require("dfdf");
3740
~~~~~
3841
!!! error TS1109: Expression expected.
@@ -48,7 +51,7 @@ tests/cases/compiler/reservedWords2.ts(10,6): error TS1003: Identifier expected.
4851

4952
!!! error TS2567: Enum declarations can only merge with namespace or other enum declarations.
5053
~~~~~
51-
!!! error TS1003: Identifier expected.
54+
!!! error TS1359: Identifier expected. 'while' is a reserved word that cannot be used here.
5255
~~~~
5356
!!! error TS1005: '(' expected.
5457
~~~~
@@ -67,7 +70,7 @@ tests/cases/compiler/reservedWords2.ts(10,6): error TS1003: Identifier expected.
6770

6871
!!! error TS2567: Enum declarations can only merge with namespace or other enum declarations.
6972
~~~~~
70-
!!! error TS1003: Identifier expected.
73+
!!! error TS1359: Identifier expected. 'throw' is a reserved word that cannot be used here.
7174
~
7275
!!! error TS1005: '=>' expected.
7376
module void {}
@@ -106,7 +109,14 @@ tests/cases/compiler/reservedWords2.ts(10,6): error TS1003: Identifier expected.
106109

107110
!!! error TS2567: Enum declarations can only merge with namespace or other enum declarations.
108111
~~~~
109-
!!! error TS1003: Identifier expected.
110-
112+
!!! error TS1359: Identifier expected. 'void' is a reserved word that cannot be used here.
113+
function f(default: number) {}
114+
~~~~~~~
115+
!!! error TS1359: Identifier expected. 'default' is a reserved word that cannot be used here.
116+
class C { m(null: string) {} }
117+
~~~~
118+
!!! error TS1359: Identifier expected. 'null' is a reserved word that cannot be used here.
119+
~
120+
!!! error TS1138: Parameter declaration expected.
111121

112122

tests/baselines/reference/reservedWords2.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ var {while, return} = { while: 1, return: 2 };
99
var {this, switch: { continue} } = { this: 1, switch: { continue: 2 }};
1010
var [debugger, if] = [1, 2];
1111
enum void {}
12-
12+
function f(default: number) {}
13+
class C { m(null: string) {} }
1314

1415

1516

@@ -38,3 +39,10 @@ if ()
3839
(function () {
3940
})( || ( = {}));
4041
void {};
42+
function f() { }
43+
var C = /** @class */ (function () {
44+
function C() {
45+
}
46+
C.prototype.m = function (, string) { };
47+
return C;
48+
}());

0 commit comments

Comments
 (0)