From 8bd7aae869d222470e724f86cce690a185711273 Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 13 Nov 2014 17:39:10 -0800 Subject: [PATCH] Add comment and clean up implementation on findAllReferences --- src/compiler/checker.ts | 19 ++++++++++++------- src/compiler/types.ts | 2 +- src/services/services.ts | 40 ++++++++++++++++++++++++++++------------ 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0bf17edbf19..6ca1df3dc85 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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, 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, (location).name, SymbolFlags.Value); + } + return undefined; } function getTypeOfNode(node: Node): Type { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f2690a82462..a4d3fc36689 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -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; diff --git a/src/services/services.ts b/src/services/services.ts index 822795a1c15..21cc23bcee5 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -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 && - !(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); } }