@@ -716,6 +716,10 @@ namespace ts.FindAllReferences.Core {
716
716
} ) ;
717
717
}
718
718
719
+ function getPossibleSymbolReferenceNodes ( sourceFile : SourceFile , symbolName : string , container : Node = sourceFile ) : ReadonlyArray < Node > {
720
+ return getPossibleSymbolReferencePositions ( sourceFile , symbolName , container ) . map ( pos => getTouchingPropertyName ( sourceFile , pos , /*includeJsDocComment*/ true ) ) ;
721
+ }
722
+
719
723
function getPossibleSymbolReferencePositions ( sourceFile : SourceFile , symbolName : string , container : Node = sourceFile ) : ReadonlyArray < number > {
720
724
const positions : number [ ] = [ ] ;
721
725
@@ -754,11 +758,9 @@ namespace ts.FindAllReferences.Core {
754
758
function getLabelReferencesInNode ( container : Node , targetLabel : Identifier ) : SymbolAndEntries [ ] {
755
759
const sourceFile = container . getSourceFile ( ) ;
756
760
const labelName = targetLabel . text ;
757
- const references = mapDefined ( getPossibleSymbolReferencePositions ( sourceFile , labelName , container ) , position => {
758
- const node = getTouchingWord ( sourceFile , position , /*includeJsDocComment*/ false ) ;
761
+ const references = mapDefined ( getPossibleSymbolReferenceNodes ( sourceFile , labelName , container ) , node =>
759
762
// Only pick labels that are either the target label, or have a target that is the target label
760
- return node && ( node === targetLabel || ( isJumpStatementTarget ( node ) && getTargetLabel ( node , labelName ) === targetLabel ) ) ? nodeEntry ( node ) : undefined ;
761
- } ) ;
763
+ node === targetLabel || ( isJumpStatementTarget ( node ) && getTargetLabel ( node , labelName ) === targetLabel ) ? nodeEntry ( node ) : undefined ) ;
762
764
return [ { definition : { type : "label" , node : targetLabel } , references } ] ;
763
765
}
764
766
@@ -788,10 +790,8 @@ namespace ts.FindAllReferences.Core {
788
790
function getAllReferencesForKeyword ( sourceFiles : ReadonlyArray < SourceFile > , keywordKind : SyntaxKind , cancellationToken : CancellationToken ) : SymbolAndEntries [ ] {
789
791
const references = flatMap ( sourceFiles , sourceFile => {
790
792
cancellationToken . throwIfCancellationRequested ( ) ;
791
- return mapDefined ( getPossibleSymbolReferencePositions ( sourceFile , tokenToString ( keywordKind ) , sourceFile ) , position => {
792
- const referenceLocation = getTouchingPropertyName ( sourceFile , position , /*includeJsDocComment*/ true ) ;
793
- return referenceLocation . kind === keywordKind ? nodeEntry ( referenceLocation ) : undefined ;
794
- } ) ;
793
+ return mapDefined ( getPossibleSymbolReferenceNodes ( sourceFile , tokenToString ( keywordKind ) , sourceFile ) , referenceLocation =>
794
+ referenceLocation . kind === keywordKind ? nodeEntry ( referenceLocation ) : undefined ) ;
795
795
} ) ;
796
796
return references . length ? [ { definition : { type : "keyword" , node : references [ 0 ] . node } , references } ] : undefined ;
797
797
}
@@ -1255,9 +1255,8 @@ namespace ts.FindAllReferences.Core {
1255
1255
}
1256
1256
1257
1257
const sourceFile = searchSpaceNode . getSourceFile ( ) ;
1258
- const references = mapDefined ( getPossibleSymbolReferencePositions ( sourceFile , "super" , searchSpaceNode ) , position => {
1259
- const node = getTouchingWord ( sourceFile , position , /*includeJsDocComment*/ false ) ;
1260
- if ( ! node || node . kind !== SyntaxKind . SuperKeyword ) {
1258
+ const references = mapDefined ( getPossibleSymbolReferenceNodes ( sourceFile , "super" , searchSpaceNode ) , node => {
1259
+ if ( node . kind !== SyntaxKind . SuperKeyword ) {
1261
1260
return ;
1262
1261
}
1263
1262
@@ -1307,65 +1306,42 @@ namespace ts.FindAllReferences.Core {
1307
1306
return undefined ;
1308
1307
}
1309
1308
1310
- const references : Entry [ ] = [ ] ;
1311
- for ( const sourceFile of searchSpaceNode . kind === SyntaxKind . SourceFile ? sourceFiles : [ searchSpaceNode . getSourceFile ( ) ] ) {
1309
+ const references = flatMap ( searchSpaceNode . kind === SyntaxKind . SourceFile ? sourceFiles : [ searchSpaceNode . getSourceFile ( ) ] , sourceFile => {
1312
1310
cancellationToken . throwIfCancellationRequested ( ) ;
1313
- const positions = getPossibleSymbolReferencePositions ( sourceFile , "this" , isSourceFile ( searchSpaceNode ) ? sourceFile : searchSpaceNode ) ;
1314
- getThisReferencesInFile ( sourceFile , searchSpaceNode . kind === SyntaxKind . SourceFile ? sourceFile : searchSpaceNode , positions , staticFlag , references ) ;
1315
- }
1311
+ return getPossibleSymbolReferenceNodes ( sourceFile , "this" , isSourceFile ( searchSpaceNode ) ? sourceFile : searchSpaceNode ) . filter ( node => {
1312
+ if ( ! isThis ( node ) ) {
1313
+ return false ;
1314
+ }
1315
+ const container = getThisContainer ( node , /* includeArrowFunctions */ false ) ;
1316
+ switch ( searchSpaceNode . kind ) {
1317
+ case SyntaxKind . FunctionExpression :
1318
+ case SyntaxKind . FunctionDeclaration :
1319
+ return searchSpaceNode . symbol === container . symbol ;
1320
+ case SyntaxKind . MethodDeclaration :
1321
+ case SyntaxKind . MethodSignature :
1322
+ return isObjectLiteralMethod ( searchSpaceNode ) && searchSpaceNode . symbol === container . symbol ;
1323
+ case SyntaxKind . ClassExpression :
1324
+ case SyntaxKind . ClassDeclaration :
1325
+ // Make sure the container belongs to the same class
1326
+ // and has the appropriate static modifier from the original container.
1327
+ return container . parent && searchSpaceNode . symbol === container . parent . symbol && ( getModifierFlags ( container ) & ModifierFlags . Static ) === staticFlag ;
1328
+ case SyntaxKind . SourceFile :
1329
+ return container . kind === SyntaxKind . SourceFile && ! isExternalModule ( < SourceFile > container ) ;
1330
+ }
1331
+ } ) ;
1332
+ } ) . map ( n => nodeEntry ( n ) ) ;
1316
1333
1317
1334
return [ {
1318
1335
definition : { type : "this" , node : thisOrSuperKeyword } ,
1319
1336
references
1320
1337
} ] ;
1321
1338
}
1322
1339
1323
- function getThisReferencesInFile ( sourceFile : SourceFile , searchSpaceNode : Node , possiblePositions : ReadonlyArray < number > , staticFlag : ModifierFlags , result : Push < Entry > ) : void {
1324
- forEach ( possiblePositions , position => {
1325
- const node = getTouchingWord ( sourceFile , position , /*includeJsDocComment*/ false ) ;
1326
- if ( ! node || ! isThis ( node ) ) {
1327
- return ;
1328
- }
1329
-
1330
- const container = getThisContainer ( node , /* includeArrowFunctions */ false ) ;
1331
-
1332
- switch ( searchSpaceNode . kind ) {
1333
- case SyntaxKind . FunctionExpression :
1334
- case SyntaxKind . FunctionDeclaration :
1335
- if ( searchSpaceNode . symbol === container . symbol ) {
1336
- result . push ( nodeEntry ( node ) ) ;
1337
- }
1338
- break ;
1339
- case SyntaxKind . MethodDeclaration :
1340
- case SyntaxKind . MethodSignature :
1341
- if ( isObjectLiteralMethod ( searchSpaceNode ) && searchSpaceNode . symbol === container . symbol ) {
1342
- result . push ( nodeEntry ( node ) ) ;
1343
- }
1344
- break ;
1345
- case SyntaxKind . ClassExpression :
1346
- case SyntaxKind . ClassDeclaration :
1347
- // Make sure the container belongs to the same class
1348
- // and has the appropriate static modifier from the original container.
1349
- if ( container . parent && searchSpaceNode . symbol === container . parent . symbol && ( getModifierFlags ( container ) & ModifierFlags . Static ) === staticFlag ) {
1350
- result . push ( nodeEntry ( node ) ) ;
1351
- }
1352
- break ;
1353
- case SyntaxKind . SourceFile :
1354
- if ( container . kind === SyntaxKind . SourceFile && ! isExternalModule ( < SourceFile > container ) ) {
1355
- result . push ( nodeEntry ( node ) ) ;
1356
- }
1357
- break ;
1358
- }
1359
- } ) ;
1360
- }
1361
-
1362
1340
function getReferencesForStringLiteral ( node : StringLiteral , sourceFiles : ReadonlyArray < SourceFile > , cancellationToken : CancellationToken ) : SymbolAndEntries [ ] {
1363
1341
const references = flatMap ( sourceFiles , sourceFile => {
1364
1342
cancellationToken . throwIfCancellationRequested ( ) ;
1365
- return mapDefined ( getPossibleSymbolReferencePositions ( sourceFile , node . text ) , position => {
1366
- const ref = tryCast ( getTouchingWord ( sourceFile , position , /*includeJsDocComment*/ false ) , isStringLiteral ) ;
1367
- return ref && ref . text === node . text ? nodeEntry ( ref , /*isInString*/ true ) : undefined ;
1368
- } ) ;
1343
+ return mapDefined ( getPossibleSymbolReferenceNodes ( sourceFile , node . text ) , ref =>
1344
+ isStringLiteral ( ref ) && ref . text === node . text ? nodeEntry ( ref , /*isInString*/ true ) : undefined ) ;
1369
1345
} ) ;
1370
1346
1371
1347
return [ {
0 commit comments