Skip to content

Commit fefe2fb

Browse files
committed
Implement getScope
1 parent dbf9e47 commit fefe2fb

File tree

6 files changed

+137
-34
lines changed

6 files changed

+137
-34
lines changed

src/services/services.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,7 +2163,7 @@ module ts {
21632163
// Compute the meaning from the location and the symbol it references
21642164
var searchMeaning = getIntersectingMeaningFromDeclarations(getMeaningFromLocation(node), symbol.getDeclarations());
21652165

2166-
var scope = getSymbolScope(symbol, node);
2166+
var scope = getSymbolScope(symbol);
21672167

21682168
if (scope) {
21692169
result = [];
@@ -2184,9 +2184,42 @@ module ts {
21842184

21852185
return result;
21862186

2187-
function getSymbolScope(symbol: Symbol, node: Node): Node {
2188-
/// TODO: find the enclosing scope
2189-
return undefined;
2187+
function getSymbolScope(symbol: Symbol): Node {
2188+
// If this is private property or method, the scope is the containing class
2189+
if (symbol.getFlags() && (SymbolFlags.Property | SymbolFlags.Method)) {
2190+
var privateDeclaration = forEach(symbol.getDeclarations(), d => (d.flags & NodeFlags.Private) ? d : undefined);
2191+
if (privateDeclaration) {
2192+
return privateDeclaration.parent;
2193+
}
2194+
}
2195+
2196+
// if this symbol is visible from its parent container, e.g. exported, then bail out
2197+
if (symbol.parent) {
2198+
return undefined;
2199+
}
2200+
2201+
var scope: Node = undefined;
2202+
2203+
var declarations = symbol.getDeclarations();
2204+
for (var i = 0, n = declarations.length; i < n; i++) {
2205+
var container = getContainerNode(declarations[i]);
2206+
2207+
if (scope && scope !== container) {
2208+
// Diffrent declarations have diffrent containers, bail out
2209+
return undefined;
2210+
}
2211+
2212+
if (container.kind === SyntaxKind.SourceFile && !isExternalModule(<SourceFile>container)) {
2213+
// This is a global variable and not an external module, any declaration defined
2214+
// withen this scope is visible outside the file
2215+
return undefined;
2216+
}
2217+
2218+
// The search scope is the container node
2219+
scope = container;
2220+
}
2221+
2222+
return scope;
21902223
}
21912224

21922225
function getPossibleSymbolReferencePositions(sourceFile: SourceFile, symbolName: string, start: number, end: number): number[] {
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
/// <reference path='fourslash.ts'/>
2-
3-
// Global variable reference.
4-
5-
// @Filename: referencesForGlobals_1.ts
6-
////var /*1*/global = 2;
7-
////
8-
////class foo {
9-
//// constructor (public global) { }
10-
//// public f(global) { }
11-
//// public f2(global) { }
12-
////}
13-
////
14-
////class bar {
15-
//// constructor () {
16-
//// var n = global;
17-
////
18-
//// var f = new foo('');
19-
//// f.global = '';
20-
//// }
21-
////}
22-
////
23-
////var k = global;
24-
25-
// @Filename: referencesForGlobals_2.ts
26-
////var m = global;
27-
1+
/// <reference path='fourslash.ts'/>
2+
3+
// Global variable reference.
4+
5+
// @Filename: referencesForGlobals_1.ts
6+
////var /*1*/global = 2;
7+
////
8+
////class foo {
9+
//// constructor (public global) { }
10+
//// public f(global) { }
11+
//// public f2(global) { }
12+
////}
13+
////
14+
////class bar {
15+
//// constructor () {
16+
//// var n = global;
17+
////
18+
//// var f = new foo('');
19+
//// f.global = '';
20+
//// }
21+
////}
22+
////
23+
////var k = global;
24+
25+
// @Filename: referencesForGlobals_2.ts
26+
////var m = global;
27+
2828
// this line triggers a semantic/syntactic error check, remove line when 788570 is fixed
29-
edit.insert('');
30-
31-
goTo.marker("1");
29+
edit.insert('');
30+
31+
goTo.marker("1");
3232
verify.referencesCountIs(4);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// Global class reference.
4+
5+
// @Filename: referencesForGlobals_1.ts
6+
////class /*2*/globalClass {
7+
//// public f() { }
8+
////}
9+
10+
// @Filename: referencesForGlobals_2.ts
11+
////var c = /*1*/globalClass();
12+
13+
goTo.marker("1");
14+
verify.referencesCountIs(2);
15+
16+
goTo.marker("2");
17+
verify.referencesCountIs(2);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// Global interface reference.
4+
5+
// @Filename: referencesForGlobals_1.ts
6+
////interface /*2*/globalInterface {
7+
//// f();
8+
////}
9+
10+
// @Filename: referencesForGlobals_2.ts
11+
////var i: /*1*/globalInterface;
12+
13+
goTo.marker("1");
14+
verify.referencesCountIs(2);
15+
16+
goTo.marker("2");
17+
verify.referencesCountIs(2);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// Global module reference.
4+
5+
// @Filename: referencesForGlobals_1.ts
6+
////module /*2*/globalModule {
7+
//// export f() { };
8+
////}
9+
10+
// @Filename: referencesForGlobals_2.ts
11+
////var m = /*1*/globalModule;
12+
13+
goTo.marker("1");
14+
verify.referencesCountIs(2);
15+
16+
goTo.marker("2");
17+
verify.referencesCountIs(2);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// Global module reference.
4+
5+
// @Filename: referencesForGlobals_1.ts
6+
////module globalModule {
7+
//// export var x;
8+
////}
9+
////
10+
////import /*2*/globalAlias = globalModule;
11+
12+
// @Filename: referencesForGlobals_2.ts
13+
////var m = /*1*/globalAlias;
14+
15+
goTo.marker("1");
16+
verify.referencesCountIs(2);
17+
18+
goTo.marker("2");
19+
verify.referencesCountIs(2);

0 commit comments

Comments
 (0)