forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindReferenceHelpers.ts
More file actions
152 lines (130 loc) · 7.59 KB
/
Copy pathfindReferenceHelpers.ts
File metadata and controls
152 lines (130 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) Microsoft. All rights reserved. Licensed under the Apache License, Version 2.0.
// See LICENSE.txt in the project root for complete license information.
///<reference path='references.ts' />
module TypeScript.Services {
export class FindReferenceHelpers {
public static compareSymbolsForLexicalIdentity(firstSymbol: TypeScript.PullSymbol, secondSymbol: TypeScript.PullSymbol): boolean {
// Unwrap modules so that we're always referring to the variable.
if (!firstSymbol.isAlias() && firstSymbol.isContainer()) {
var containerForFirstSymbol = (<TypeScript.PullContainerSymbol>firstSymbol);
if (containerForFirstSymbol.getInstanceSymbol()) {
firstSymbol = containerForFirstSymbol.getInstanceSymbol();
}
}
if (!secondSymbol.isAlias() && secondSymbol.isContainer()) {
var containerForSecondSymbol = (<TypeScript.PullContainerSymbol>secondSymbol);
if (containerForSecondSymbol.getInstanceSymbol()) {
secondSymbol = containerForSecondSymbol.getInstanceSymbol();
}
}
if (firstSymbol.kind === secondSymbol.kind) {
if (firstSymbol === secondSymbol) {
return true;
}
// If we have two variables and they have the same name and the same parent, then
// they are the same symbol.
if (firstSymbol.kind === TypeScript.PullElementKind.Variable &&
firstSymbol.name === secondSymbol.name &&
firstSymbol.getDeclarations() && firstSymbol.getDeclarations().length >= 1 &&
secondSymbol.getDeclarations() && secondSymbol.getDeclarations().length >= 1) {
var firstSymbolDecl = firstSymbol.getDeclarations()[0];
var secondSymbolDecl = secondSymbol.getDeclarations()[0];
return firstSymbolDecl.getParentDecl() === secondSymbolDecl.getParentDecl();
}
// If we have two properties that belong to an object literal, then we need ot see
// if they came from teh same object literal ast.
if (firstSymbol.kind === TypeScript.PullElementKind.Property &&
firstSymbol.name === secondSymbol.name &&
firstSymbol.getDeclarations() && firstSymbol.getDeclarations().length >= 1 &&
secondSymbol.getDeclarations() && secondSymbol.getDeclarations().length >= 1) {
var firstSymbolDecl = firstSymbol.getDeclarations()[0];
var secondSymbolDecl = secondSymbol.getDeclarations()[0];
var firstParentDecl = firstSymbolDecl.getParentDecl();
var secondParentDecl = secondSymbolDecl.getParentDecl();
if (firstParentDecl.kind === TypeScript.PullElementKind.ObjectLiteral &&
secondParentDecl.kind === TypeScript.PullElementKind.ObjectLiteral) {
return firstParentDecl.ast() === secondParentDecl.ast();
}
}
// check if we are dealing with the implementation of interface method or a method override
if (firstSymbol.name === secondSymbol.name) {
// at this point firstSymbol.kind === secondSymbol.kind so we can pick any of those
switch (firstSymbol.kind) {
case PullElementKind.Property:
case PullElementKind.Method:
case PullElementKind.GetAccessor:
case PullElementKind.SetAccessor:
// these kinds can only be defined in types
var t1 = <PullTypeSymbol>firstSymbol.getContainer();
var t2 = <PullTypeSymbol>secondSymbol.getContainer();
t1._resolveDeclaredSymbol();
t2._resolveDeclaredSymbol();
return t1.hasBase(t2) || t2.hasBase(t1);
break;
}
}
return false;
}
else {
switch (firstSymbol.kind) {
case TypeScript.PullElementKind.Class: {
return this.checkSymbolsForDeclarationEquality(firstSymbol, secondSymbol);
}
case TypeScript.PullElementKind.Property: {
if (firstSymbol.isAccessor()) {
var getterSymbol = (<TypeScript.PullAccessorSymbol>firstSymbol).getGetter();
var setterSymbol = (<TypeScript.PullAccessorSymbol>firstSymbol).getSetter();
if (getterSymbol && getterSymbol === secondSymbol) {
return true;
}
if (setterSymbol && setterSymbol === secondSymbol) {
return true;
}
}
return false;
}
case TypeScript.PullElementKind.Function: {
if (secondSymbol.isAccessor()) {
var getterSymbol = (<TypeScript.PullAccessorSymbol>secondSymbol).getGetter();
var setterSymbol = (<TypeScript.PullAccessorSymbol>secondSymbol).getSetter();
if (getterSymbol && getterSymbol === firstSymbol) {
return true;
}
if (setterSymbol && setterSymbol === firstSymbol) {
return true;
}
}
return false;
}
case TypeScript.PullElementKind.ConstructorMethod: {
return this.checkSymbolsForDeclarationEquality(firstSymbol, secondSymbol);
}
}
}
return firstSymbol === secondSymbol;
}
private static checkSymbolsForDeclarationEquality(firstSymbol: TypeScript.PullSymbol, secondSymbol: TypeScript.PullSymbol): boolean {
var firstSymbolDeclarations: TypeScript.PullDecl[] = firstSymbol.getDeclarations();
var secondSymbolDeclarations: TypeScript.PullDecl[] = secondSymbol.getDeclarations();
for (var i = 0, iLen = firstSymbolDeclarations.length; i < iLen; i++) {
for (var j = 0, jLen = secondSymbolDeclarations.length; j < jLen; j++) {
if (this.declarationsAreSameOrParents(firstSymbolDeclarations[i], secondSymbolDeclarations[j])) {
return true;
}
}
}
return false;
}
private static declarationsAreSameOrParents(firstDecl: TypeScript.PullDecl, secondDecl: TypeScript.PullDecl): boolean {
var firstParent: TypeScript.PullDecl = firstDecl.getParentDecl();
var secondParent: TypeScript.PullDecl = secondDecl.getParentDecl();
if (firstDecl === secondDecl ||
firstDecl === secondParent ||
firstParent === secondDecl ||
firstParent === secondParent) {
return true;
}
return false;
}
}
}