@@ -43,21 +43,10 @@ namespace ts.FindAllReferences {
4343
4444 export function findReferencedSymbols ( program : Program , cancellationToken : CancellationToken , sourceFiles : ReadonlyArray < SourceFile > , sourceFile : SourceFile , position : number ) : ReferencedSymbol [ ] | undefined {
4545 const referencedSymbols = findAllReferencedSymbols ( program , cancellationToken , sourceFiles , sourceFile , position ) ;
46-
47- if ( ! referencedSymbols || ! referencedSymbols . length ) {
48- return undefined ;
49- }
50-
51- const out : ReferencedSymbol [ ] = [ ] ;
5246 const checker = program . getTypeChecker ( ) ;
53- for ( const { definition, references } of referencedSymbols ) {
47+ return ! referencedSymbols || ! referencedSymbols . length ? undefined : mapDefined ( referencedSymbols , ( { definition, references } ) =>
5448 // Only include referenced symbols that have a valid definition.
55- if ( definition ) {
56- out . push ( { definition : definitionToReferencedSymbolDefinitionInfo ( definition , checker ) , references : references . map ( toReferenceEntry ) } ) ;
57- }
58- }
59-
60- return out ;
49+ definition && { definition : definitionToReferencedSymbolDefinitionInfo ( definition , checker ) , references : references . map ( toReferenceEntry ) } ) ;
6150 }
6251
6352 export function getImplementationsAtPosition ( program : Program , cancellationToken : CancellationToken , sourceFiles : ReadonlyArray < SourceFile > , sourceFile : SourceFile , position : number ) : ImplementationLocation [ ] {
@@ -876,6 +865,8 @@ namespace ts.FindAllReferences.Core {
876865 case SpecialSearchKind . Class :
877866 addClassStaticThisReferences ( referenceLocation , search , state ) ;
878867 break ;
868+ default :
869+ Debug . assertNever ( state . specialSearchKind ) ;
879870 }
880871
881872 getImportOrExportReferences ( referenceLocation , referenceSymbol , search , state ) ;
@@ -1436,7 +1427,7 @@ namespace ts.FindAllReferences.Core {
14361427 // This is not needed when searching for re-exports.
14371428 function populateSearchSymbolSet ( symbol : Symbol , location : Node , checker : TypeChecker , implementations : boolean ) : Symbol [ ] {
14381429 // The search set contains at least the current symbol
1439- const result = [ symbol ] ;
1430+ const result : Symbol [ ] = [ ] ;
14401431
14411432 const containingObjectLiteralElement = getContainingObjectLiteralElement ( location ) ;
14421433 if ( containingObjectLiteralElement ) {
@@ -1453,9 +1444,9 @@ namespace ts.FindAllReferences.Core {
14531444 // If the location is in a context sensitive location (i.e. in an object literal) try
14541445 // to get a contextual type for it, and add the property symbol from the contextual
14551446 // type to the search set
1456- forEach ( getPropertySymbolsFromContextualType ( containingObjectLiteralElement , checker ) , contextualSymbol => {
1457- addRange ( result , checker . getRootSymbols ( contextualSymbol ) ) ;
1458- } ) ;
1447+ for ( const contextualSymbol of getPropertySymbolsFromContextualType ( containingObjectLiteralElement , checker ) ) {
1448+ addRootSymbols ( contextualSymbol ) ;
1449+ }
14591450
14601451 /* Because in short-hand property assignment, location has two meaning : property name and as value of the property
14611452 * When we do findAllReference at the position of the short-hand property assignment, we would want to have references to position of
@@ -1496,9 +1487,7 @@ namespace ts.FindAllReferences.Core {
14961487 // If this is a union property, add all the symbols from all its source symbols in all unioned types.
14971488 // If the symbol is an instantiation from a another symbol (e.g. widened symbol) , add the root the list
14981489 for ( const rootSymbol of checker . getRootSymbols ( sym ) ) {
1499- if ( rootSymbol !== sym ) {
1500- result . push ( rootSymbol ) ;
1501- }
1490+ result . push ( rootSymbol ) ;
15021491
15031492 // Add symbol of properties/methods of the same name in base classes and implemented interfaces definitions
15041493 if ( ! implementations && rootSymbol . parent && rootSymbol . parent . flags & ( SymbolFlags . Class | SymbolFlags . Interface ) ) {
@@ -1522,7 +1511,7 @@ namespace ts.FindAllReferences.Core {
15221511 * @param previousIterationSymbolsCache a cache of symbol from previous iterations of calling this function to prevent infinite revisiting of the same symbol.
15231512 * The value of previousIterationSymbol is undefined when the function is first called.
15241513 */
1525- function getPropertySymbolsFromBaseTypes ( symbol : Symbol , propertyName : string , result : Symbol [ ] , previousIterationSymbolsCache : SymbolTable , checker : TypeChecker ) : void {
1514+ function getPropertySymbolsFromBaseTypes ( symbol : Symbol , propertyName : string , result : Push < Symbol > , previousIterationSymbolsCache : SymbolTable , checker : TypeChecker ) : void {
15261515 if ( ! symbol ) {
15271516 return ;
15281517 }
@@ -1591,9 +1580,7 @@ namespace ts.FindAllReferences.Core {
15911580 // compare to our searchSymbol
15921581 const containingObjectLiteralElement = getContainingObjectLiteralElement ( referenceLocation ) ;
15931582 if ( containingObjectLiteralElement ) {
1594- const contextualSymbol = forEach ( getPropertySymbolsFromContextualType ( containingObjectLiteralElement , checker ) , contextualSymbol =>
1595- find ( checker . getRootSymbols ( contextualSymbol ) , search . includes ) ) ;
1596-
1583+ const contextualSymbol = firstDefined ( getPropertySymbolsFromContextualType ( containingObjectLiteralElement , checker ) , findRootSymbol ) ;
15971584 if ( contextualSymbol ) {
15981585 return contextualSymbol ;
15991586 }
@@ -1622,7 +1609,7 @@ namespace ts.FindAllReferences.Core {
16221609 function findRootSymbol ( sym : Symbol ) : Symbol | undefined {
16231610 // Unwrap symbols to get to the root (e.g. transient symbols as a result of widening)
16241611 // Or a union property, use its underlying unioned symbols
1625- return forEach ( state . checker . getRootSymbols ( sym ) , rootSymbol => {
1612+ return firstDefined ( checker . getRootSymbols ( sym ) , rootSymbol => {
16261613 // if it is in the list, then we are done
16271614 if ( search . includes ( rootSymbol ) ) {
16281615 return rootSymbol ;
@@ -1633,12 +1620,12 @@ namespace ts.FindAllReferences.Core {
16331620 // parent symbol
16341621 if ( rootSymbol . parent && rootSymbol . parent . flags & ( SymbolFlags . Class | SymbolFlags . Interface ) ) {
16351622 // Parents will only be defined if implementations is true
1636- if ( search . parents && ! some ( search . parents , parent => explicitlyInheritsFrom ( rootSymbol . parent , parent , state . inheritsFromCache , state . checker ) ) ) {
1623+ if ( search . parents && ! some ( search . parents , parent => explicitlyInheritsFrom ( rootSymbol . parent , parent , state . inheritsFromCache , checker ) ) ) {
16371624 return undefined ;
16381625 }
16391626
16401627 const result : Symbol [ ] = [ ] ;
1641- getPropertySymbolsFromBaseTypes ( rootSymbol . parent , rootSymbol . name , result , /*previousIterationSymbolsCache*/ createSymbolTable ( ) , state . checker ) ;
1628+ getPropertySymbolsFromBaseTypes ( rootSymbol . parent , rootSymbol . name , result , /*previousIterationSymbolsCache*/ createSymbolTable ( ) , checker ) ;
16421629 return find ( result , search . includes ) ;
16431630 }
16441631
@@ -1660,28 +1647,12 @@ namespace ts.FindAllReferences.Core {
16601647 }
16611648
16621649 /** Gets all symbols for one property. Does not get symbols for every property. */
1663- function getPropertySymbolsFromContextualType ( node : ObjectLiteralElement , checker : TypeChecker ) : Symbol [ ] | undefined {
1664- const objectLiteral = < ObjectLiteralExpression > node . parent ;
1665- const contextualType = checker . getContextualType ( objectLiteral ) ;
1650+ function getPropertySymbolsFromContextualType ( node : ObjectLiteralElement , checker : TypeChecker ) : ReadonlyArray < Symbol > {
1651+ const contextualType = checker . getContextualType ( < ObjectLiteralExpression > node . parent ) ;
16661652 const name = getNameFromObjectLiteralElement ( node ) ;
1667- if ( name && contextualType ) {
1668- const result : Symbol [ ] = [ ] ;
1669- const symbol = contextualType . getProperty ( name ) ;
1670- if ( symbol ) {
1671- result . push ( symbol ) ;
1672- }
1673-
1674- if ( contextualType . flags & TypeFlags . Union ) {
1675- forEach ( ( < UnionType > contextualType ) . types , t => {
1676- const symbol = t . getProperty ( name ) ;
1677- if ( symbol ) {
1678- result . push ( symbol ) ;
1679- }
1680- } ) ;
1681- }
1682- return result ;
1683- }
1684- return undefined ;
1653+ const symbol = contextualType && name && contextualType . getProperty ( name ) ;
1654+ return symbol ? [ symbol ] :
1655+ contextualType && contextualType . flags & TypeFlags . Union ? mapDefined ( ( < UnionType > contextualType ) . types , t => t . getProperty ( name ) ) : emptyArray ;
16851656 }
16861657
16871658 /**
0 commit comments