@@ -743,18 +743,13 @@ namespace ts.FindAllReferences.Core {
743
743
}
744
744
745
745
function getLabelReferencesInNode ( container : Node , targetLabel : Identifier ) : SymbolAndEntries [ ] {
746
- const references : Entry [ ] = [ ] ;
747
746
const sourceFile = container . getSourceFile ( ) ;
748
747
const labelName = targetLabel . text ;
749
- const possiblePositions = getPossibleSymbolReferencePositions ( sourceFile , labelName , container ) ;
750
- for ( const position of possiblePositions ) {
748
+ const references = mapDefined ( getPossibleSymbolReferencePositions ( sourceFile , labelName , container ) , position => {
751
749
const node = getTouchingWord ( sourceFile , position , /*includeJsDocComment*/ false ) ;
752
750
// Only pick labels that are either the target label, or have a target that is the target label
753
- if ( node && ( node === targetLabel || ( isJumpStatementTarget ( node ) && getTargetLabel ( node , labelName ) === targetLabel ) ) ) {
754
- references . push ( nodeEntry ( node ) ) ;
755
- }
756
- }
757
-
751
+ return node && ( node === targetLabel || ( isJumpStatementTarget ( node ) && getTargetLabel ( node , labelName ) === targetLabel ) ) ? nodeEntry ( node ) : undefined ;
752
+ } ) ;
758
753
return [ { definition : { type : "label" , node : targetLabel } , references } ] ;
759
754
}
760
755
@@ -780,24 +775,16 @@ namespace ts.FindAllReferences.Core {
780
775
}
781
776
782
777
function getAllReferencesForKeyword ( sourceFiles : ReadonlyArray < SourceFile > , keywordKind : ts . SyntaxKind , cancellationToken : CancellationToken ) : SymbolAndEntries [ ] {
783
- const references : NodeEntry [ ] = [ ] ;
784
- for ( const sourceFile of sourceFiles ) {
778
+ const references = flatMap ( sourceFiles , sourceFile => {
785
779
cancellationToken . throwIfCancellationRequested ( ) ;
786
- addReferencesForKeywordInFile ( sourceFile , keywordKind , tokenToString ( keywordKind ) , references ) ;
787
- }
780
+ return mapDefined ( getPossibleSymbolReferencePositions ( sourceFile , tokenToString ( keywordKind ) , sourceFile ) , position => {
781
+ const referenceLocation = getTouchingPropertyName ( sourceFile , position , /*includeJsDocComment*/ true ) ;
782
+ return referenceLocation . kind === keywordKind ? nodeEntry ( referenceLocation ) : undefined ;
783
+ } ) ;
784
+ } ) ;
788
785
return references . length ? [ { definition : { type : "keyword" , node : references [ 0 ] . node } , references } ] : undefined ;
789
786
}
790
787
791
- function addReferencesForKeywordInFile ( sourceFile : SourceFile , kind : SyntaxKind , searchText : string , references : Push < NodeEntry > ) : void {
792
- const possiblePositions = getPossibleSymbolReferencePositions ( sourceFile , searchText , sourceFile ) ;
793
- for ( const position of possiblePositions ) {
794
- const referenceLocation = getTouchingPropertyName ( sourceFile , position , /*includeJsDocComment*/ true ) ;
795
- if ( referenceLocation . kind === kind ) {
796
- references . push ( nodeEntry ( referenceLocation ) ) ;
797
- }
798
- }
799
- }
800
-
801
788
function getReferencesInSourceFile ( sourceFile : ts . SourceFile , search : Search , state : State ) : void {
802
789
state . cancellationToken . throwIfCancellationRequested ( ) ;
803
790
return getReferencesInContainer ( sourceFile , sourceFile , search , state ) ;
@@ -1288,26 +1275,20 @@ namespace ts.FindAllReferences.Core {
1288
1275
return undefined ;
1289
1276
}
1290
1277
1291
- const references : Entry [ ] = [ ] ;
1292
-
1293
1278
const sourceFile = searchSpaceNode . getSourceFile ( ) ;
1294
- const possiblePositions = getPossibleSymbolReferencePositions ( sourceFile , "super" , searchSpaceNode ) ;
1295
- for ( const position of possiblePositions ) {
1279
+ const references = mapDefined ( getPossibleSymbolReferencePositions ( sourceFile , "super" , searchSpaceNode ) , position => {
1296
1280
const node = getTouchingWord ( sourceFile , position , /*includeJsDocComment*/ false ) ;
1297
-
1298
1281
if ( ! node || node . kind !== SyntaxKind . SuperKeyword ) {
1299
- continue ;
1282
+ return ;
1300
1283
}
1301
1284
1302
1285
const container = getSuperContainer ( node , /*stopOnFunctions*/ false ) ;
1303
1286
1304
1287
// If we have a 'super' container, we must have an enclosing class.
1305
1288
// Now make sure the owning class is the same as the search-space
1306
1289
// and has the same static qualifier as the original 'super's owner.
1307
- if ( container && ( ModifierFlags . Static & getModifierFlags ( container ) ) === staticFlag && container . parent . symbol === searchSpaceNode . symbol ) {
1308
- references . push ( nodeEntry ( node ) ) ;
1309
- }
1310
- }
1290
+ return container && ( ModifierFlags . Static & getModifierFlags ( container ) ) === staticFlag && container . parent . symbol === searchSpaceNode . symbol ? nodeEntry ( node ) : undefined ;
1291
+ } ) ;
1311
1292
1312
1293
return [ { definition : { type : "symbol" , symbol : searchSpaceNode . symbol , node : superKeyword } , references } ] ;
1313
1294
}
@@ -1348,19 +1329,10 @@ namespace ts.FindAllReferences.Core {
1348
1329
}
1349
1330
1350
1331
const references : Entry [ ] = [ ] ;
1351
-
1352
- let possiblePositions : ReadonlyArray < number > ;
1353
- if ( searchSpaceNode . kind === SyntaxKind . SourceFile ) {
1354
- forEach ( sourceFiles , sourceFile => {
1355
- cancellationToken . throwIfCancellationRequested ( ) ;
1356
- possiblePositions = getPossibleSymbolReferencePositions ( sourceFile , "this" ) ;
1357
- getThisReferencesInFile ( sourceFile , sourceFile , possiblePositions , staticFlag , references ) ;
1358
- } ) ;
1359
- }
1360
- else {
1361
- const sourceFile = searchSpaceNode . getSourceFile ( ) ;
1362
- possiblePositions = getPossibleSymbolReferencePositions ( sourceFile , "this" , searchSpaceNode ) ;
1363
- getThisReferencesInFile ( sourceFile , searchSpaceNode , possiblePositions , staticFlag , references ) ;
1332
+ for ( const sourceFile of searchSpaceNode . kind === SyntaxKind . SourceFile ? sourceFiles : [ searchSpaceNode . getSourceFile ( ) ] ) {
1333
+ cancellationToken . throwIfCancellationRequested ( ) ;
1334
+ const positions = getPossibleSymbolReferencePositions ( sourceFile , "this" , isSourceFile ( searchSpaceNode ) ? sourceFile : searchSpaceNode ) ;
1335
+ getThisReferencesInFile ( sourceFile , searchSpaceNode . kind === SyntaxKind . SourceFile ? sourceFile : searchSpaceNode , positions , staticFlag , references ) ;
1364
1336
}
1365
1337
1366
1338
return [ {
@@ -1409,27 +1381,18 @@ namespace ts.FindAllReferences.Core {
1409
1381
}
1410
1382
1411
1383
function getReferencesForStringLiteral ( node : StringLiteral , sourceFiles : ReadonlyArray < SourceFile > , cancellationToken : CancellationToken ) : SymbolAndEntries [ ] {
1412
- const references : NodeEntry [ ] = [ ] ;
1413
-
1414
- for ( const sourceFile of sourceFiles ) {
1384
+ const references = flatMap ( sourceFiles , sourceFile => {
1415
1385
cancellationToken . throwIfCancellationRequested ( ) ;
1416
- const possiblePositions = getPossibleSymbolReferencePositions ( sourceFile , node . text ) ;
1417
- getReferencesForStringLiteralInFile ( sourceFile , node . text , possiblePositions , references ) ;
1418
- }
1386
+ return mapDefined ( getPossibleSymbolReferencePositions ( sourceFile , node . text ) , position => {
1387
+ const ref = tryCast ( getTouchingWord ( sourceFile , position , /*includeJsDocComment*/ false ) , isStringLiteral ) ;
1388
+ return ref && ref . text === node . text ? nodeEntry ( ref , /*isInString*/ true ) : undefined ;
1389
+ } ) ;
1390
+ } ) ;
1419
1391
1420
1392
return [ {
1421
1393
definition : { type : "string" , node } ,
1422
1394
references
1423
1395
} ] ;
1424
-
1425
- function getReferencesForStringLiteralInFile ( sourceFile : SourceFile , searchText : string , possiblePositions : ReadonlyArray < number > , references : Push < NodeEntry > ) : void {
1426
- for ( const position of possiblePositions ) {
1427
- const node = getTouchingWord ( sourceFile , position , /*includeJsDocComment*/ false ) ;
1428
- if ( node && node . kind === SyntaxKind . StringLiteral && ( node as StringLiteral ) . text === searchText ) {
1429
- references . push ( nodeEntry ( node , /*isInString*/ true ) ) ;
1430
- }
1431
- }
1432
- }
1433
1396
}
1434
1397
1435
1398
// For certain symbol kinds, we need to include other symbols in the search set.
0 commit comments