Add comment and clean up implementation on findAllReferences

This commit is contained in:
Yui T
2014-11-13 17:39:10 -08:00
parent a8ebdf0cbd
commit 8bd7aae869
3 changed files with 41 additions and 20 deletions

View File

@@ -94,7 +94,7 @@ module ts {
getReturnTypeOfSignature: getReturnTypeOfSignature,
getSymbolsInScope: getSymbolsInScope,
getSymbolInfo: getSymbolInfo,
getValueSymbolInfo: getValueSymbolInfo,
getShorthandAssignmentValueSymbol: getShorthandAssignmentValueSymbol,
getTypeOfNode: getTypeOfNode,
typeToString: typeToString,
getSymbolDisplayBuilder: getSymbolDisplayBuilder,
@@ -2407,7 +2407,7 @@ module ts {
}
// Return the symbol for the property with the given name in the given type. Creates synthetic union properties when
// necessary, maps primtive types and type parameters are to their apparent types, and augments with properties from
// necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from
// Object and Function as appropriate.
function getPropertyOfType(type: Type, name: string): Symbol {
if (type.flags & TypeFlags.Union) {
@@ -2442,7 +2442,7 @@ module ts {
}
// Return the signatures of the given kind in the given type. Creates synthetic union signatures when necessary and
// maps primtive types and type parameters are to their apparent types.
// maps primitive types and type parameters are to their apparent types.
function getSignaturesOfType(type: Type, kind: SignatureKind): Signature[] {
return getSignaturesOfObjectOrUnionType(getApparentType(type), kind);
}
@@ -2455,7 +2455,7 @@ module ts {
}
// Return the index type of the given kind in the given type. Creates synthetic union index types when necessary and
// maps primtive types and type parameters are to their apparent types.
// maps primitive types and type parameters are to their apparent types.
function getIndexTypeOfType(type: Type, kind: IndexKind): Type {
return getIndexTypeOfObjectOrUnionType(getApparentType(type), kind);
}
@@ -8644,9 +8644,14 @@ module ts {
return undefined;
}
function getValueSymbolInfo(node: Node): Symbol {
// Get symbol information as value
return resolveEntityName(node, <Identifier>node, SymbolFlags.Value);
function getShorthandAssignmentValueSymbol(location: Node): Symbol {
// The function returns a value symbol of an identifier in the short-hand property assignment.
// This is necessary as an identifier in short-hand property assignment can contains two meaning:
// property name and property value.
if (location && location.kind === SyntaxKind.ShorthandPropertyAssignment) {
return resolveEntityName(location, (<ShortHandPropertyDeclaration>location).name, SymbolFlags.Value);
}
return undefined;
}
function getTypeOfNode(node: Node): Type {

View File

@@ -715,7 +715,7 @@ module ts {
getReturnTypeOfSignature(signature: Signature): Type;
getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[];
getSymbolInfo(node: Node): Symbol;
getValueSymbolInfo(node: Node): Symbol;
getShorthandAssignmentValueSymbol(location: Node): Symbol;
getTypeOfNode(node: Node): Type;
typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string;
symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string;

View File

@@ -4058,15 +4058,20 @@ module ts {
}
var referenceSymbol = typeInfoResolver.getSymbolInfo(referenceLocation);
if (referenceSymbol && isRelatableToSearchSet(searchSymbols, referenceSymbol, referenceLocation)) {
result.push(getReferenceEntryFromNode(referenceLocation));
}
// TODO (yuisu): Comment
else if (referenceSymbol && referenceSymbol.declarations[0].kind === SyntaxKind.ShorthandPropertyAssignment) {
var referenceSymbolDeclName = referenceSymbol.declarations[0].name;
if (searchSymbols.indexOf(typeInfoResolver.getValueSymbolInfo(referenceSymbolDeclName)) >= 0 &&
!(<SymbolLinks>referenceSymbol).target) {
result.push(getReferenceEntryFromNode(referenceSymbolDeclName));
if (referenceSymbol) {
var referenceSymbolDeclaration = referenceSymbol.valueDeclaration;
var shorthandValueSymbol = typeInfoResolver.getShorthandAssignmentValueSymbol(referenceSymbolDeclaration);
if (isRelatableToSearchSet(searchSymbols, referenceSymbol, referenceLocation)) {
result.push(getReferenceEntryFromNode(referenceLocation));
}
/* Because in short-hand property assignment, an identifier which stored as name of the short-hand property assignment
* has two meaning : property name and property value. Therefore when we do findAllReference at the position where
* an identifier is declared, the language service should return the position of the variable declaration as well as
* the position in short-hand property assignment excluding property accessing. However, if we do findAllReference at the
* position of property accessing, the referenceEntry of such position will be handled in the first case.
*/
else if (!(referenceSymbol.flags & SymbolFlags.Transient) && searchSymbols.indexOf(shorthandValueSymbol) >= 0) {
result.push(getReferenceEntryFromNode(referenceSymbolDeclaration.name));
}
}
});
@@ -4236,9 +4241,20 @@ module ts {
result.push.apply(result, typeInfoResolver.getRootSymbols(contextualSymbol));
});
// Add the symbol in the case of short-hand property assignment
if (location.kind === SyntaxKind.Identifier && location.parent.kind === SyntaxKind.ShorthandPropertyAssignment) {
result.push(typeInfoResolver.getValueSymbolInfo(location));
/* Because in short-hand property assignment, location has two meaning : property name and as value of the property
* When we do findAllReference at the position of the short-hand property assignment, we would want to have references to position of
* property name and variable declaration of the identifier.
* Like in below example, when querying for all references for an identifier 'name', of the property assignment, the language service
* should show both 'name' in 'obj' and 'name' in variable declaration
* var name = "Foo";
* var obj = { name };
* In order to do that, we will populate the search set with the value symbol of the identifier as a value of the property assignment
* so that when matching with potential reference symbol, both symbols from property declaration and variable declaration
* will be included correctly.
*/
var shorthandValueSymbol = typeInfoResolver.getShorthandAssignmentValueSymbol(location.parent);
if (shorthandValueSymbol) {
result.push(shorthandValueSymbol);
}
}