add support for getReferences on property string index access

This commit is contained in:
Mohamed Hegazy 2014-08-13 13:58:42 -07:00
parent bfd13347d4
commit fa1033ab7a
4 changed files with 63 additions and 4 deletions

View file

@ -2219,6 +2219,22 @@ module ts {
return result;
}
function isValidReferencePosition(node: Node, searchSymbol: Symbol): boolean {
if (node) {
var searchSymbolName = searchSymbol.getName();
// Compare the length so we filter out strict superstrings of the symbol we are looking for
if (node.kind === SyntaxKind.Identifier && node.getWidth() === searchSymbolName.length) {
return true;
}
else if (isIndexOfStringIndexAccess(node) && node.getWidth() === searchSymbolName.length + 2) {
return true;
}
}
return false;
}
function getReferencesInNode(container: Node, searchSymbol: Symbol, result: ReferenceEntry[]): void {
var searchSymbolName = searchSymbol.getName();
@ -2233,10 +2249,7 @@ module ts {
// Each position we're searching for should be at the start of an identifier.
var node = getNodeAtPosition(sourceFile, position);
///TODO: handle string properties
// Compare the length so we filter out strict superstrings of the symbol we are looking for
if (!node || node.kind !== SyntaxKind.Identifier || node.getWidth() !== searchSymbolName.length) {
if (!isValidReferencePosition(node, searchSymbol)) {
return;
}

View file

@ -0,0 +1,18 @@
/// <reference path='fourslash.ts'/>
// References a class property using string index access
////class Foo {
//// property: number;
//// method(): void { }
////}
////
////var f: Foo;
////f[/*1*/"property"];
////f[/*2*/"method"];
goTo.marker("1");
verify.referencesCountIs(2);
goTo.marker("2");
verify.referencesCountIs(2);

View file

@ -0,0 +1,9 @@
/// <reference path='fourslash.ts'/>
// References to a unknown index property
////var a;
////a[/*1*/"blah"];
goTo.marker("1");
verify.referencesCountIs(1);

View file

@ -0,0 +1,19 @@
/// <reference path='fourslash.ts'/>
// References to a property of the apparent type using string indexer
////interface Object {
//// toMyString();
////}
////
////var y: Object;
////y./*1*/toMyString();
////
////var x = {};
////x[/*2*/"toMyString"]();
goTo.marker("1");
verify.referencesCountIs(3);
goTo.marker("2");
verify.referencesCountIs(3);