@@ -8,7 +8,7 @@ namespace ts.FindAllReferences {
8
8
}
9
9
10
10
export type Definition =
11
- | { type : "symbol" ; symbol : Symbol ; node : Node }
11
+ | { type : "symbol" ; symbol : Symbol }
12
12
| { type : "label" ; node : Identifier }
13
13
| { type : "keyword" ; node : ts . Node }
14
14
| { type : "this" ; node : ts . Node }
@@ -42,11 +42,12 @@ namespace ts.FindAllReferences {
42
42
}
43
43
44
44
export function findReferencedSymbols ( program : Program , cancellationToken : CancellationToken , sourceFiles : ReadonlyArray < SourceFile > , sourceFile : SourceFile , position : number ) : ReferencedSymbol [ ] | undefined {
45
- const referencedSymbols = findAllReferencedSymbols ( program , cancellationToken , sourceFiles , sourceFile , position ) ;
45
+ const node = getTouchingPropertyName ( sourceFile , position , /*includeJsDocComment*/ true ) ;
46
+ const referencedSymbols = Core . getReferencedSymbolsForNode ( position , node , program , sourceFiles , cancellationToken , /*options*/ { } ) ;
46
47
const checker = program . getTypeChecker ( ) ;
47
48
return ! referencedSymbols || ! referencedSymbols . length ? undefined : mapDefined < SymbolAndEntries , ReferencedSymbol > ( referencedSymbols , ( { definition, references } ) =>
48
49
// Only include referenced symbols that have a valid definition.
49
- definition && { definition : definitionToReferencedSymbolDefinitionInfo ( definition , checker ) , references : references . map ( toReferenceEntry ) } ) ;
50
+ definition && { definition : definitionToReferencedSymbolDefinitionInfo ( definition , checker , node ) , references : references . map ( toReferenceEntry ) } ) ;
50
51
}
51
52
52
53
export function getImplementationsAtPosition ( program : Program , cancellationToken : CancellationToken , sourceFiles : ReadonlyArray < SourceFile > , sourceFile : SourceFile , position : number ) : ImplementationLocation [ ] {
@@ -83,31 +84,26 @@ namespace ts.FindAllReferences {
83
84
}
84
85
85
86
export function findReferencedEntries ( program : Program , cancellationToken : CancellationToken , sourceFiles : ReadonlyArray < SourceFile > , sourceFile : SourceFile , position : number , options ?: Options ) : ReferenceEntry [ ] | undefined {
86
- const x = flattenEntries ( findAllReferencedSymbols ( program , cancellationToken , sourceFiles , sourceFile , position , options ) ) ;
87
- return map ( x , toReferenceEntry ) ;
87
+ const node = getTouchingPropertyName ( sourceFile , position , /*includeJsDocComment*/ true ) ;
88
+ return map ( flattenEntries ( Core . getReferencedSymbolsForNode ( position , node , program , sourceFiles , cancellationToken , options ) ) , toReferenceEntry ) ;
88
89
}
89
90
90
91
export function getReferenceEntriesForNode ( position : number , node : Node , program : Program , sourceFiles : ReadonlyArray < SourceFile > , cancellationToken : CancellationToken , options : Options = { } ) : Entry [ ] | undefined {
91
92
return flattenEntries ( Core . getReferencedSymbolsForNode ( position , node , program , sourceFiles , cancellationToken , options ) ) ;
92
93
}
93
94
94
- function findAllReferencedSymbols ( program : Program , cancellationToken : CancellationToken , sourceFiles : ReadonlyArray < SourceFile > , sourceFile : SourceFile , position : number , options ?: Options ) : SymbolAndEntries [ ] | undefined {
95
- const node = getTouchingPropertyName ( sourceFile , position , /*includeJsDocComment*/ true ) ;
96
- return Core . getReferencedSymbolsForNode ( position , node , program , sourceFiles , cancellationToken , options ) ;
97
- }
98
-
99
95
function flattenEntries ( referenceSymbols : SymbolAndEntries [ ] ) : Entry [ ] {
100
96
return referenceSymbols && flatMap ( referenceSymbols , r => r . references ) ;
101
97
}
102
98
103
- function definitionToReferencedSymbolDefinitionInfo ( def : Definition , checker : TypeChecker ) : ReferencedSymbolDefinitionInfo | undefined {
99
+ function definitionToReferencedSymbolDefinitionInfo ( def : Definition , checker : TypeChecker , originalNode : Node ) : ReferencedSymbolDefinitionInfo | undefined {
104
100
const info = ( ( ) => {
105
101
switch ( def . type ) {
106
102
case "symbol" : {
107
- const { symbol, node } = def ;
108
- const { displayParts, kind } = getDefinitionKindAndDisplayParts ( symbol , node , checker ) ;
103
+ const { symbol } = def ;
104
+ const { displayParts, kind } = getDefinitionKindAndDisplayParts ( symbol , checker , originalNode ) ;
109
105
const name = displayParts . map ( p => p . text ) . join ( "" ) ;
110
- return { node, name, kind, displayParts } ;
106
+ return { node : symbol . declarations ? getNameOfDeclaration ( first ( symbol . declarations ) ) || first ( symbol . declarations ) : originalNode , name, kind, displayParts } ;
111
107
}
112
108
case "label" : {
113
109
const { node } = def ;
@@ -129,13 +125,11 @@ namespace ts.FindAllReferences {
129
125
const { node } = def ;
130
126
return { node, name : node . text , kind : ScriptElementKind . variableElement , displayParts : [ displayPart ( getTextOfNode ( node ) , SymbolDisplayPartKind . stringLiteral ) ] } ;
131
127
}
128
+ default :
129
+ return Debug . assertNever ( def ) ;
132
130
}
133
131
} ) ( ) ;
134
132
135
- if ( ! info ) {
136
- return undefined ;
137
- }
138
-
139
133
const { node, name, kind, displayParts } = info ;
140
134
const sourceFile = node . getSourceFile ( ) ;
141
135
return {
@@ -149,9 +143,11 @@ namespace ts.FindAllReferences {
149
143
} ;
150
144
}
151
145
152
- function getDefinitionKindAndDisplayParts ( symbol : Symbol , node : Node , checker : TypeChecker ) : { displayParts : SymbolDisplayPart [ ] , kind : ScriptElementKind } {
146
+ function getDefinitionKindAndDisplayParts ( symbol : Symbol , checker : TypeChecker , node : Node ) : { displayParts : SymbolDisplayPart [ ] , kind : ScriptElementKind } {
147
+ const meaning = Core . getIntersectingMeaningFromDeclarations ( node , symbol ) ;
148
+ const enclosingDeclaration = firstOrUndefined ( symbol . declarations ) || node ;
153
149
const { displayParts, symbolKind } =
154
- SymbolDisplay . getSymbolDisplayPartsDocumentationAndSymbolKind ( checker , symbol , node . getSourceFile ( ) , getContainerNode ( node ) , node ) ;
150
+ SymbolDisplay . getSymbolDisplayPartsDocumentationAndSymbolKind ( checker , symbol , enclosingDeclaration . getSourceFile ( ) , enclosingDeclaration , enclosingDeclaration , meaning ) ;
155
151
return { displayParts, kind : symbolKind } ;
156
152
}
157
153
@@ -186,7 +182,7 @@ namespace ts.FindAllReferences {
186
182
function implementationKindDisplayParts ( node : ts . Node , checker : ts . TypeChecker ) : { kind : ScriptElementKind , displayParts : SymbolDisplayPart [ ] } {
187
183
const symbol = checker . getSymbolAtLocation ( isDeclaration ( node ) && node . name ? node . name : node ) ;
188
184
if ( symbol ) {
189
- return getDefinitionKindAndDisplayParts ( symbol , node , checker ) ;
185
+ return getDefinitionKindAndDisplayParts ( symbol , checker , node ) ;
190
186
}
191
187
else if ( node . kind === SyntaxKind . ObjectLiteralExpression ) {
192
188
return {
@@ -317,10 +313,7 @@ namespace ts.FindAllReferences.Core {
317
313
}
318
314
}
319
315
320
- return [ {
321
- definition : { type : "symbol" , symbol, node : symbol . valueDeclaration } ,
322
- references
323
- } ] ;
316
+ return [ { definition : { type : "symbol" , symbol } , references } ] ;
324
317
}
325
318
326
319
/** getReferencedSymbols for special node kinds. */
@@ -357,13 +350,13 @@ namespace ts.FindAllReferences.Core {
357
350
symbol = skipPastExportOrImportSpecifierOrUnion ( symbol , node , checker ) || symbol ;
358
351
359
352
// Compute the meaning from the location and the symbol it references
360
- const searchMeaning = getIntersectingMeaningFromDeclarations ( getMeaningFromLocation ( node ) , symbol . declarations ) ;
353
+ const searchMeaning = getIntersectingMeaningFromDeclarations ( node , symbol ) ;
361
354
362
355
const result : SymbolAndEntries [ ] = [ ] ;
363
356
const state = new State ( sourceFiles , getSpecialSearchKind ( node ) , checker , cancellationToken , searchMeaning , options , result ) ;
364
357
365
358
if ( node . kind === SyntaxKind . DefaultKeyword ) {
366
- addReference ( node , symbol , node , state ) ;
359
+ addReference ( node , symbol , state ) ;
367
360
searchForImportsOfExport ( node , symbol , { exportingModuleSymbol : Debug . assertDefined ( symbol . parent , "Expected export symbol to have a parent" ) , exportKind : ExportKind . Default } , state ) ;
368
361
}
369
362
else {
@@ -434,7 +427,6 @@ namespace ts.FindAllReferences.Core {
434
427
/** If coming from an export, we will not recursively search for the imported symbol (since that's where we came from). */
435
428
readonly comingFrom ?: ImportExport ;
436
429
437
- readonly location : Node ;
438
430
readonly symbol : Symbol ;
439
431
readonly text : string ;
440
432
readonly escapedText : __String ;
@@ -514,7 +506,7 @@ namespace ts.FindAllReferences.Core {
514
506
const escapedText = escapeLeadingUnderscores ( text ) ;
515
507
const parents = this . options . implementations && getParentSymbolsOfPropertyAccess ( location , symbol , this . checker ) ;
516
508
return {
517
- location , symbol, comingFrom, text, escapedText, parents,
509
+ symbol, comingFrom, text, escapedText, parents,
518
510
includes : referenceSymbol => allSearchSymbols ? contains ( allSearchSymbols , referenceSymbol ) : referenceSymbol === symbol ,
519
511
} ;
520
512
}
@@ -524,12 +516,12 @@ namespace ts.FindAllReferences.Core {
524
516
* Callback to add references for a particular searched symbol.
525
517
* This initializes a reference group, so only call this if you will add at least one reference.
526
518
*/
527
- referenceAdder ( searchSymbol : Symbol , searchLocation : Node ) : ( node : Node ) => void {
519
+ referenceAdder ( searchSymbol : Symbol ) : ( node : Node ) => void {
528
520
const symbolId = getSymbolId ( searchSymbol ) ;
529
521
let references = this . symbolIdToReferences [ symbolId ] ;
530
522
if ( ! references ) {
531
523
references = this . symbolIdToReferences [ symbolId ] = [ ] ;
532
- this . result . push ( { definition : { type : "symbol" , symbol : searchSymbol , node : searchLocation } , references } ) ;
524
+ this . result . push ( { definition : { type : "symbol" , symbol : searchSymbol } , references } ) ;
533
525
}
534
526
return node => references . push ( nodeEntry ( node ) ) ;
535
527
}
@@ -559,7 +551,7 @@ namespace ts.FindAllReferences.Core {
559
551
560
552
// For `import { foo as bar }` just add the reference to `foo`, and don't otherwise search in the file.
561
553
if ( singleReferences . length ) {
562
- const addRef = state . referenceAdder ( exportSymbol , exportLocation ) ;
554
+ const addRef = state . referenceAdder ( exportSymbol ) ;
563
555
for ( const singleRef of singleReferences ) {
564
556
addRef ( singleRef ) ;
565
557
}
@@ -862,7 +854,7 @@ namespace ts.FindAllReferences.Core {
862
854
863
855
switch ( state . specialSearchKind ) {
864
856
case SpecialSearchKind . None :
865
- addReference ( referenceLocation , relatedSymbol , search . location , state ) ;
857
+ addReference ( referenceLocation , relatedSymbol , state ) ;
866
858
break ;
867
859
case SpecialSearchKind . Constructor :
868
860
addConstructorReferences ( referenceLocation , sourceFile , search , state ) ;
@@ -896,7 +888,7 @@ namespace ts.FindAllReferences.Core {
896
888
}
897
889
898
890
if ( ! state . options . isForRename && state . markSeenReExportRHS ( name ) ) {
899
- addReference ( name , referenceSymbol , name , state ) ;
891
+ addReference ( name , referenceSymbol , state ) ;
900
892
}
901
893
}
902
894
else {
@@ -920,7 +912,7 @@ namespace ts.FindAllReferences.Core {
920
912
}
921
913
922
914
function addRef ( ) {
923
- addReference ( referenceLocation , localSymbol , search . location , state ) ;
915
+ addReference ( referenceLocation , localSymbol , state ) ;
924
916
}
925
917
}
926
918
@@ -969,12 +961,12 @@ namespace ts.FindAllReferences.Core {
969
961
* position of property accessing, the referenceEntry of such position will be handled in the first case.
970
962
*/
971
963
if ( ! ( flags & SymbolFlags . Transient ) && search . includes ( shorthandValueSymbol ) ) {
972
- addReference ( getNameOfDeclaration ( valueDeclaration ) , shorthandValueSymbol , search . location , state ) ;
964
+ addReference ( getNameOfDeclaration ( valueDeclaration ) , shorthandValueSymbol , state ) ;
973
965
}
974
966
}
975
967
976
- function addReference ( referenceLocation : Node , relatedSymbol : Symbol , searchLocation : Node , state : State ) : void {
977
- const addRef = state . referenceAdder ( relatedSymbol , searchLocation ) ;
968
+ function addReference ( referenceLocation : Node , relatedSymbol : Symbol , state : State ) : void {
969
+ const addRef = state . referenceAdder ( relatedSymbol ) ;
978
970
if ( state . options . implementations ) {
979
971
addImplementationReferences ( referenceLocation , addRef , state ) ;
980
972
}
@@ -986,10 +978,10 @@ namespace ts.FindAllReferences.Core {
986
978
/** Adds references when a constructor is used with `new this()` in its own class and `super()` calls in subclasses. */
987
979
function addConstructorReferences ( referenceLocation : Node , sourceFile : SourceFile , search : Search , state : State ) : void {
988
980
if ( isNewExpressionTarget ( referenceLocation ) ) {
989
- addReference ( referenceLocation , search . symbol , search . location , state ) ;
981
+ addReference ( referenceLocation , search . symbol , state ) ;
990
982
}
991
983
992
- const pusher = ( ) => state . referenceAdder ( search . symbol , search . location ) ;
984
+ const pusher = ( ) => state . referenceAdder ( search . symbol ) ;
993
985
994
986
if ( isClassLike ( referenceLocation . parent ) ) {
995
987
Debug . assert ( referenceLocation . kind === SyntaxKind . DefaultKeyword || referenceLocation . parent . name === referenceLocation ) ;
@@ -1006,11 +998,11 @@ namespace ts.FindAllReferences.Core {
1006
998
}
1007
999
1008
1000
function addClassStaticThisReferences ( referenceLocation : Node , search : Search , state : State ) : void {
1009
- addReference ( referenceLocation , search . symbol , search . location , state ) ;
1001
+ addReference ( referenceLocation , search . symbol , state ) ;
1010
1002
if ( isClassLike ( referenceLocation . parent ) ) {
1011
1003
Debug . assert ( referenceLocation . parent . name === referenceLocation ) ;
1012
1004
// This is the class declaration.
1013
- addStaticThisReferences ( referenceLocation . parent , state . referenceAdder ( search . symbol , search . location ) ) ;
1005
+ addStaticThisReferences ( referenceLocation . parent , state . referenceAdder ( search . symbol ) ) ;
1014
1006
}
1015
1007
}
1016
1008
@@ -1300,7 +1292,7 @@ namespace ts.FindAllReferences.Core {
1300
1292
return container && ( ModifierFlags . Static & getModifierFlags ( container ) ) === staticFlag && container . parent . symbol === searchSpaceNode . symbol ? nodeEntry ( node ) : undefined ;
1301
1293
} ) ;
1302
1294
1303
- return [ { definition : { type : "symbol" , symbol : searchSpaceNode . symbol , node : superKeyword } , references } ] ;
1295
+ return [ { definition : { type : "symbol" , symbol : searchSpaceNode . symbol } , references } ] ;
1304
1296
}
1305
1297
1306
1298
function getReferencesForThisKeyword ( thisOrSuperKeyword : Node , sourceFiles : ReadonlyArray < SourceFile > , cancellationToken : CancellationToken ) : SymbolAndEntries [ ] {
@@ -1645,7 +1637,9 @@ namespace ts.FindAllReferences.Core {
1645
1637
* module, we want to keep the search limited to only types, as the two declarations (interface and uninstantiated module)
1646
1638
* do not intersect in any of the three spaces.
1647
1639
*/
1648
- function getIntersectingMeaningFromDeclarations ( meaning : SemanticMeaning , declarations : Declaration [ ] ) : SemanticMeaning {
1640
+ export function getIntersectingMeaningFromDeclarations ( node : Node , symbol : Symbol ) : SemanticMeaning {
1641
+ let meaning = getMeaningFromLocation ( node ) ;
1642
+ const { declarations } = symbol ;
1649
1643
if ( declarations ) {
1650
1644
let lastIterationMeaning : SemanticMeaning ;
1651
1645
do {
0 commit comments