Skip to content

Commit 0e93d28

Browse files
Separated 'super'/'this' keyword searching to simplify logic.
1 parent 131ac2f commit 0e93d28

File tree

1 file changed

+79
-59
lines changed

1 file changed

+79
-59
lines changed

src/services/services.ts

Lines changed: 79 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,6 @@ module ts {
21342134
return result;
21352135
}
21362136

2137-
/// Find references
21382137
function getOccurrencesAtPosition(filename: string, position: number): ReferenceEntry[] {
21392138
synchronizeHostData();
21402139

@@ -2404,8 +2403,12 @@ module ts {
24042403
}
24052404
}
24062405

2407-
if (node.kind === SyntaxKind.ThisKeyword || node.kind === SyntaxKind.SuperKeyword) {
2408-
return getReferencesForThisOrSuperKeyword(node, sourceFiles);
2406+
if (node.kind === SyntaxKind.ThisKeyword) {
2407+
return getReferencesForThisKeyword(node, sourceFiles);
2408+
}
2409+
2410+
if (node.kind === SyntaxKind.SuperKeyword) {
2411+
return getReferencesForSuperKeyword(node);
24092412
}
24102413

24112414
var symbol = typeInfoResolver.getSymbolInfo(node);
@@ -2629,24 +2632,57 @@ module ts {
26292632
}
26302633
}
26312634

2632-
function getReferencesForThisOrSuperKeyword(thisOrSuperKeyword: Node, sourceFiles: SourceFile[]): ReferenceEntry[] {
2633-
var keywordName: string;
2634-
var searchSpaceNode: Node;
2635-
2636-
if (thisOrSuperKeyword.kind === SyntaxKind.ThisKeyword) {
2637-
keywordName = "this"
2638-
searchSpaceNode = getThisContainer(thisOrSuperKeyword, /* includeArrowFunctions */ false);
2635+
function getReferencesForSuperKeyword(superKeyword: Node): ReferenceEntry[]{
2636+
var searchSpaceNode = getSuperContainer(superKeyword);
2637+
if (!searchSpaceNode) {
2638+
return undefined;
26392639
}
2640-
else {
2641-
keywordName = "super";
2642-
searchSpaceNode = getSuperContainer(thisOrSuperKeyword);
2640+
// Whether 'super' occurs in a static context within a class.
2641+
var staticFlag = NodeFlags.Static;
26432642

2644-
if (!searchSpaceNode) {
2643+
switch (searchSpaceNode.kind) {
2644+
case SyntaxKind.Property:
2645+
case SyntaxKind.Method:
2646+
case SyntaxKind.Constructor:
2647+
case SyntaxKind.GetAccessor:
2648+
case SyntaxKind.SetAccessor:
2649+
staticFlag &= searchSpaceNode.flags;
2650+
searchSpaceNode = searchSpaceNode.parent; // re-assign to be the owning class
2651+
break;
2652+
default:
26452653
return undefined;
2646-
}
26472654
}
26482655

2649-
// Whether 'this'/'super' occurs in a static context within a class.
2656+
var result: ReferenceEntry[] = [];
2657+
2658+
var sourceFile = searchSpaceNode.getSourceFile();
2659+
var possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "super", searchSpaceNode.getStart(), searchSpaceNode.getEnd());
2660+
forEach(possiblePositions, position => {
2661+
cancellationToken.throwIfCancellationRequested();
2662+
2663+
var node = getNodeAtPosition(sourceFile, position);
2664+
2665+
if (!node || node.kind !== SyntaxKind.SuperKeyword) {
2666+
return;
2667+
}
2668+
2669+
var container = getSuperContainer(node);
2670+
2671+
// If we have a 'super' container, we must have an enclosing class.
2672+
// Now make sure the owning class is the same as the search-space
2673+
// and has the same static qualifier as the original 'super's owner.
2674+
if (container && (NodeFlags.Static & container.flags) === staticFlag && container.parent.symbol === searchSpaceNode.symbol) {
2675+
result.push(getReferenceEntryFromNode(node));
2676+
}
2677+
});
2678+
2679+
return result;
2680+
}
2681+
2682+
function getReferencesForThisKeyword(thisOrSuperKeyword: Node, sourceFiles: SourceFile[]): ReferenceEntry[] {
2683+
var searchSpaceNode = getThisContainer(thisOrSuperKeyword, /* includeArrowFunctions */ false);
2684+
2685+
// Whether 'this' occurs in a static context within a class.
26502686
var staticFlag = NodeFlags.Static;
26512687

26522688
switch (searchSpaceNode.kind) {
@@ -2662,13 +2698,9 @@ module ts {
26622698
if (isExternalModule(<SourceFile>searchSpaceNode)) {
26632699
return undefined;
26642700
}
2665-
break;
2701+
// Fall through
26662702
case SyntaxKind.FunctionDeclaration:
26672703
case SyntaxKind.FunctionExpression:
2668-
// 'super' can only occur within a class.
2669-
if (thisOrSuperKeyword.kind === SyntaxKind.SuperKeyword) {
2670-
return undefined;
2671-
}
26722704
break;
26732705
default:
26742706
return undefined;
@@ -2678,60 +2710,48 @@ module ts {
26782710

26792711
if (searchSpaceNode.kind === SyntaxKind.SourceFile) {
26802712
forEach(sourceFiles, sourceFile => {
2681-
var possiblePositions = getPossibleSymbolReferencePositions(sourceFile, keywordName, sourceFile.getStart(), sourceFile.getEnd());
2682-
getThisOrSuperReferencesInFile(sourceFile, sourceFile, possiblePositions, result);
2713+
var possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this", sourceFile.getStart(), sourceFile.getEnd());
2714+
getThisReferencesInFile(sourceFile, sourceFile, possiblePositions, result);
26832715
});
26842716
}
26852717
else {
26862718
var sourceFile = searchSpaceNode.getSourceFile();
2687-
var possiblePositions = getPossibleSymbolReferencePositions(sourceFile, keywordName, searchSpaceNode.getStart(), searchSpaceNode.getEnd());
2688-
getThisOrSuperReferencesInFile(sourceFile, searchSpaceNode, possiblePositions, result);
2719+
var possiblePositions = getPossibleSymbolReferencePositions(sourceFile, "this", searchSpaceNode.getStart(), searchSpaceNode.getEnd());
2720+
getThisReferencesInFile(sourceFile, searchSpaceNode, possiblePositions, result);
26892721
}
26902722

26912723
return result;
26922724

2693-
function getThisOrSuperReferencesInFile(sourceFile: SourceFile, searchSpaceNode: Node, possiblePositions: number[], result: ReferenceEntry[]): void {
2725+
function getThisReferencesInFile(sourceFile: SourceFile, searchSpaceNode: Node, possiblePositions: number[], result: ReferenceEntry[]): void {
26942726
forEach(possiblePositions, position => {
26952727
cancellationToken.throwIfCancellationRequested();
26962728

26972729
var node = getNodeAtPosition(sourceFile, position);
2698-
if (!node) {
2730+
if (!node || node.kind !== SyntaxKind.ThisKeyword) {
26992731
return;
27002732
}
27012733

2702-
var container = getNodeAtPosition(sourceFile, position);
2703-
if (!container) {
2704-
return;
2705-
}
2706-
2707-
if (node.kind === SyntaxKind.SuperKeyword) {
2708-
container = getSuperContainer(node);
2709-
}
2710-
else if (node.kind === SyntaxKind.ThisKeyword) {
2711-
container = getThisContainer(node, /* includeArrowFunctions */ false);
2712-
}
2734+
var container = getThisContainer(node, /* includeArrowFunctions */ false);
27132735

2714-
if (container) {
2715-
switch (searchSpaceNode.kind) {
2716-
case SyntaxKind.FunctionExpression:
2717-
case SyntaxKind.FunctionDeclaration:
2718-
if (searchSpaceNode.symbol === container.symbol) {
2719-
result.push(getReferenceEntryFromNode(node));
2720-
}
2721-
break;
2722-
case SyntaxKind.ClassDeclaration:
2723-
// Make sure the container belongs to the same class
2724-
// and has the appropriate static modifier from the original container.
2725-
if (container.parent && searchSpaceNode.symbol === container.parent.symbol && (container.flags & NodeFlags.Static) === staticFlag) {
2726-
result.push(getReferenceEntryFromNode(node));
2727-
}
2728-
break;
2729-
case SyntaxKind.SourceFile:
2730-
if (container.kind === SyntaxKind.SourceFile && !isExternalModule(<SourceFile>container)) {
2731-
result.push(getReferenceEntryFromNode(node));
2732-
}
2733-
break;
2734-
}
2736+
switch (searchSpaceNode.kind) {
2737+
case SyntaxKind.FunctionExpression:
2738+
case SyntaxKind.FunctionDeclaration:
2739+
if (searchSpaceNode.symbol === container.symbol) {
2740+
result.push(getReferenceEntryFromNode(node));
2741+
}
2742+
break;
2743+
case SyntaxKind.ClassDeclaration:
2744+
// Make sure the container belongs to the same class
2745+
// and has the appropriate static modifier from the original container.
2746+
if (container.parent && searchSpaceNode.symbol === container.parent.symbol && (container.flags & NodeFlags.Static) === staticFlag) {
2747+
result.push(getReferenceEntryFromNode(node));
2748+
}
2749+
break;
2750+
case SyntaxKind.SourceFile:
2751+
if (container.kind === SyntaxKind.SourceFile && !isExternalModule(<SourceFile>container)) {
2752+
result.push(getReferenceEntryFromNode(node));
2753+
}
2754+
break;
27352755
}
27362756
});
27372757
}

0 commit comments

Comments
 (0)