@@ -43,21 +43,10 @@ namespace ts.FindAllReferences {
43
43
44
44
export function findReferencedSymbols ( program : Program , cancellationToken : CancellationToken , sourceFiles : ReadonlyArray < SourceFile > , sourceFile : SourceFile , position : number ) : ReferencedSymbol [ ] | undefined {
45
45
const referencedSymbols = findAllReferencedSymbols ( program , cancellationToken , sourceFiles , sourceFile , position ) ;
46
-
47
- if ( ! referencedSymbols || ! referencedSymbols . length ) {
48
- return undefined ;
49
- }
50
-
51
- const out : ReferencedSymbol [ ] = [ ] ;
52
46
const checker = program . getTypeChecker ( ) ;
53
- for ( const { definition, references } of referencedSymbols ) {
47
+ return ! referencedSymbols || ! referencedSymbols . length ? undefined : mapDefined ( referencedSymbols , ( { definition, references } ) =>
54
48
// 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 ) } ) ;
61
50
}
62
51
63
52
export function getImplementationsAtPosition ( program : Program , cancellationToken : CancellationToken , sourceFiles : ReadonlyArray < SourceFile > , sourceFile : SourceFile , position : number ) : ImplementationLocation [ ] {
@@ -876,6 +865,8 @@ namespace ts.FindAllReferences.Core {
876
865
case SpecialSearchKind . Class :
877
866
addClassStaticThisReferences ( referenceLocation , search , state ) ;
878
867
break ;
868
+ default :
869
+ Debug . assertNever ( state . specialSearchKind ) ;
879
870
}
880
871
881
872
getImportOrExportReferences ( referenceLocation , referenceSymbol , search , state ) ;
@@ -1436,7 +1427,7 @@ namespace ts.FindAllReferences.Core {
1436
1427
// This is not needed when searching for re-exports.
1437
1428
function populateSearchSymbolSet ( symbol : Symbol , location : Node , checker : TypeChecker , implementations : boolean ) : Symbol [ ] {
1438
1429
// The search set contains at least the current symbol
1439
- const result = [ symbol ] ;
1430
+ const result : Symbol [ ] = [ ] ;
1440
1431
1441
1432
const containingObjectLiteralElement = getContainingObjectLiteralElement ( location ) ;
1442
1433
if ( containingObjectLiteralElement ) {
@@ -1453,9 +1444,9 @@ namespace ts.FindAllReferences.Core {
1453
1444
// If the location is in a context sensitive location (i.e. in an object literal) try
1454
1445
// to get a contextual type for it, and add the property symbol from the contextual
1455
1446
// 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
+ }
1459
1450
1460
1451
/* Because in short-hand property assignment, location has two meaning : property name and as value of the property
1461
1452
* 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 {
1496
1487
// If this is a union property, add all the symbols from all its source symbols in all unioned types.
1497
1488
// If the symbol is an instantiation from a another symbol (e.g. widened symbol) , add the root the list
1498
1489
for ( const rootSymbol of checker . getRootSymbols ( sym ) ) {
1499
- if ( rootSymbol !== sym ) {
1500
- result . push ( rootSymbol ) ;
1501
- }
1490
+ result . push ( rootSymbol ) ;
1502
1491
1503
1492
// Add symbol of properties/methods of the same name in base classes and implemented interfaces definitions
1504
1493
if ( ! implementations && rootSymbol . parent && rootSymbol . parent . flags & ( SymbolFlags . Class | SymbolFlags . Interface ) ) {
@@ -1522,7 +1511,7 @@ namespace ts.FindAllReferences.Core {
1522
1511
* @param previousIterationSymbolsCache a cache of symbol from previous iterations of calling this function to prevent infinite revisiting of the same symbol.
1523
1512
* The value of previousIterationSymbol is undefined when the function is first called.
1524
1513
*/
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 {
1526
1515
if ( ! symbol ) {
1527
1516
return ;
1528
1517
}
@@ -1591,9 +1580,7 @@ namespace ts.FindAllReferences.Core {
1591
1580
// compare to our searchSymbol
1592
1581
const containingObjectLiteralElement = getContainingObjectLiteralElement ( referenceLocation ) ;
1593
1582
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 ) ;
1597
1584
if ( contextualSymbol ) {
1598
1585
return contextualSymbol ;
1599
1586
}
@@ -1622,7 +1609,7 @@ namespace ts.FindAllReferences.Core {
1622
1609
function findRootSymbol ( sym : Symbol ) : Symbol | undefined {
1623
1610
// Unwrap symbols to get to the root (e.g. transient symbols as a result of widening)
1624
1611
// Or a union property, use its underlying unioned symbols
1625
- return forEach ( state . checker . getRootSymbols ( sym ) , rootSymbol => {
1612
+ return firstDefined ( checker . getRootSymbols ( sym ) , rootSymbol => {
1626
1613
// if it is in the list, then we are done
1627
1614
if ( search . includes ( rootSymbol ) ) {
1628
1615
return rootSymbol ;
@@ -1633,12 +1620,12 @@ namespace ts.FindAllReferences.Core {
1633
1620
// parent symbol
1634
1621
if ( rootSymbol . parent && rootSymbol . parent . flags & ( SymbolFlags . Class | SymbolFlags . Interface ) ) {
1635
1622
// 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 ) ) ) {
1637
1624
return undefined ;
1638
1625
}
1639
1626
1640
1627
const result : Symbol [ ] = [ ] ;
1641
- getPropertySymbolsFromBaseTypes ( rootSymbol . parent , rootSymbol . name , result , /*previousIterationSymbolsCache*/ createSymbolTable ( ) , state . checker ) ;
1628
+ getPropertySymbolsFromBaseTypes ( rootSymbol . parent , rootSymbol . name , result , /*previousIterationSymbolsCache*/ createSymbolTable ( ) , checker ) ;
1642
1629
return find ( result , search . includes ) ;
1643
1630
}
1644
1631
@@ -1660,28 +1647,12 @@ namespace ts.FindAllReferences.Core {
1660
1647
}
1661
1648
1662
1649
/** 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 ) ;
1666
1652
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 ;
1685
1656
}
1686
1657
1687
1658
/**
0 commit comments