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

Specific diagnostic suggestions for unexpected keyword or identifier #43005

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0c967f9
Specific diagnostic suggestions for unexpected keywords or identifier
Feb 28, 2021
107e10d
Don't reach into there, that's not allowed
Mar 4, 2021
12088e9
Improved error when there is already an initializer
Mar 23, 2021
73a9852
Specific module error message for invalid template literal strings
Mar 28, 2021
cc70a1b
Skip 'unexpected keyword or identifier' diagnostics for declare nodes
Mar 28, 2021
23e0acb
Improve error for function calls in type positions
Mar 28, 2021
5cf8606
Merge branch 'master'
Mar 28, 2021
05e8d0f
Switch class properties to old diagnostic
Apr 7, 2021
f547914
Merge branch 'master'
Apr 7, 2021
972da93
Corrected errors in class members and reused existing textToKeywordOb…
Apr 11, 2021
c554519
Merge branch 'master'
Apr 11, 2021
5a0804b
Corrected more baselines from the merge
Apr 11, 2021
182f75a
Update src/compiler/parser.ts
JoshuaKGoldberg Apr 13, 2021
dabe89a
Mostly addressed feedback
Apr 13, 2021
6d40a4c
Clarified function call type message
Apr 17, 2021
7e49aa6
Split up and clarified parsing vs error functions
Apr 17, 2021
a052314
Swap interface name complaints back, and skip new errors on unknown (…
Apr 17, 2021
8e9e46d
Merge branch 'master'
Apr 17, 2021
dd47c39
Used tokenToString, not a raw semicolon
Apr 17, 2021
ad6f591
Merge branch 'master' into jg-unexpected-keyword-identifier-diagnostics
May 28, 2021
cd4d28d
Merge branch 'main'
Jul 14, 2021
b878e74
Inline getExpressionText helper
Jul 14, 2021
b16d5eb
Remove remarks in src/compiler/parser.ts
JoshuaKGoldberg Jul 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -1368,6 +1368,46 @@
"category": "Error",
"code": 1433
},
"Unexpected keyword or identifier.": {
"category": "Error",
"code": 1434
},
"Unknown keyword or identifier. Did you mean '{0}'?": {
"category": "Error",
"code": 1435
},
"Decorators must precede the name and all keywords of property declarations.": {
"category": "Error",
"code": 1436
},
"Namespace must be given a name.": {
"category": "Error",
"code": 1437
},
"Interface must be given a name.": {
"category": "Error",
"code": 1438
},
"Type alias must be given a name.": {
"category": "Error",
"code": 1439
},
"Variable declaration not allowed at this location.": {
"category": "Error",
"code": 1440
},
"Cannot start a function call in a type annotation.": {
"category": "Error",
"code": 1441
},
"Missing '=' before default property value.": {
"category": "Error",
"code": 1442
},
"Module declaration names may only use ' or \" quoted strings.": {
"category": "Error",
"code": 1443
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
Expand Down Expand Up @@ -3356,6 +3396,10 @@
"category": "Error",
"code": 2818
},
"Namespace name cannot be '{0}'.": {
"category": "Error",
"code": 2819
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
176 changes: 163 additions & 13 deletions src/compiler/parser.ts
Expand Up @@ -1549,6 +1549,149 @@ namespace ts {
return false;
}

const viableKeywordSuggestions = Object.keys(textToKeywordObj).filter(keyword => keyword.length > 2);

/**
* Provides a better error message than the generic "';' expected" if possible for
* known common variants of a missing semicolon, such as from a mispelled names.
*
* @param node Node preceding the expected semicolon location.
*/
function parseErrorForMissingSemicolonAfter(node: Expression | PropertyName): void {
// Tagged template literals are sometimes used in places where only simple strings are allowed, i.e.:
// module `M1` {
// ^^^^^^^^^^^ This block is parsed as a template literal like module`M1`.
if (isTaggedTemplateExpression(node)) {
parseErrorAt(skipTrivia(sourceText, node.template.pos), node.template.end, Diagnostics.Module_declaration_names_may_only_use_or_quoted_strings);
return;
}

// Otherwise, if this isn't a well-known keyword-like identifier, give the generic fallback message.
const expressionText = ts.isIdentifier(node) ? idText(node) : undefined;
if (!expressionText || !isIdentifierText(expressionText, languageVersion)) {
parseErrorAtCurrentToken(Diagnostics._0_expected, tokenToString(SyntaxKind.SemicolonToken));
return;
}

const pos = skipTrivia(sourceText, node.pos);

// Some known keywords are likely signs of syntax being used improperly.
switch (expressionText) {
case "const":
case "let":
case "var":
parseErrorAt(pos, node.end, Diagnostics.Variable_declaration_not_allowed_at_this_location);
return;

case "declare":
// If a declared node failed to parse, it would have emitted a diagnostic already.
return;

case "interface":
parseErrorForInvalidName(Diagnostics.Interface_name_cannot_be_0, Diagnostics.Interface_must_be_given_a_name, SyntaxKind.OpenBraceToken);
return;

case "is":
parseErrorAt(pos, scanner.getTextPos(), Diagnostics.A_type_predicate_is_only_allowed_in_return_type_position_for_functions_and_methods);
return;

case "module":
case "namespace":
parseErrorForInvalidName(Diagnostics.Namespace_name_cannot_be_0, Diagnostics.Namespace_must_be_given_a_name, SyntaxKind.OpenBraceToken);
return;

case "type":
parseErrorForInvalidName(Diagnostics.Type_alias_name_cannot_be_0, Diagnostics.Type_alias_must_be_given_a_name, SyntaxKind.EqualsToken);
return;
}

// The user alternatively might have misspelled or forgotten to add a space after a common keyword.
const suggestion = getSpellingSuggestion(expressionText, viableKeywordSuggestions, n => n) ?? getSpaceSuggestion(expressionText);
if (suggestion) {
parseErrorAt(pos, node.end, Diagnostics.Unknown_keyword_or_identifier_Did_you_mean_0, suggestion);
return;
}

// Unknown tokens are handled with their own errors in the scanner
if (token() === SyntaxKind.Unknown) {
return;
}

// Otherwise, we know this some kind of unknown word, not just a missing expected semicolon.
parseErrorAt(pos, node.end, Diagnostics.Unexpected_keyword_or_identifier);
}

/**
* Reports a diagnostic error for the current token being an invalid name.
*
* @param blankDiagnostic Diagnostic to report for the case of the name being blank (matched tokenIfBlankName).
* @param nameDiagnostic Diagnostic to report for all other cases.
* @param tokenIfBlankName Current token if the name was invalid for being blank (not provided / skipped).
*/
function parseErrorForInvalidName(nameDiagnostic: DiagnosticMessage, blankDiagnostic: DiagnosticMessage, tokenIfBlankName: SyntaxKind) {
if (token() === tokenIfBlankName) {
parseErrorAtCurrentToken(blankDiagnostic);
}
else {
parseErrorAtCurrentToken(nameDiagnostic, tokenToString(token()));
}
}

function getSpaceSuggestion(expressionText: string) {
for (const keyword of viableKeywordSuggestions) {
if (expressionText.length > keyword.length + 2 && startsWith(expressionText, keyword)) {
return `${keyword} ${expressionText.slice(keyword.length)}`;
}
}

return undefined;
}

function parseSemicolonAfterPropertyName(name: PropertyName, type: TypeNode | undefined, initializer: Expression | undefined) {
switch (token()) {
case SyntaxKind.AtToken:
parseErrorAtCurrentToken(Diagnostics.Decorators_must_precede_the_name_and_all_keywords_of_property_declarations);
Copy link
Member

@rbuckton rbuckton Jul 24, 2021

Choose a reason for hiding this comment

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

We should only report an error here if there's no preceding line terminator. This is valid JS:

class C {
  a
  b
  c() {}
}

ASI inserts a ; between a and b, and between b and c(). I would expect the same to be true for decorators:

class C {
  a
  @dec b
  c() {}
}

This should be legal since ASI would insert a ; between a and @dec, however now it is an error.

return;

case SyntaxKind.OpenParenToken:
parseErrorAtCurrentToken(Diagnostics.Cannot_start_a_function_call_in_a_type_annotation);
nextToken();
return;
}

if (type && !canParseSemicolon()) {
if (initializer) {
parseErrorAtCurrentToken(Diagnostics._0_expected, tokenToString(SyntaxKind.SemicolonToken));
}
else {
parseErrorAtCurrentToken(Diagnostics.Missing_before_default_property_value);
}
return;
}

if (tryParseSemicolon()) {
return;
}

// If an initializer was parsed but there is still an error in finding the next semicolon,
// we generally know there was an error already reported in the initializer...
// class Example { a = new Map([), ) }
// ~
if (initializer) {
// ...unless we've found the start of a block after a property declaration, in which
// case we can know that regardless of the initializer we should complain on the block.
// class Example { a = 0 {} }
// ~
if (token() === SyntaxKind.OpenBraceToken) {
parseErrorAtCurrentToken(Diagnostics._0_expected, tokenToString(SyntaxKind.SemicolonToken));
}

return;
}

parseErrorForMissingSemicolonAfter(name);
}

function parseExpectedJSDoc(kind: JSDocSyntaxKind) {
if (token() === kind) {
nextTokenJSDoc();
Expand Down Expand Up @@ -1618,18 +1761,21 @@ namespace ts {
return token() === SyntaxKind.CloseBraceToken || token() === SyntaxKind.EndOfFileToken || scanner.hasPrecedingLineBreak();
}

function parseSemicolon(): boolean {
if (canParseSemicolon()) {
if (token() === SyntaxKind.SemicolonToken) {
// consume the semicolon if it was explicitly provided.
nextToken();
}

return true;
function tryParseSemicolon() {
if (!canParseSemicolon()) {
return false;
}
else {
return parseExpected(SyntaxKind.SemicolonToken);

if (token() === SyntaxKind.SemicolonToken) {
// consume the semicolon if it was explicitly provided.
nextToken();
}

return true;
}

function parseSemicolon(): boolean {
return tryParseSemicolon() || parseExpected(SyntaxKind.SemicolonToken);
}

function createNodeArray<T extends Node>(elements: T[], pos: number, end?: number, hasTrailingComma?: boolean): NodeArray<T> {
Expand Down Expand Up @@ -5888,7 +6034,9 @@ namespace ts {
identifierCount++;
expression = finishNode(factory.createIdentifier(""), getNodePos());
}
parseSemicolon();
if (!tryParseSemicolon()) {
parseErrorForMissingSemicolonAfter(expression);
}
return withJSDoc(finishNode(factory.createThrowStatement(expression), pos), hasJSDoc);
}

Expand Down Expand Up @@ -5951,7 +6099,9 @@ namespace ts {
node = factory.createLabeledStatement(expression, parseStatement());
}
else {
parseSemicolon();
if (!tryParseSemicolon()) {
parseErrorForMissingSemicolonAfter(expression);
}
node = factory.createExpressionStatement(expression);
if (hasParen) {
// do not parse the same jsdoc twice
Expand Down Expand Up @@ -6546,7 +6696,7 @@ namespace ts {
const exclamationToken = !questionToken && !scanner.hasPrecedingLineBreak() ? parseOptionalToken(SyntaxKind.ExclamationToken) : undefined;
const type = parseTypeAnnotation();
const initializer = doOutsideOfContext(NodeFlags.YieldContext | NodeFlags.AwaitContext | NodeFlags.DisallowInContext, parseInitializer);
parseSemicolon();
parseSemicolonAfterPropertyName(name, type, initializer);
const node = factory.createPropertyDeclaration(decorators, modifiers, name, questionToken || exclamationToken, type, initializer);
return withJSDoc(finishNode(node, pos), hasJSDoc);
}
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/scanner.ts
Expand Up @@ -77,7 +77,8 @@ namespace ts {
tryScan<T>(callback: () => T): T;
}

const textToKeywordObj: MapLike<KeywordSyntaxKind> = {
/** @internal */
export const textToKeywordObj: MapLike<KeywordSyntaxKind> = {
abstract: SyntaxKind.AbstractKeyword,
any: SyntaxKind.AnyKeyword,
as: SyntaxKind.AsKeyword,
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/ClassDeclaration26.errors.txt
@@ -1,4 +1,4 @@
tests/cases/compiler/ClassDeclaration26.ts(2,22): error TS1005: ';' expected.
tests/cases/compiler/ClassDeclaration26.ts(2,18): error TS1440: Variable declaration not allowed at this location.
tests/cases/compiler/ClassDeclaration26.ts(4,5): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
tests/cases/compiler/ClassDeclaration26.ts(4,20): error TS1005: ',' expected.
tests/cases/compiler/ClassDeclaration26.ts(4,23): error TS1005: '=>' expected.
Expand All @@ -8,8 +8,8 @@ tests/cases/compiler/ClassDeclaration26.ts(5,1): error TS1128: Declaration or st
==== tests/cases/compiler/ClassDeclaration26.ts (5 errors) ====
class C {
public const var export foo = 10;
~~~~~~
!!! error TS1005: ';' expected.
~~~
!!! error TS1440: Variable declaration not allowed at this location.

var constructor() { }
~~~
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/anonymousModules.errors.txt
@@ -1,24 +1,24 @@
tests/cases/compiler/anonymousModules.ts(1,1): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
tests/cases/compiler/anonymousModules.ts(1,8): error TS1005: ';' expected.
tests/cases/compiler/anonymousModules.ts(1,8): error TS1437: Namespace must be given a name.
tests/cases/compiler/anonymousModules.ts(4,2): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
tests/cases/compiler/anonymousModules.ts(4,9): error TS1005: ';' expected.
tests/cases/compiler/anonymousModules.ts(4,9): error TS1437: Namespace must be given a name.
tests/cases/compiler/anonymousModules.ts(10,2): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
tests/cases/compiler/anonymousModules.ts(10,9): error TS1005: ';' expected.
tests/cases/compiler/anonymousModules.ts(10,9): error TS1437: Namespace must be given a name.


==== tests/cases/compiler/anonymousModules.ts (6 errors) ====
module {
~~~~~~
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
~
!!! error TS1005: ';' expected.
!!! error TS1437: Namespace must be given a name.
export var foo = 1;

module {
~~~~~~
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
~
!!! error TS1005: ';' expected.
!!! error TS1437: Namespace must be given a name.
export var bar = 1;
}

Expand All @@ -28,7 +28,7 @@ tests/cases/compiler/anonymousModules.ts(10,9): error TS1005: ';' expected.
~~~~~~
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
~
!!! error TS1005: ';' expected.
!!! error TS1437: Namespace must be given a name.
var x = bar;
}
}
16 changes: 11 additions & 5 deletions tests/baselines/reference/classUpdateTests.errors.txt
Expand Up @@ -11,14 +11,16 @@ tests/cases/compiler/classUpdateTests.ts(95,1): error TS1128: Declaration or sta
tests/cases/compiler/classUpdateTests.ts(99,3): error TS1128: Declaration or statement expected.
tests/cases/compiler/classUpdateTests.ts(101,1): error TS1128: Declaration or statement expected.
tests/cases/compiler/classUpdateTests.ts(105,3): error TS1128: Declaration or statement expected.
tests/cases/compiler/classUpdateTests.ts(105,14): error TS1005: ';' expected.
tests/cases/compiler/classUpdateTests.ts(105,10): error TS1434: Unexpected keyword or identifier.
tests/cases/compiler/classUpdateTests.ts(105,14): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
tests/cases/compiler/classUpdateTests.ts(107,1): error TS1128: Declaration or statement expected.
tests/cases/compiler/classUpdateTests.ts(111,3): error TS1128: Declaration or statement expected.
tests/cases/compiler/classUpdateTests.ts(111,15): error TS1005: ';' expected.
tests/cases/compiler/classUpdateTests.ts(111,11): error TS1434: Unexpected keyword or identifier.
tests/cases/compiler/classUpdateTests.ts(111,15): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or statement expected.


==== tests/cases/compiler/classUpdateTests.ts (16 errors) ====
==== tests/cases/compiler/classUpdateTests.ts (18 errors) ====
//
// test codegen for instance properties
//
Expand Down Expand Up @@ -154,8 +156,10 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st
public this.p1 = 0; // ERROR
~~~~~~
!!! error TS1128: Declaration or statement expected.
~~~~
!!! error TS1434: Unexpected keyword or identifier.
~
!!! error TS1005: ';' expected.
!!! error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
}
}
~
Expand All @@ -166,8 +170,10 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st
private this.p1 = 0; // ERROR
~~~~~~~
!!! error TS1128: Declaration or statement expected.
~~~~
!!! error TS1434: Unexpected keyword or identifier.
~
!!! error TS1005: ';' expected.
!!! error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
}
}
~
Expand Down