Skip to content

Commit 232e513

Browse files
Moved null-guards to appropriate places, added helpers.
1 parent 3825c9b commit 232e513

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

src/services/services.ts

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,62 +2174,62 @@ module ts {
21742174
case SyntaxKind.TryKeyword:
21752175
case SyntaxKind.CatchKeyword:
21762176
case SyntaxKind.FinallyKeyword:
2177-
result = getTryCatchFinallyOccurrences(<TryStatement>(node.parent && node.parent.parent));
2177+
if (hasKind(parent(parent(node)), SyntaxKind.TryStatement)) {
2178+
result = getTryCatchFinallyOccurrences(<TryStatement>(node.parent.parent));
2179+
}
21782180
break;
21792181
case SyntaxKind.SwitchKeyword:
2180-
result = getSwitchCaseDefaultOccurrences(<SwitchStatement>node.parent);
2182+
if (hasKind(node.parent, SyntaxKind.SwitchStatement)) {
2183+
result = getSwitchCaseDefaultOccurrences(<SwitchStatement>node.parent);
2184+
}
21812185
break;
21822186
case SyntaxKind.CaseKeyword:
21832187
case SyntaxKind.DefaultKeyword:
2184-
result = getSwitchCaseDefaultOccurrences(<SwitchStatement>(node.parent && node.parent.parent));
2188+
if (hasKind(parent(parent(node)), SyntaxKind.SwitchStatement)) {
2189+
result = getSwitchCaseDefaultOccurrences(<SwitchStatement>(node.parent.parent));
2190+
}
21852191
break;
21862192
case SyntaxKind.BreakKeyword:
2187-
result = getBreakStatementOccurences(<BreakOrContinueStatement>node.parent);
2188-
2193+
if (hasKind(node.parent, SyntaxKind.BreakStatement)) {
2194+
result = getBreakStatementOccurences(<BreakOrContinueStatement>node.parent);
2195+
}
2196+
break;
21892197
}
21902198

21912199
return result;
21922200

21932201
function getTryCatchFinallyOccurrences(tryStatement: TryStatement): ReferenceEntry[] {
2194-
if (!tryStatement || tryStatement.kind !== SyntaxKind.TryStatement) {
2195-
return undefined;
2196-
}
2197-
21982202
var keywords: Node[] = [];
21992203

2200-
pushIfKeyword(keywords, tryStatement.getFirstToken());
2204+
pushIfKeyword(keywords, tryStatement.getFirstToken(), SyntaxKind.TryKeyword);
22012205

22022206
if (tryStatement.catchBlock) {
2203-
pushIfKeyword(keywords, tryStatement.catchBlock.getFirstToken());
2207+
pushIfKeyword(keywords, tryStatement.catchBlock.getFirstToken(), SyntaxKind.CatchKeyword);
22042208
}
22052209

22062210
if (tryStatement.finallyBlock) {
2207-
pushIfKeyword(keywords, tryStatement.finallyBlock.getFirstToken());
2211+
pushIfKeyword(keywords, tryStatement.finallyBlock.getFirstToken(), SyntaxKind.FinallyKeyword);
22082212
}
22092213

22102214
return keywordsToReferenceEntries(keywords);
22112215
}
22122216

22132217
function getSwitchCaseDefaultOccurrences(switchStatement: SwitchStatement) {
2214-
if (!switchStatement || switchStatement.kind !== SyntaxKind.SwitchStatement) {
2215-
return undefined;
2216-
}
2217-
22182218
var keywords: Node[] = [];
22192219

2220-
pushIfKeyword(keywords, switchStatement.getFirstToken());
2220+
pushIfKeyword(keywords, switchStatement.getFirstToken(), SyntaxKind.SwitchKeyword);
22212221

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

22262226
// For each clause, also recursively traverse the statements where we can find analogous breaks.
22272227
forEachChild(clause, function aggregateBreakKeywords(node: Node): void {
22282228
switch (node.kind) {
22292229
case SyntaxKind.BreakStatement:
2230-
// If the break statement has a label, cannot be part of
2230+
// If the break statement has a label, it cannot be part of a switch block.
22312231
if (!(<BreakOrContinueStatement>node).label) {
2232-
pushIfKeyword(keywords, node.getFirstToken());
2232+
pushIfKeyword(keywords, node.getFirstToken(), SyntaxKind.BreakKeyword);
22332233
}
22342234
// Fall through
22352235
case SyntaxKind.ForStatement:
@@ -2251,10 +2251,6 @@ module ts {
22512251
}
22522252

22532253
function getBreakStatementOccurences(breakStatement: BreakOrContinueStatement): ReferenceEntry[]{
2254-
if (!breakStatement || breakStatement.kind !== SyntaxKind.BreakStatement) {
2255-
return undefined;
2256-
}
2257-
22582254
// TODO (drosen): Deal with labeled statements.
22592255
if (breakStatement.label) {
22602256
return undefined;
@@ -2280,10 +2276,30 @@ module ts {
22802276
return undefined;
22812277
}
22822278

2283-
function pushIfKeyword(keywordList: Node[], token: Node) {
2284-
if (token && isKeyword(token.kind)) {
2279+
// returns true if 'node' is defined and has a matching 'kind'.
2280+
function hasKind(node: Node, kind: SyntaxKind) {
2281+
return !!(node && node.kind === kind);
2282+
}
2283+
2284+
// Null-propagating 'parent' function.
2285+
function parent(node: Node): Node {
2286+
return node && node.parent;
2287+
}
2288+
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 {
2292+
if (!token) {
2293+
return;
2294+
}
2295+
2296+
if (token.kind === expected ||
2297+
(expected.length && (<SyntaxKind[]>expected).some(expectedKind => expectedKind === token.kind))) {
22852298
keywordList.push(token);
22862299
}
2300+
else {
2301+
Debug.assert("Expected keyword, got " + token.getFullText().substring(token.getLeadingTriviaWidth()));
2302+
}
22872303
}
22882304

22892305
function keywordsToReferenceEntries(keywords: Node[]): ReferenceEntry[]{

0 commit comments

Comments
 (0)