Skip to content

Commit 79735b4

Browse files
Simplified error recovery by just using 'parseTryStatement'.
1 parent 0939f77 commit 79735b4

File tree

4 files changed

+8
-47
lines changed

4 files changed

+8
-47
lines changed

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ module ts {
160160
Constructor_implementation_expected: { code: 2240, category: DiagnosticCategory.Error, key: "Constructor implementation expected." },
161161
An_export_assignment_cannot_be_used_in_a_module_with_other_exported_elements: { code: 2245, category: DiagnosticCategory.Error, key: "An export assignment cannot be used in a module with other exported elements." },
162162
A_parameter_property_is_only_allowed_in_a_constructor_implementation: { code: 2246, category: DiagnosticCategory.Error, key: "A parameter property is only allowed in a constructor implementation." },
163-
A_catch_clause_must_be_preceded_by_a_try_statement: { code: 2249, category: DiagnosticCategory.Error, key: "A 'catch' clause must be preceded by a 'try' statement." },
164-
A_finally_block_must_be_preceded_by_a_try_statement: { code: 2250, category: DiagnosticCategory.Error, key: "A 'finally' block must be preceded by a 'try' statement." },
165163
Circular_definition_of_import_alias_0: { code: 3000, category: DiagnosticCategory.Error, key: "Circular definition of import alias '{0}'." },
166164
Cannot_find_name_0: { code: 3001, category: DiagnosticCategory.Error, key: "Cannot find name '{0}'." },
167165
Module_0_has_no_exported_member_1: { code: 3002, category: DiagnosticCategory.Error, key: "Module '{0}' has no exported member '{1}'." },

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -632,14 +632,6 @@
632632
"category": "Error",
633633
"code": 2246
634634
},
635-
"A 'catch' clause must be preceded by a 'try' statement.": {
636-
"category": "Error",
637-
"code": 2249
638-
},
639-
"A 'finally' block must be preceded by a 'try' statement.": {
640-
"category": "Error",
641-
"code": 2250
642-
},
643635

644636
"Circular definition of import alias '{0}'.": {
645637
"category": "Error",
@@ -1246,4 +1238,4 @@
12461238
"category": "Error",
12471239
"code": -9999999
12481240
}
1249-
}
1241+
}

src/compiler/parser.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,35 +2542,6 @@ module ts {
25422542
return finishNode(node);
25432543
}
25442544

2545-
// This function is used for parsing 'catch'/'finally' blocks
2546-
// in spite of them missing a 'try' statement.
2547-
function parseCatchOrFinallyBlocksMissingTryStatement(): TryStatement {
2548-
2549-
Debug.assert(token === SyntaxKind.CatchKeyword || token === SyntaxKind.FinallyKeyword,
2550-
"'parseCatchOrFinallyBlocksMissingTryStatement' should only be called when the current token is a 'catch' or 'finally' keyword.");
2551-
2552-
// We're just going to return a bogus TryStatement.
2553-
var node = <TryStatement>createNode(SyntaxKind.TryStatement);
2554-
node.tryBlock = <Block>createNode(SyntaxKind.Block);
2555-
node.tryBlock.statements = createMissingList<Statement>();
2556-
2557-
if (token === SyntaxKind.CatchKeyword) {
2558-
error(Diagnostics.A_catch_clause_must_be_preceded_by_a_try_statement);
2559-
node.catchBlock = parseCatchBlock();
2560-
}
2561-
2562-
if (token === SyntaxKind.FinallyKeyword) {
2563-
// Only report an error on the 'finally' block if we haven't on the 'catch' block.
2564-
if (node.catchBlock === undefined) {
2565-
error(Diagnostics.A_finally_block_must_be_preceded_by_a_try_statement);
2566-
}
2567-
2568-
node.finallyBlock = parseTokenAndBlock(SyntaxKind.FinallyKeyword, SyntaxKind.FinallyBlock);
2569-
}
2570-
2571-
return finishNode(node);
2572-
}
2573-
25742545
function parseTokenAndBlock(token: SyntaxKind, kind: SyntaxKind): Block {
25752546
var pos = getNodePos();
25762547
parseExpected(token);
@@ -2733,10 +2704,10 @@ module ts {
27332704
case SyntaxKind.ThrowKeyword:
27342705
return parseThrowStatement();
27352706
case SyntaxKind.TryKeyword:
2736-
return parseTryStatement();
2707+
// Include the next two for error recovery.
27372708
case SyntaxKind.CatchKeyword:
27382709
case SyntaxKind.FinallyKeyword:
2739-
return parseCatchOrFinallyBlocksMissingTryStatement();
2710+
return parseTryStatement();
27402711
case SyntaxKind.DebuggerKeyword:
27412712
return parseDebuggerStatement();
27422713
default:

tests/baselines/reference/invalidTryStatements2.errors.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88

99
catch(x) { } // error missing try
1010
~~~~~
11-
!!! A 'catch' clause must be preceded by a 'try' statement.
11+
!!! 'try' expected.
1212

1313
finally{ } // potential error; can be absorbed by the 'catch'
1414
}
1515

1616
function fn2() {
1717
finally { } // error missing try
1818
~~~~~~~
19-
!!! A 'finally' block must be preceded by a 'try' statement.
19+
!!! 'try' expected.
2020
catch (x) { } // error missing try
2121
~~~~~
22-
!!! A 'catch' clause must be preceded by a 'try' statement.
22+
!!! 'try' expected.
2323

2424
// no error
2525
try {
@@ -30,12 +30,12 @@
3030
// error missing try
3131
finally {
3232
~~~~~~~
33-
!!! A 'finally' block must be preceded by a 'try' statement.
33+
!!! 'try' expected.
3434
}
3535

3636
// error missing try
3737
catch (x) {
3838
~~~~~
39-
!!! A 'catch' clause must be preceded by a 'try' statement.
39+
!!! 'try' expected.
4040
}
4141
}

0 commit comments

Comments
 (0)