Skip to content
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
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1261,4 +1261,4 @@
"category": "Error",
"code": -9999999
}
}
}
15 changes: 13 additions & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2652,20 +2652,28 @@ module ts {
case SyntaxKind.ThrowKeyword:
case SyntaxKind.TryKeyword:
case SyntaxKind.DebuggerKeyword:
// 'catch' and 'finally' do not actually indicate that the code is part of a statement,
// however, we say they are here so that we may gracefully parse them and error later.
case SyntaxKind.CatchKeyword:
case SyntaxKind.FinallyKeyword:
return true;
case SyntaxKind.InterfaceKeyword:
case SyntaxKind.ClassKeyword:
case SyntaxKind.ModuleKeyword:
case SyntaxKind.EnumKeyword:
// When followed by an identifier, these do not start a statement but might
// instead be following declarations
if (isDeclaration()) return false;
if (isDeclaration()) {
return false;
}
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.StaticKeyword:
// When followed by an identifier or keyword, these do not start a statement but
// might instead be following type members
if (lookAhead(() => nextToken() >= SyntaxKind.Identifier)) return false;
if (lookAhead(() => nextToken() >= SyntaxKind.Identifier)) {
return false;
}
default:
return isExpression();
}
Expand Down Expand Up @@ -2702,6 +2710,9 @@ module ts {
case SyntaxKind.ThrowKeyword:
return parseThrowStatement();
case SyntaxKind.TryKeyword:
// Include the next two for error recovery.
case SyntaxKind.CatchKeyword:
case SyntaxKind.FinallyKeyword:
return parseTryStatement();
case SyntaxKind.DebuggerKeyword:
return parseDebuggerStatement();
Expand Down
35 changes: 29 additions & 6 deletions tests/baselines/reference/invalidTryStatements2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
==== tests/cases/conformance/statements/tryStatements/invalidTryStatements2.ts (4 errors) ====
==== tests/cases/conformance/statements/tryStatements/invalidTryStatements2.ts (6 errors) ====
function fn() {
try {
} catch { // syntax error, missing '(x)'
Expand All @@ -8,11 +8,34 @@

catch(x) { } // error missing try
~~~~~
!!! Statement expected.
~
!!! '=>' expected.
!!! 'try' expected.

finally{ } // error missing try
finally{ } // potential error; can be absorbed by the 'catch'
}

function fn2() {
finally { } // error missing try
~~~~~~~
!!! 'try' expected.
catch (x) { } // error missing try
~~~~~
!!! 'try' expected.

// no error
try {
}
finally {
}

// error missing try
finally {
~~~~~~~
!!! Statement expected.
!!! 'try' expected.
}

// error missing try
catch (x) {
~~~~~
!!! 'try' expected.
}
}
4 changes: 3 additions & 1 deletion tests/baselines/reference/parserMissingToken1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
==== tests/cases/conformance/parser/ecmascript5/MissingTokens/parserMissingToken1.ts (2 errors) ====
==== tests/cases/conformance/parser/ecmascript5/MissingTokens/parserMissingToken1.ts (3 errors) ====
a / finally
~~~~~~~
!!! Expression expected.

!!! '{' expected.
~
!!! Cannot find name 'a'.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,24 @@ function fn() {

catch(x) { } // error missing try

finally{ } // error missing try
finally{ } // potential error; can be absorbed by the 'catch'
}

function fn2() {
finally { } // error missing try
catch (x) { } // error missing try

// no error
try {
}
finally {
}

// error missing try
finally {
}

// error missing try
catch (x) {
}
}