Skip to content

Commit 50d0cdc

Browse files
Better coverage against function boundaries.
1 parent 232e513 commit 50d0cdc

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/services/services.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,8 +2168,7 @@ module ts {
21682168

21692169
var result: ReferenceEntry[];
21702170

2171-
// Each of these helper functions bails out if the node is undefined,
2172-
// which is why you'll see much of this 'node.parent && node.parent.parent' pattern.
2171+
// 'parent' and 'hasKind' are falsey-propagating convenience functions.
21732172
switch (node.kind) {
21742173
case SyntaxKind.TryKeyword:
21752174
case SyntaxKind.CatchKeyword:
@@ -2201,14 +2200,14 @@ module ts {
22012200
function getTryCatchFinallyOccurrences(tryStatement: TryStatement): ReferenceEntry[] {
22022201
var keywords: Node[] = [];
22032202

2204-
pushIfKeyword(keywords, tryStatement.getFirstToken(), SyntaxKind.TryKeyword);
2203+
pushKeyword(keywords, tryStatement.getFirstToken(), SyntaxKind.TryKeyword);
22052204

22062205
if (tryStatement.catchBlock) {
2207-
pushIfKeyword(keywords, tryStatement.catchBlock.getFirstToken(), SyntaxKind.CatchKeyword);
2206+
pushKeyword(keywords, tryStatement.catchBlock.getFirstToken(), SyntaxKind.CatchKeyword);
22082207
}
22092208

22102209
if (tryStatement.finallyBlock) {
2211-
pushIfKeyword(keywords, tryStatement.finallyBlock.getFirstToken(), SyntaxKind.FinallyKeyword);
2210+
pushKeyword(keywords, tryStatement.finallyBlock.getFirstToken(), SyntaxKind.FinallyKeyword);
22122211
}
22132212

22142213
return keywordsToReferenceEntries(keywords);
@@ -2217,33 +2216,33 @@ module ts {
22172216
function getSwitchCaseDefaultOccurrences(switchStatement: SwitchStatement) {
22182217
var keywords: Node[] = [];
22192218

2220-
pushIfKeyword(keywords, switchStatement.getFirstToken(), SyntaxKind.SwitchKeyword);
2219+
pushKeyword(keywords, switchStatement.getFirstToken(), SyntaxKind.SwitchKeyword);
22212220

22222221
// Go through each clause in the switch statement, collecting the clause keywords.
22232222
switchStatement.clauses.forEach(clause => {
2224-
pushIfKeyword(keywords, clause.getFirstToken(), [SyntaxKind.CaseKeyword, SyntaxKind.DefaultKeyword]);
2223+
pushKeyword(keywords, clause.getFirstToken(), [SyntaxKind.CaseKeyword, SyntaxKind.DefaultKeyword]);
22252224

22262225
// For each clause, also recursively traverse the statements where we can find analogous breaks.
22272226
forEachChild(clause, function aggregateBreakKeywords(node: Node): void {
22282227
switch (node.kind) {
22292228
case SyntaxKind.BreakStatement:
22302229
// If the break statement has a label, it cannot be part of a switch block.
22312230
if (!(<BreakOrContinueStatement>node).label) {
2232-
pushIfKeyword(keywords, node.getFirstToken(), SyntaxKind.BreakKeyword);
2231+
pushKeyword(keywords, node.getFirstToken(), SyntaxKind.BreakKeyword);
22332232
}
22342233
// Fall through
22352234
case SyntaxKind.ForStatement:
22362235
case SyntaxKind.ForInStatement:
22372236
case SyntaxKind.DoStatement:
22382237
case SyntaxKind.WhileStatement:
22392238
case SyntaxKind.SwitchStatement:
2240-
case SyntaxKind.FunctionExpression:
2241-
case SyntaxKind.FunctionDeclaration:
2242-
case SyntaxKind.ArrowFunction:
22432239
return;
22442240
}
22452241

2246-
forEachChild(node, aggregateBreakKeywords);
2242+
// Do not cross function boundaries.
2243+
if (!isAnyFunction(node)) {
2244+
forEachChild(node, aggregateBreakKeywords);
2245+
}
22472246
});
22482247
});
22492248

@@ -2264,12 +2263,14 @@ module ts {
22642263
case SyntaxKind.WhileStatement:
22652264
// TODO (drosen): Handle loops!
22662265
return undefined;
2267-
case SyntaxKind.FunctionExpression:
2268-
case SyntaxKind.FunctionDeclaration:
2269-
case SyntaxKind.ArrowFunction:
2270-
return undefined;
2266+
22712267
case SyntaxKind.SwitchStatement:
22722268
return getSwitchCaseDefaultOccurrences(<SwitchStatement>owner);
2269+
2270+
default:
2271+
if (isAnyFunction(owner)) {
2272+
return undefined;
2273+
}
22732274
}
22742275
}
22752276

@@ -2286,9 +2287,9 @@ module ts {
22862287
return node && node.parent;
22872288
}
22882289

2289-
function pushIfKeyword(keywordList: Node[], token: Node, expected: SyntaxKind): void;
2290-
function pushIfKeyword(keywordList: Node[], token: Node, expected: SyntaxKind[]): void;
2291-
function pushIfKeyword(keywordList: Node[], token: Node, expected: any): void {
2290+
function pushKeyword(keywordList: Node[], token: Node, expected: SyntaxKind): void;
2291+
function pushKeyword(keywordList: Node[], token: Node, expected: SyntaxKind[]): void;
2292+
function pushKeyword(keywordList: Node[], token: Node, expected: any): void {
22922293
if (!token) {
22932294
return;
22942295
}

0 commit comments

Comments
 (0)