Add better error recovery logic for cases with line ending with "id." followed by a declaration e.g. "class id"

This commit is contained in:
Mohamed Hegazy 2014-09-12 16:48:52 -07:00
parent 085cabf8e0
commit 5d577df69e
4 changed files with 37 additions and 9 deletions

View File

@ -2198,10 +2198,38 @@ module ts {
function parseCallAndAccess(expr: Expression, inNewExpression: boolean): Expression {
while (true) {
var dotStart = scanner.getTokenPos();
if (parseOptional(SyntaxKind.DotToken)) {
var propertyAccess = <PropertyAccess>createNode(SyntaxKind.PropertyAccess, expr.pos);
// Technically a keyword is valid here as all keywords are identifier names.
// However, often we'll encounter this in error situations when the keyword
// is actually starting another valid construct.
//
// So, we check for the following specific case:
//
// name.
// keyword identifierNameOrKeyword
//
// Note: the newlines are important here. For example, if that above code
// were rewritten into:
//
// name.keyword
// identifierNameOrKeyword
//
// Then we would consider it valid. That's because ASI would take effect and
// the code would be implicitly: "name.keyword; identifierNameOrKeyword".
// In the first case though, ASI will not take effect because there is not a
// line terminator after the keyword.
if (scanner.hasPrecedingLineBreak() && scanner.isReservedWord() && lookAhead(() => scanner.isReservedWord())) {
grammarErrorAtPos(dotStart, scanner.getStartPos() - dotStart, Diagnostics.Identifier_expected);
var id = <Identifier>createMissingNode();
}
else {
var id = parseIdentifierName();
}
propertyAccess.left = expr;
propertyAccess.right = parseIdentifierName();
propertyAccess.right = id;
expr = finishNode(propertyAccess);
continue;
}

View File

@ -3,9 +3,9 @@
IgnoreRulesSpecific = 0,
}
var x = IgnoreRulesSpecific.
~
!!! Identifier expected.
~~~~~~~~~~~~~~~~~~~
!!! Cannot find name 'IgnoreRulesSpecific'.
var y = Position.IgnoreRulesSpecific;
~
!!! ',' expected.

View File

@ -3,10 +3,10 @@
IgnoreRulesSpecific = 0
}
var x = IgnoreRulesSpecific. // error
~
!!! Identifier expected.
~~~~~~~~~~~~~~~~~~~
!!! Cannot find name 'IgnoreRulesSpecific'.
var y = 1;
~
!!! ',' expected.
var z = Position2.IgnoreRulesSpecific; // no error

View File

@ -16,9 +16,9 @@
// Verify the memberlist of module when the following line has a keyword
goTo.marker('namedType');
verify.completionListContains('C1', 'TypeModule1.C1');
verify.completionListContains('C2', 'TypeModule1.C2');
verify.completionListContains('C1');
verify.completionListContains('C2');
goTo.marker('dotedExpression');
verify.completionListContains('C1', 'TypeModule1.C1');
verify.completionListContains('C2', 'TypeModule1.C2');
verify.completionListContains('C1');
verify.completionListContains('C2');