Skip to content

Commit 1f77198

Browse files
Made getOccs more resilient.
1 parent 558be4e commit 1f77198

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

src/compiler/parser.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,22 @@ module ts {
483483
nodeIsNestedInLabel(label: Identifier, requireIterationStatement: boolean, stopAtFunctionBoundary: boolean): ControlBlockContext;
484484
}
485485

486+
export function isKeyword(token: SyntaxKind): boolean {
487+
return SyntaxKind.FirstKeyword <= token && token <= SyntaxKind.LastKeyword;
488+
}
489+
490+
export function isModifier(token: SyntaxKind): boolean {
491+
switch (token) {
492+
case SyntaxKind.PublicKeyword:
493+
case SyntaxKind.PrivateKeyword:
494+
case SyntaxKind.StaticKeyword:
495+
case SyntaxKind.ExportKeyword:
496+
case SyntaxKind.DeclareKeyword:
497+
return true;
498+
}
499+
return false;
500+
}
501+
486502
export function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen: boolean = false): SourceFile {
487503
var file: SourceFile;
488504
var scanner: Scanner;
@@ -853,23 +869,6 @@ module ts {
853869
return parseIdentifierName();
854870
}
855871

856-
857-
function isKeyword(token: SyntaxKind): boolean {
858-
return SyntaxKind.FirstKeyword <= token && token <= SyntaxKind.LastKeyword;
859-
}
860-
861-
function isModifier(token: SyntaxKind): boolean {
862-
switch (token) {
863-
case SyntaxKind.PublicKeyword:
864-
case SyntaxKind.PrivateKeyword:
865-
case SyntaxKind.StaticKeyword:
866-
case SyntaxKind.ExportKeyword:
867-
case SyntaxKind.DeclareKeyword:
868-
return true;
869-
}
870-
return false;
871-
}
872-
873872
function parseContextualModifier(t: SyntaxKind): boolean {
874873
return token === t && tryParse(() => {
875874
nextToken();

src/services/services.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,27 +2172,37 @@ module ts {
21722172
case SyntaxKind.TryKeyword:
21732173
case SyntaxKind.CatchKeyword:
21742174
case SyntaxKind.FinallyKeyword:
2175-
return getTryCatchFinallyOccurrences(<TryStatement>node.parent.parent);
2175+
return getTryCatchFinallyOccurrences(<TryStatement>(node.parent && node.parent.parent));
21762176
}
21772177

21782178
return undefined;
21792179

2180-
function getTryCatchFinallyOccurrences(tryStatement: TryStatement): ReferenceEntry[] {
2180+
function getTryCatchFinallyOccurrences(tryStatement: TryStatement): ReferenceEntry[]{
2181+
if (!tryStatement || tryStatement.kind !== SyntaxKind.TryStatement) {
2182+
return undefined;
2183+
}
2184+
21812185
var keywords: Node[] = [];
21822186

2183-
keywords.push(tryStatement.getFirstToken())
2187+
pushIfKeyword(keywords, tryStatement.getFirstToken());
21842188

21852189
if (tryStatement.catchBlock) {
2186-
keywords.push(tryStatement.catchBlock.getFirstToken());
2190+
pushIfKeyword(keywords, tryStatement.catchBlock.getFirstToken());
21872191
}
21882192

21892193
if (tryStatement.finallyBlock) {
2190-
keywords.push(tryStatement.finallyBlock.getFirstToken());
2194+
pushIfKeyword(keywords, tryStatement.finallyBlock.getFirstToken());
21912195
}
21922196

21932197
return keywordsToReferenceEntries(keywords);
21942198
}
21952199

2200+
function pushIfKeyword(keywordList: Node[], token: Node) {
2201+
if (token && isKeyword(token.kind)) {
2202+
keywordList.push(token);
2203+
}
2204+
}
2205+
21962206
function keywordsToReferenceEntries(keywords: Node[]): ReferenceEntry[]{
21972207
return keywords.map(keyword =>
21982208
new ReferenceEntry(filename, TypeScript.TextSpan.fromBounds(keyword.getStart(), keyword.end), /* isWriteAccess */ false)

0 commit comments

Comments
 (0)