Skip to content

Commit 2503e50

Browse files
Changed "ownership" relation of try blocks on throw statements.
A try-block now only owns a throw statement if its try statement has a catch-clause.
1 parent 3f56411 commit 2503e50

File tree

3 files changed

+80
-7
lines changed

3 files changed

+80
-7
lines changed

src/services/services.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,7 +2803,7 @@ module ts {
28032803

28042804
/**
28052805
* Aggregates all throw-statements within this node *without* crossing
2806-
* into function boundaries and try-blocks.
2806+
* into function boundaries and try-blocks with catch-clauses.
28072807
*/
28082808
function aggregateOwnedThrowStatements(node: Node): ThrowStatement[] {
28092809
var statementAccumulator: ThrowStatement[] = []
@@ -2816,10 +2816,16 @@ module ts {
28162816
}
28172817
else if (node.kind === SyntaxKind.TryStatement) {
28182818
var tryStatement = <TryStatement>node;
2819-
2819+
2820+
// If a try statement has a catch clause, then any thrown exceptions in the try block
2821+
// will be propagated to the next owner.
28202822
if (tryStatement.catchBlock) {
28212823
aggregate(tryStatement.catchBlock);
28222824
}
2825+
else {
2826+
aggregate(tryStatement.tryBlock);
2827+
}
2828+
28232829
if (tryStatement.finallyBlock) {
28242830
aggregate(tryStatement.finallyBlock);
28252831
}
@@ -2833,7 +2839,8 @@ module ts {
28332839

28342840
/**
28352841
* For lack of a better name, this function takes a throw statement and returns the first
2836-
* encountered ancestor that is a try-block, function-block, or source file.
2842+
* encountered ancestor that is a try-block (whose try statement has a catch clause),
2843+
* function-block, or source file.
28372844
*/
28382845
function getContextualThrowStatementOwner(throwStatement: ThrowStatement): Node {
28392846
var child: Node = throwStatement;
@@ -2845,10 +2852,14 @@ module ts {
28452852
return parent;
28462853
}
28472854

2848-
// A throw-statement is only owned by a try-statement if it occurs in the try block.
2849-
// Otherwise, it is owned by the next closest function-block or try-block.
2850-
if (parent.kind === SyntaxKind.TryStatement && child === (<TryStatement>parent).tryBlock) {
2851-
return child;
2855+
// A throw-statement is only owned by a try-statement if the try-statement has
2856+
// a catch clause, and if the throw-statement occurs within the try block.
2857+
if (parent.kind === SyntaxKind.TryStatement) {
2858+
var tryStatement = <TryStatement>parent;
2859+
2860+
if (tryStatement.tryBlock === child && tryStatement.catchBlock) {
2861+
return child;
2862+
}
28522863
}
28532864

28542865
child = parent;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////try {
4+
//// [|throw|] 10;
5+
////
6+
//// try {
7+
//// throw 10;
8+
//// }
9+
//// catch (x) {
10+
//// [|throw|] 10;
11+
//// }
12+
//// finally {
13+
//// [|throw|] 10;
14+
//// }
15+
////}
16+
////finally {
17+
//// [|throw|] 10;
18+
////}
19+
////
20+
////[|throw|] 10;
21+
22+
test.ranges().forEach(r => {
23+
goTo.position(r.start);
24+
25+
test.ranges().forEach(range => {
26+
verify.occurrencesAtPositionContains(range, false);
27+
});
28+
29+
verify.occurrencesAtPositionCount(test.ranges().length);
30+
});
31+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////try {
4+
//// throw 10;
5+
////
6+
//// try {
7+
//// [|throw|] 10;
8+
//// }
9+
//// catch (x) {
10+
//// throw 10;
11+
//// }
12+
//// finally {
13+
//// throw 10;
14+
//// }
15+
////}
16+
////finally {
17+
//// throw 10;
18+
////}
19+
////
20+
////throw 10;
21+
22+
test.ranges().forEach(r => {
23+
goTo.position(r.start);
24+
25+
test.ranges().forEach(range => {
26+
verify.occurrencesAtPositionContains(range, false);
27+
});
28+
29+
verify.occurrencesAtPositionCount(test.ranges().length);
30+
});
31+

0 commit comments

Comments
 (0)