Skip to content

Commit 0298033

Browse files
Specified error diagnostic for invalid variable names (microsoft#40105)
* Specified error diagnostic for invalid variable names * Use callback directly, and isKeyword
1 parent 4dd9f69 commit 0298033

7 files changed

+49
-42
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,10 @@
11801180
"category": "Error",
11811181
"code": 1388
11821182
},
1183+
"'{0}' is not allowed as a variable declaration name.": {
1184+
"category": "Error",
1185+
"code": 1389
1186+
},
11831187

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

src/compiler/parser.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,7 +2356,7 @@ namespace ts {
23562356

23572357
// Returns true if we should abort parsing.
23582358
function abortParsingListOrMoveToNextToken(kind: ParsingContext) {
2359-
parseErrorAtCurrentToken(parsingContextErrors(kind));
2359+
parsingContextErrors(kind);
23602360
if (isInSomeParsingContext()) {
23612361
return true;
23622362
}
@@ -2365,33 +2365,36 @@ namespace ts {
23652365
return false;
23662366
}
23672367

2368-
function parsingContextErrors(context: ParsingContext): DiagnosticMessage {
2368+
function parsingContextErrors(context: ParsingContext) {
23692369
switch (context) {
2370-
case ParsingContext.SourceElements: return Diagnostics.Declaration_or_statement_expected;
2371-
case ParsingContext.BlockStatements: return Diagnostics.Declaration_or_statement_expected;
2372-
case ParsingContext.SwitchClauses: return Diagnostics.case_or_default_expected;
2373-
case ParsingContext.SwitchClauseStatements: return Diagnostics.Statement_expected;
2370+
case ParsingContext.SourceElements: return parseErrorAtCurrentToken(Diagnostics.Declaration_or_statement_expected);
2371+
case ParsingContext.BlockStatements: return parseErrorAtCurrentToken(Diagnostics.Declaration_or_statement_expected);
2372+
case ParsingContext.SwitchClauses: return parseErrorAtCurrentToken(Diagnostics.case_or_default_expected);
2373+
case ParsingContext.SwitchClauseStatements: return parseErrorAtCurrentToken(Diagnostics.Statement_expected);
23742374
case ParsingContext.RestProperties: // fallthrough
2375-
case ParsingContext.TypeMembers: return Diagnostics.Property_or_signature_expected;
2376-
case ParsingContext.ClassMembers: return Diagnostics.Unexpected_token_A_constructor_method_accessor_or_property_was_expected;
2377-
case ParsingContext.EnumMembers: return Diagnostics.Enum_member_expected;
2378-
case ParsingContext.HeritageClauseElement: return Diagnostics.Expression_expected;
2379-
case ParsingContext.VariableDeclarations: return Diagnostics.Variable_declaration_expected;
2380-
case ParsingContext.ObjectBindingElements: return Diagnostics.Property_destructuring_pattern_expected;
2381-
case ParsingContext.ArrayBindingElements: return Diagnostics.Array_element_destructuring_pattern_expected;
2382-
case ParsingContext.ArgumentExpressions: return Diagnostics.Argument_expression_expected;
2383-
case ParsingContext.ObjectLiteralMembers: return Diagnostics.Property_assignment_expected;
2384-
case ParsingContext.ArrayLiteralMembers: return Diagnostics.Expression_or_comma_expected;
2385-
case ParsingContext.JSDocParameters: return Diagnostics.Parameter_declaration_expected;
2386-
case ParsingContext.Parameters: return Diagnostics.Parameter_declaration_expected;
2387-
case ParsingContext.TypeParameters: return Diagnostics.Type_parameter_declaration_expected;
2388-
case ParsingContext.TypeArguments: return Diagnostics.Type_argument_expected;
2389-
case ParsingContext.TupleElementTypes: return Diagnostics.Type_expected;
2390-
case ParsingContext.HeritageClauses: return Diagnostics.Unexpected_token_expected;
2391-
case ParsingContext.ImportOrExportSpecifiers: return Diagnostics.Identifier_expected;
2392-
case ParsingContext.JsxAttributes: return Diagnostics.Identifier_expected;
2393-
case ParsingContext.JsxChildren: return Diagnostics.Identifier_expected;
2394-
default: return undefined!; // TODO: GH#18217 `default: Debug.assertNever(context);`
2375+
case ParsingContext.TypeMembers: return parseErrorAtCurrentToken(Diagnostics.Property_or_signature_expected);
2376+
case ParsingContext.ClassMembers: return parseErrorAtCurrentToken(Diagnostics.Unexpected_token_A_constructor_method_accessor_or_property_was_expected);
2377+
case ParsingContext.EnumMembers: return parseErrorAtCurrentToken(Diagnostics.Enum_member_expected);
2378+
case ParsingContext.HeritageClauseElement: return parseErrorAtCurrentToken(Diagnostics.Expression_expected);
2379+
case ParsingContext.VariableDeclarations:
2380+
return isKeyword(token())
2381+
? parseErrorAtCurrentToken(Diagnostics._0_is_not_allowed_as_a_variable_declaration_name, tokenToString(token()))
2382+
: parseErrorAtCurrentToken(Diagnostics.Variable_declaration_expected);
2383+
case ParsingContext.ObjectBindingElements: return parseErrorAtCurrentToken(Diagnostics.Property_destructuring_pattern_expected);
2384+
case ParsingContext.ArrayBindingElements: return parseErrorAtCurrentToken(Diagnostics.Array_element_destructuring_pattern_expected);
2385+
case ParsingContext.ArgumentExpressions: return parseErrorAtCurrentToken(Diagnostics.Argument_expression_expected);
2386+
case ParsingContext.ObjectLiteralMembers: return parseErrorAtCurrentToken(Diagnostics.Property_assignment_expected);
2387+
case ParsingContext.ArrayLiteralMembers: return parseErrorAtCurrentToken(Diagnostics.Expression_or_comma_expected);
2388+
case ParsingContext.JSDocParameters: return parseErrorAtCurrentToken(Diagnostics.Parameter_declaration_expected);
2389+
case ParsingContext.Parameters: return parseErrorAtCurrentToken(Diagnostics.Parameter_declaration_expected);
2390+
case ParsingContext.TypeParameters: return parseErrorAtCurrentToken(Diagnostics.Type_parameter_declaration_expected);
2391+
case ParsingContext.TypeArguments: return parseErrorAtCurrentToken(Diagnostics.Type_argument_expected);
2392+
case ParsingContext.TupleElementTypes: return parseErrorAtCurrentToken(Diagnostics.Type_expected);
2393+
case ParsingContext.HeritageClauses: return parseErrorAtCurrentToken(Diagnostics.Unexpected_token_expected);
2394+
case ParsingContext.ImportOrExportSpecifiers: return parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
2395+
case ParsingContext.JsxAttributes: return parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
2396+
case ParsingContext.JsxChildren: return parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
2397+
default: return [undefined!]; // TODO: GH#18217 `default: Debug.assertNever(context);`
23952398
}
23962399
}
23972400

tests/baselines/reference/expressionTypeNodeShouldError.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
tests/cases/compiler/base.d.ts(1,23): error TS1005: ',' expected.
22
tests/cases/compiler/base.d.ts(1,34): error TS1005: ',' expected.
33
tests/cases/compiler/boolean.ts(7,23): error TS1005: ',' expected.
4-
tests/cases/compiler/boolean.ts(7,24): error TS1134: Variable declaration expected.
4+
tests/cases/compiler/boolean.ts(7,24): error TS1389: 'typeof' is not allowed as a variable declaration name.
55
tests/cases/compiler/boolean.ts(12,22): error TS1005: ';' expected.
66
tests/cases/compiler/number.ts(7,26): error TS1005: ',' expected.
7-
tests/cases/compiler/number.ts(7,27): error TS1134: Variable declaration expected.
7+
tests/cases/compiler/number.ts(7,27): error TS1389: 'typeof' is not allowed as a variable declaration name.
88
tests/cases/compiler/number.ts(12,20): error TS1005: ';' expected.
99
tests/cases/compiler/string.ts(7,20): error TS1005: ',' expected.
10-
tests/cases/compiler/string.ts(7,21): error TS1134: Variable declaration expected.
10+
tests/cases/compiler/string.ts(7,21): error TS1389: 'typeof' is not allowed as a variable declaration name.
1111
tests/cases/compiler/string.ts(12,19): error TS1005: ';' expected.
1212

1313

@@ -29,7 +29,7 @@ tests/cases/compiler/string.ts(12,19): error TS1005: ';' expected.
2929
~
3030
!!! error TS1005: ',' expected.
3131
~~~~~~
32-
!!! error TS1134: Variable declaration expected.
32+
!!! error TS1389: 'typeof' is not allowed as a variable declaration name.
3333
}
3434
}
3535

@@ -49,7 +49,7 @@ tests/cases/compiler/string.ts(12,19): error TS1005: ';' expected.
4949
~
5050
!!! error TS1005: ',' expected.
5151
~~~~~~
52-
!!! error TS1134: Variable declaration expected.
52+
!!! error TS1389: 'typeof' is not allowed as a variable declaration name.
5353
}
5454
}
5555

@@ -69,7 +69,7 @@ tests/cases/compiler/string.ts(12,19): error TS1005: ';' expected.
6969
~
7070
!!! error TS1005: ',' expected.
7171
~~~~~~
72-
!!! error TS1134: Variable declaration expected.
72+
!!! error TS1389: 'typeof' is not allowed as a variable declaration name.
7373
}
7474
}
7575

tests/baselines/reference/getJavaScriptSyntacticDiagnostics02.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Syntactic Diagnostics for file '/tests/cases/fourslash/server/getJavaScriptSyntacticDiagnostics02.ts':
22
/tests/cases/fourslash/server/b.js(2,8): error TS8010: Type annotations can only be used in TypeScript files.
33
/tests/cases/fourslash/server/b.js(3,17): error TS8010: Type annotations can only be used in TypeScript files.
4-
/tests/cases/fourslash/server/b.js(4,5): error TS1134: Variable declaration expected.
4+
/tests/cases/fourslash/server/b.js(4,5): error TS1389: 'var' is not allowed as a variable declaration name.
55
/tests/cases/fourslash/server/b.js(4,9): error TS1134: Variable declaration expected.
66
/tests/cases/fourslash/server/b.js(4,11): error TS1134: Variable declaration expected.
77

@@ -16,7 +16,7 @@ Syntactic Diagnostics for file '/tests/cases/fourslash/server/getJavaScriptSynta
1616
!!! error TS8010: Type annotations can only be used in TypeScript files.
1717
var var = "c";
1818
~~~
19-
!!! error TS1134: Variable declaration expected.
19+
!!! error TS1389: 'var' is not allowed as a variable declaration name.
2020
~
2121
!!! error TS1134: Variable declaration expected.
2222
~~~

tests/baselines/reference/parserInvalidIdentifiersInVariableStatements1.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(1,5): error TS1134: Variable declaration expected.
2-
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(3,5): error TS1134: Variable declaration expected.
1+
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(1,5): error TS1389: 'export' is not allowed as a variable declaration name.
2+
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(3,5): error TS1389: 'class' is not allowed as a variable declaration name.
33
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(3,10): error TS1005: '{' expected.
44

55

66
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts (3 errors) ====
77
var export;
88
~~~~~~
9-
!!! error TS1134: Variable declaration expected.
9+
!!! error TS1389: 'export' is not allowed as a variable declaration name.
1010
var foo;
1111
var class;
1212
~~~~~
13-
!!! error TS1134: Variable declaration expected.
13+
!!! error TS1389: 'class' is not allowed as a variable declaration name.
1414
~
1515
!!! error TS1005: '{' expected.
1616
var bar;

tests/baselines/reference/reservedWords2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tests/cases/compiler/reservedWords2.ts(2,14): error TS1359: Identifier expected.
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.
11-
tests/cases/compiler/reservedWords2.ts(4,5): error TS1134: Variable declaration expected.
11+
tests/cases/compiler/reservedWords2.ts(4,5): error TS1389: 'typeof' is not allowed as a variable declaration name.
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.
@@ -61,7 +61,7 @@ tests/cases/compiler/reservedWords2.ts(12,17): error TS1138: Parameter declarati
6161

6262
var typeof = 10;
6363
~~~~~~
64-
!!! error TS1134: Variable declaration expected.
64+
!!! error TS1389: 'typeof' is not allowed as a variable declaration name.
6565
~
6666
!!! error TS1109: Expression expected.
6767
function throw() {}

tests/baselines/reference/umd-errors.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ tests/cases/conformance/externalModules/err2.d.ts(3,2): error TS1316: Global mod
33
tests/cases/conformance/externalModules/err3.d.ts(3,1): error TS1184: Modifiers cannot appear here.
44
tests/cases/conformance/externalModules/err3.d.ts(4,1): error TS1184: Modifiers cannot appear here.
55
tests/cases/conformance/externalModules/err3.d.ts(5,1): error TS1184: Modifiers cannot appear here.
6-
tests/cases/conformance/externalModules/err3.d.ts(6,7): error TS1134: Variable declaration expected.
6+
tests/cases/conformance/externalModules/err3.d.ts(6,7): error TS1389: 'export' is not allowed as a variable declaration name.
77
tests/cases/conformance/externalModules/err4.d.ts(3,2): error TS1316: Global module exports may only appear at top level.
88
tests/cases/conformance/externalModules/err5.ts(3,1): error TS1315: Global module exports may only appear in declaration files.
99

@@ -36,7 +36,7 @@ tests/cases/conformance/externalModules/err5.ts(3,1): error TS1315: Global modul
3636
!!! error TS1184: Modifiers cannot appear here.
3737
const export as namespace oo4;
3838
~~~~~~
39-
!!! error TS1134: Variable declaration expected.
39+
!!! error TS1389: 'export' is not allowed as a variable declaration name.
4040

4141
==== tests/cases/conformance/externalModules/err4.d.ts (1 errors) ====
4242
// Illegal, must be at top-level

0 commit comments

Comments
 (0)