@@ -5,12 +5,13 @@ namespace ts.FindAllReferences {
5
5
references : Entry [ ] ;
6
6
}
7
7
8
+ export const enum DefinitionKind { Symbol , Label , Keyword , This , String }
8
9
export type Definition =
9
- | { type : "symbol" ; symbol : Symbol }
10
- | { type : "label" ; node : Identifier }
11
- | { type : "keyword" ; node : Node }
12
- | { type : "this" ; node : Node }
13
- | { type : "string" ; node : StringLiteral } ;
10
+ | { readonly type : DefinitionKind . Symbol ; readonly symbol : Symbol }
11
+ | { readonly type : DefinitionKind . Label ; readonly node : Identifier }
12
+ | { readonly type : DefinitionKind . Keyword ; readonly node : Node }
13
+ | { readonly type : DefinitionKind . This ; readonly node : Node }
14
+ | { readonly type : DefinitionKind . String ; readonly node : StringLiteral } ;
14
15
15
16
export type Entry = NodeEntry | SpanEntry ;
16
17
export interface NodeEntry {
@@ -98,29 +99,29 @@ namespace ts.FindAllReferences {
98
99
function definitionToReferencedSymbolDefinitionInfo ( def : Definition , checker : TypeChecker , originalNode : Node ) : ReferencedSymbolDefinitionInfo {
99
100
const info = ( ( ) => {
100
101
switch ( def . type ) {
101
- case "symbol" : {
102
+ case DefinitionKind . Symbol : {
102
103
const { symbol } = def ;
103
104
const { displayParts, kind } = getDefinitionKindAndDisplayParts ( symbol , checker , originalNode ) ;
104
105
const name = displayParts . map ( p => p . text ) . join ( "" ) ;
105
106
return { node : symbol . declarations ? getNameOfDeclaration ( first ( symbol . declarations ) ) || first ( symbol . declarations ) : originalNode , name, kind, displayParts } ;
106
107
}
107
- case "label" : {
108
+ case DefinitionKind . Label : {
108
109
const { node } = def ;
109
110
return { node, name : node . text , kind : ScriptElementKind . label , displayParts : [ displayPart ( node . text , SymbolDisplayPartKind . text ) ] } ;
110
111
}
111
- case "keyword" : {
112
+ case DefinitionKind . Keyword : {
112
113
const { node } = def ;
113
114
const name = tokenToString ( node . kind ) ! ;
114
115
return { node, name, kind : ScriptElementKind . keyword , displayParts : [ { text : name , kind : ScriptElementKind . keyword } ] } ;
115
116
}
116
- case "this" : {
117
+ case DefinitionKind . This : {
117
118
const { node } = def ;
118
119
const symbol = checker . getSymbolAtLocation ( node ) ;
119
120
const displayParts = symbol && SymbolDisplay . getSymbolDisplayPartsDocumentationAndSymbolKind (
120
121
checker , symbol , node . getSourceFile ( ) , getContainerNode ( node ) , node ) . displayParts || [ textPart ( "this" ) ] ;
121
122
return { node, name : "this" , kind : ScriptElementKind . variableElement , displayParts } ;
122
123
}
123
- case "string" : {
124
+ case DefinitionKind . String : {
124
125
const { node } = def ;
125
126
return { node, name : node . text , kind : ScriptElementKind . variableElement , displayParts : [ displayPart ( getTextOfNode ( node ) , SymbolDisplayPartKind . stringLiteral ) ] } ;
126
127
}
@@ -374,7 +375,7 @@ namespace ts.FindAllReferences.Core {
374
375
}
375
376
}
376
377
377
- return references . length ? [ { definition : { type : "symbol" , symbol } , references } ] : emptyArray ;
378
+ return references . length ? [ { definition : { type : DefinitionKind . Symbol , symbol } , references } ] : emptyArray ;
378
379
}
379
380
380
381
/** getReferencedSymbols for special node kinds. */
@@ -585,7 +586,7 @@ namespace ts.FindAllReferences.Core {
585
586
let references = this . symbolIdToReferences [ symbolId ] ;
586
587
if ( ! references ) {
587
588
references = this . symbolIdToReferences [ symbolId ] = [ ] ;
588
- this . result . push ( { definition : { type : "symbol" , symbol : searchSymbol } , references } ) ;
589
+ this . result . push ( { definition : { type : DefinitionKind . Symbol , symbol : searchSymbol } , references } ) ;
589
590
}
590
591
return node => references . push ( nodeEntry ( node ) ) ;
591
592
}
@@ -879,7 +880,7 @@ namespace ts.FindAllReferences.Core {
879
880
const references = mapDefined ( getPossibleSymbolReferenceNodes ( sourceFile , labelName , container ) , node =>
880
881
// Only pick labels that are either the target label, or have a target that is the target label
881
882
node === targetLabel || ( isJumpStatementTarget ( node ) && getTargetLabel ( node , labelName ) === targetLabel ) ? nodeEntry ( node ) : undefined ) ;
882
- return [ { definition : { type : "label" , node : targetLabel } , references } ] ;
883
+ return [ { definition : { type : DefinitionKind . Label , node : targetLabel } , references } ] ;
883
884
}
884
885
885
886
function isValidReferencePosition ( node : Node , searchSymbolName : string ) : boolean {
@@ -911,7 +912,7 @@ namespace ts.FindAllReferences.Core {
911
912
return mapDefined ( getPossibleSymbolReferenceNodes ( sourceFile , tokenToString ( keywordKind ) ! , sourceFile ) , referenceLocation =>
912
913
referenceLocation . kind === keywordKind ? nodeEntry ( referenceLocation ) : undefined ) ;
913
914
} ) ;
914
- return references . length ? [ { definition : { type : "keyword" , node : references [ 0 ] . node } , references } ] : undefined ;
915
+ return references . length ? [ { definition : { type : DefinitionKind . Keyword , node : references [ 0 ] . node } , references } ] : undefined ;
915
916
}
916
917
917
918
function getReferencesInSourceFile ( sourceFile : SourceFile , search : Search , state : State , addReferencesHere = true ) : void {
@@ -1353,7 +1354,7 @@ namespace ts.FindAllReferences.Core {
1353
1354
return container && ( ModifierFlags . Static & getModifierFlags ( container ) ) === staticFlag && container . parent . symbol === searchSpaceNode . symbol ? nodeEntry ( node ) : undefined ;
1354
1355
} ) ;
1355
1356
1356
- return [ { definition : { type : "symbol" , symbol : searchSpaceNode . symbol } , references } ] ;
1357
+ return [ { definition : { type : DefinitionKind . Symbol , symbol : searchSpaceNode . symbol } , references } ] ;
1357
1358
}
1358
1359
1359
1360
function getReferencesForThisKeyword ( thisOrSuperKeyword : Node , sourceFiles : ReadonlyArray < SourceFile > , cancellationToken : CancellationToken ) : SymbolAndEntries [ ] | undefined {
@@ -1416,8 +1417,9 @@ namespace ts.FindAllReferences.Core {
1416
1417
} ) ;
1417
1418
} ) . map ( n => nodeEntry ( n ) ) ;
1418
1419
1420
+ const thisParameter = firstDefined ( references , r => isParameter ( r . node . parent ) ? r . node : undefined ) ;
1419
1421
return [ {
1420
- definition : { type : "this" , node : thisOrSuperKeyword } ,
1422
+ definition : { type : DefinitionKind . This , node : thisParameter || thisOrSuperKeyword } ,
1421
1423
references
1422
1424
} ] ;
1423
1425
}
@@ -1430,7 +1432,7 @@ namespace ts.FindAllReferences.Core {
1430
1432
} ) ;
1431
1433
1432
1434
return [ {
1433
- definition : { type : "string" , node } ,
1435
+ definition : { type : DefinitionKind . String , node } ,
1434
1436
references
1435
1437
} ] ;
1436
1438
}
0 commit comments