@@ -4265,7 +4265,7 @@ namespace ts {
4265
4265
isLiteralNameOfPropertyDeclarationOrIndexAccess ( node ) ||
4266
4266
isNameOfExternalModuleImportOrDeclaration ( node ) ) {
4267
4267
4268
- let referencedSymbols = getReferencedSymbolsForNodes ( node , sourceFilesToSearch , /*findInStrings:*/ false , /*findInComments:*/ false ) ;
4268
+ let referencedSymbols = getReferencedSymbolsForNode ( node , sourceFilesToSearch , /*findInStrings:*/ false , /*findInComments:*/ false ) ;
4269
4269
return convertReferencedSymbols ( referencedSymbols ) ;
4270
4270
}
4271
4271
@@ -4917,10 +4917,10 @@ namespace ts {
4917
4917
}
4918
4918
4919
4919
Debug . assert ( node . kind === SyntaxKind . Identifier || node . kind === SyntaxKind . NumericLiteral || node . kind === SyntaxKind . StringLiteral ) ;
4920
- return getReferencedSymbolsForNodes ( node , program . getSourceFiles ( ) , findInStrings , findInComments ) ;
4920
+ return getReferencedSymbolsForNode ( node , program . getSourceFiles ( ) , findInStrings , findInComments ) ;
4921
4921
}
4922
4922
4923
- function getReferencedSymbolsForNodes ( node : Node , sourceFiles : SourceFile [ ] , findInStrings : boolean , findInComments : boolean ) : ReferencedSymbol [ ] {
4923
+ function getReferencedSymbolsForNode ( node : Node , sourceFiles : SourceFile [ ] , findInStrings : boolean , findInComments : boolean ) : ReferencedSymbol [ ] {
4924
4924
let typeChecker = program . getTypeChecker ( ) ;
4925
4925
4926
4926
// Labels
@@ -4955,7 +4955,7 @@ namespace ts {
4955
4955
4956
4956
let declarations = symbol . declarations ;
4957
4957
4958
- // The symbol was an internal symbol and does not have a declaration e.g.undefined symbol
4958
+ // The symbol was an internal symbol and does not have a declaration e.g. undefined symbol
4959
4959
if ( ! declarations || ! declarations . length ) {
4960
4960
return undefined ;
4961
4961
}
@@ -4965,8 +4965,9 @@ namespace ts {
4965
4965
// Compute the meaning from the location and the symbol it references
4966
4966
let searchMeaning = getIntersectingMeaningFromDeclarations ( getMeaningFromLocation ( node ) , declarations ) ;
4967
4967
4968
- // Get the text to search for, we need to normalize it as external module names will have quote
4969
- let declaredName = getDeclaredName ( symbol , node ) ;
4968
+ // Get the text to search for.
4969
+ // Note: if this is an external module symbol, the name doesn't include quotes.
4970
+ let declaredName = getDeclaredName ( typeChecker , symbol , node ) ;
4970
4971
4971
4972
// Try to get the smallest valid scope that we can limit our search to;
4972
4973
// otherwise we'll need to search globally (i.e. include each file).
@@ -5013,76 +5014,43 @@ namespace ts {
5013
5014
} ;
5014
5015
}
5015
5016
5016
- function isImportOrExportSpecifierName ( location : Node ) : boolean {
5017
- return location . parent &&
5018
- ( location . parent . kind === SyntaxKind . ImportSpecifier || location . parent . kind === SyntaxKind . ExportSpecifier ) &&
5019
- ( < ImportOrExportSpecifier > location . parent ) . propertyName === location ;
5020
- }
5021
-
5022
5017
function isImportOrExportSpecifierImportSymbol ( symbol : Symbol ) {
5023
5018
return ( symbol . flags & SymbolFlags . Alias ) && forEach ( symbol . declarations , declaration => {
5024
5019
return declaration . kind === SyntaxKind . ImportSpecifier || declaration . kind === SyntaxKind . ExportSpecifier ;
5025
5020
} ) ;
5026
5021
}
5027
5022
5028
- function getDeclaredName ( symbol : Symbol , location : Node ) {
5029
- // Special case for function expressions, whose names are solely local to their bodies.
5030
- let functionExpression = forEach ( symbol . declarations , d => d . kind === SyntaxKind . FunctionExpression ? < FunctionExpression > d : undefined ) ;
5031
-
5032
- // When a name gets interned into a SourceFile's 'identifiers' Map,
5033
- // its name is escaped and stored in the same way its symbol name/identifier
5034
- // name should be stored. Function expressions, however, are a special case,
5035
- // because despite sometimes having a name, the binder unconditionally binds them
5036
- // to a symbol with the name "__function".
5037
- let name : string ;
5038
- if ( functionExpression && functionExpression . name ) {
5039
- name = functionExpression . name . text ;
5040
- }
5041
-
5042
- // If this is an export or import specifier it could have been renamed using the as syntax.
5043
- // if so we want to search for whatever under the cursor, the symbol is pointing to the alias (name)
5044
- // so check for the propertyName.
5045
- if ( isImportOrExportSpecifierName ( location ) ) {
5046
- return location . getText ( ) ;
5047
- }
5048
-
5049
- name = typeChecker . symbolToString ( symbol ) ;
5050
-
5051
- return stripQuotes ( name ) ;
5052
- }
5053
-
5054
5023
function getInternedName ( symbol : Symbol , location : Node , declarations : Declaration [ ] ) : string {
5055
- // If this is an export or import specifier it could have been renamed using the as syntax.
5056
- // if so we want to search for whatever under the cursor, the symbol is pointing to the alias (name)
5057
- // so check for the propertyName.
5024
+ // If this is an export or import specifier it could have been renamed using the 'as' syntax.
5025
+ // If so we want to search for whatever under the cursor.
5058
5026
if ( isImportOrExportSpecifierName ( location ) ) {
5059
5027
return location . getText ( ) ;
5060
5028
}
5061
5029
5062
- // Special case for function expressions, whose names are solely local to their bodies.
5063
- let functionExpression = forEach ( declarations , d => d . kind === SyntaxKind . FunctionExpression ? < FunctionExpression > d : undefined ) ;
5064
-
5065
- // When a name gets interned into a SourceFile's 'identifiers' Map,
5066
- // its name is escaped and stored in the same way its symbol name/identifier
5067
- // name should be stored. Function expressions, however, are a special case,
5068
- // because despite sometimes having a name, the binder unconditionally binds them
5069
- // to a symbol with the name "__function".
5070
- let name = functionExpression && functionExpression . name
5071
- ? functionExpression . name . text
5072
- : symbol . name ;
5073
-
5074
- return stripQuotes ( name ) ;
5075
- }
5030
+ // Try to get the local symbol if we're dealing with an 'export default'
5031
+ // since that symbol has the "true" name.
5032
+ let localExportDefaultSymbol = getLocalSymbolForExportDefault ( symbol ) ;
5033
+ symbol = localExportDefaultSymbol || symbol ;
5076
5034
5077
- function stripQuotes ( name : string ) {
5078
- let length = name . length ;
5079
- if ( length >= 2 && name . charCodeAt ( 0 ) === CharacterCodes . doubleQuote && name . charCodeAt ( length - 1 ) === CharacterCodes . doubleQuote ) {
5080
- return name . substring ( 1 , length - 1 ) ;
5081
- } ;
5082
- return name ;
5035
+ return stripQuotes ( symbol . name ) ;
5083
5036
}
5084
5037
5038
+ /**
5039
+ * Determines the smallest scope in which a symbol may have named references.
5040
+ * Note that not every construct has been accounted for. This function can
5041
+ * probably be improved.
5042
+ *
5043
+ * @returns undefined if the scope cannot be determined, implying that
5044
+ * a reference to a symbol can occur anywhere.
5045
+ */
5085
5046
function getSymbolScope ( symbol : Symbol ) : Node {
5047
+ // If this is the symbol of a function expression, then named references
5048
+ // are limited to its own scope.
5049
+ let valueDeclaration = symbol . valueDeclaration ;
5050
+ if ( valueDeclaration && valueDeclaration . kind === SyntaxKind . FunctionExpression ) {
5051
+ return valueDeclaration ;
5052
+ }
5053
+
5086
5054
// If this is private property or method, the scope is the containing class
5087
5055
if ( symbol . flags & ( SymbolFlags . Property | SymbolFlags . Method ) ) {
5088
5056
let privateDeclaration = forEach ( symbol . getDeclarations ( ) , d => ( d . flags & NodeFlags . Private ) ? d : undefined ) ;
@@ -6724,12 +6692,13 @@ namespace ts {
6724
6692
}
6725
6693
}
6726
6694
6695
+ let displayName = getDeclaredName ( typeChecker , symbol , node ) ;
6727
6696
let kind = getSymbolKind ( symbol , node ) ;
6728
6697
if ( kind ) {
6729
6698
return {
6730
6699
canRename : true ,
6731
6700
localizedErrorMessage : undefined ,
6732
- displayName : symbol . name ,
6701
+ displayName,
6733
6702
fullDisplayName : typeChecker . getFullyQualifiedName ( symbol ) ,
6734
6703
kind : kind ,
6735
6704
kindModifiers : getSymbolModifiers ( symbol ) ,
0 commit comments