1
1
/* @internal */
2
2
namespace ts . Completions {
3
+ export type Log = ( message : string ) => void ;
4
+
3
5
export enum SortText {
4
6
LocalDeclarationPriority = "0" ,
5
7
LocationPriority = "1" ,
@@ -8,9 +10,27 @@ namespace ts.Completions {
8
10
SuggestedClassMembers = "4" ,
9
11
GlobalsOrKeywords = "5" ,
10
12
AutoImportSuggestions = "6" ,
11
- JavascriptIdentifiers = "7"
13
+ JavascriptIdentifiers = "7" ,
14
+ DeprecatedLocalDeclarationPriority = "8" ,
15
+ DeprecatedLocationPriority = "9" ,
16
+ DeprecatedOptionalMember = "10" ,
17
+ DeprecatedMemberDeclaredBySpreadAssignment = "11" ,
18
+ DeprecatedSuggestedClassMembers = "12" ,
19
+ DeprecatedGlobalsOrKeywords = "13" ,
20
+ DeprecatedAutoImportSuggestions = "14"
21
+ }
22
+
23
+ enum SortTextId {
24
+ LocalDeclarationPriority ,
25
+ LocationPriority ,
26
+ OptionalMember ,
27
+ MemberDeclaredBySpreadAssignment ,
28
+ SuggestedClassMembers ,
29
+ GlobalsOrKeywords ,
30
+ AutoImportSuggestions
12
31
}
13
- export type Log = ( message : string ) => void ;
32
+
33
+ const DeprecatedSortTextStart = SortTextId . AutoImportSuggestions + 2 ; // for Javascript identifiers since with this change they are preferred over deprecated symbols
14
34
15
35
/**
16
36
* Special values for `CompletionInfo['source']` used to disambiguate
@@ -105,8 +125,8 @@ namespace ts.Completions {
105
125
*/
106
126
type SymbolOriginInfoMap = Record < number , SymbolOriginInfo > ;
107
127
108
- /** Map from symbol id -> SortText . */
109
- type SymbolSortTextMap = ( SortText | undefined ) [ ] ;
128
+ /** Map from symbol id -> SortTextId . */
129
+ type SymbolSortTextIdMap = ( SortTextId | undefined ) [ ] ;
110
130
111
131
const enum KeywordCompletionFilters {
112
132
None , // No keywords
@@ -205,7 +225,7 @@ namespace ts.Completions {
205
225
isJsxIdentifierExpected,
206
226
importCompletionNode,
207
227
insideJsDocTagTypeExpression,
208
- symbolToSortTextMap ,
228
+ symbolToSortTextIdMap ,
209
229
} = completionData ;
210
230
211
231
// Verify if the file is JSX language variant
@@ -238,7 +258,7 @@ namespace ts.Completions {
238
258
importCompletionNode ,
239
259
recommendedCompletion ,
240
260
symbolToOriginInfoMap ,
241
- symbolToSortTextMap
261
+ symbolToSortTextIdMap
242
262
) ;
243
263
getJSCompletionEntries ( sourceFile , location . pos , uniqueNames , compilerOptions . target ! , entries ) ; // TODO: GH#18217
244
264
}
@@ -266,7 +286,7 @@ namespace ts.Completions {
266
286
importCompletionNode ,
267
287
recommendedCompletion ,
268
288
symbolToOriginInfoMap ,
269
- symbolToSortTextMap
289
+ symbolToSortTextIdMap
270
290
) ;
271
291
}
272
292
@@ -566,7 +586,7 @@ namespace ts.Completions {
566
586
importCompletionNode ?: Node ,
567
587
recommendedCompletion ?: Symbol ,
568
588
symbolToOriginInfoMap ?: SymbolOriginInfoMap ,
569
- symbolToSortTextMap ?: SymbolSortTextMap ,
589
+ symbolToSortTextIdMap ?: SymbolSortTextIdMap ,
570
590
) : UniqueNameSet {
571
591
const start = timestamp ( ) ;
572
592
const variableDeclaration = getVariableDeclaration ( location ) ;
@@ -580,14 +600,16 @@ namespace ts.Completions {
580
600
const symbol = symbols [ i ] ;
581
601
const origin = symbolToOriginInfoMap ?. [ i ] ;
582
602
const info = getCompletionEntryDisplayNameForSymbol ( symbol , target , origin , kind , ! ! jsxIdentifierExpected ) ;
583
- if ( ! info || uniques . get ( info . name ) || kind === CompletionKind . Global && symbolToSortTextMap && ! shouldIncludeSymbol ( symbol , symbolToSortTextMap ) ) {
603
+ if ( ! info || uniques . get ( info . name ) || kind === CompletionKind . Global && symbolToSortTextIdMap && ! shouldIncludeSymbol ( symbol , symbolToSortTextIdMap ) ) {
584
604
continue ;
585
605
}
586
606
587
607
const { name, needsConvertPropertyAccess } = info ;
608
+ const sortTextId = symbolToSortTextIdMap ?. [ getSymbolId ( symbol ) ] ?? SortTextId . LocationPriority ;
609
+ const sortText = ( isDeprecated ( symbol , typeChecker ) ? DeprecatedSortTextStart + sortTextId : sortTextId ) . toString ( ) as SortText ;
588
610
const entry = createCompletionEntry (
589
611
symbol ,
590
- symbolToSortTextMap && symbolToSortTextMap [ getSymbolId ( symbol ) ] || SortText . LocationPriority ,
612
+ sortText ,
591
613
contextToken ,
592
614
location ,
593
615
sourceFile ,
@@ -623,7 +645,7 @@ namespace ts.Completions {
623
645
add : name => uniques . set ( name , true ) ,
624
646
} ;
625
647
626
- function shouldIncludeSymbol ( symbol : Symbol , symbolToSortTextMap : SymbolSortTextMap ) : boolean {
648
+ function shouldIncludeSymbol ( symbol : Symbol , symbolToSortTextIdMap : SymbolSortTextIdMap ) : boolean {
627
649
if ( ! isSourceFile ( location ) ) {
628
650
// export = /**/ here we want to get all meanings, so any symbol is ok
629
651
if ( isExportAssignment ( location . parent ) ) {
@@ -645,9 +667,9 @@ namespace ts.Completions {
645
667
// Auto Imports are not available for scripts so this conditional is always false
646
668
if ( ! ! sourceFile . externalModuleIndicator
647
669
&& ! compilerOptions . allowUmdGlobalAccess
648
- && symbolToSortTextMap [ getSymbolId ( symbol ) ] === SortText . GlobalsOrKeywords
649
- && ( symbolToSortTextMap [ getSymbolId ( symbolOrigin ) ] === SortText . AutoImportSuggestions
650
- || symbolToSortTextMap [ getSymbolId ( symbolOrigin ) ] === SortText . LocationPriority ) ) {
670
+ && symbolToSortTextIdMap [ getSymbolId ( symbol ) ] === SortTextId . GlobalsOrKeywords
671
+ && ( symbolToSortTextIdMap [ getSymbolId ( symbolOrigin ) ] === SortTextId . AutoImportSuggestions
672
+ || symbolToSortTextIdMap [ getSymbolId ( symbolOrigin ) ] === SortTextId . LocationPriority ) ) {
651
673
return false ;
652
674
}
653
675
// Continue with origin symbol
@@ -669,8 +691,6 @@ namespace ts.Completions {
669
691
}
670
692
}
671
693
672
-
673
-
674
694
function getLabelCompletionAtPosition ( node : BreakOrContinueStatement ) : CompletionInfo | undefined {
675
695
const entries = getLabelStatementCompletions ( node ) ;
676
696
if ( entries . length ) {
@@ -912,7 +932,7 @@ namespace ts.Completions {
912
932
readonly previousToken : Node | undefined ;
913
933
readonly isJsxInitializer : IsJsxInitializer ;
914
934
readonly insideJsDocTagTypeExpression : boolean ;
915
- readonly symbolToSortTextMap : SymbolSortTextMap ;
935
+ readonly symbolToSortTextIdMap : SymbolSortTextIdMap ;
916
936
readonly isTypeOnlyLocation : boolean ;
917
937
/** In JSX tag name and attribute names, identifiers like "my-tag" or "aria-name" is valid identifier. */
918
938
readonly isJsxIdentifierExpected : boolean ;
@@ -1239,7 +1259,7 @@ namespace ts.Completions {
1239
1259
// This also gets mutated in nested-functions after the return
1240
1260
let symbols : Symbol [ ] = [ ] ;
1241
1261
const symbolToOriginInfoMap : SymbolOriginInfoMap = [ ] ;
1242
- const symbolToSortTextMap : SymbolSortTextMap = [ ] ;
1262
+ const symbolToSortTextIdMap : SymbolSortTextIdMap = [ ] ;
1243
1263
const seenPropertySymbols = new Map < SymbolId , true > ( ) ;
1244
1264
const isTypeOnly = isTypeOnlyCompletion ( ) ;
1245
1265
const getModuleSpecifierResolutionHost = memoizeOne ( ( isFromPackageJson : boolean ) => {
@@ -1295,7 +1315,7 @@ namespace ts.Completions {
1295
1315
previousToken,
1296
1316
isJsxInitializer,
1297
1317
insideJsDocTagTypeExpression,
1298
- symbolToSortTextMap ,
1318
+ symbolToSortTextIdMap ,
1299
1319
isTypeOnlyLocation : isTypeOnly ,
1300
1320
isJsxIdentifierExpected,
1301
1321
importCompletionNode,
@@ -1484,7 +1504,7 @@ namespace ts.Completions {
1484
1504
1485
1505
function addSymbolSortInfo ( symbol : Symbol ) {
1486
1506
if ( isStaticProperty ( symbol ) ) {
1487
- symbolToSortTextMap [ getSymbolId ( symbol ) ] = SortText . LocalDeclarationPriority ;
1507
+ symbolToSortTextIdMap [ getSymbolId ( symbol ) ] = SortTextId . LocalDeclarationPriority ;
1488
1508
}
1489
1509
}
1490
1510
@@ -1601,7 +1621,7 @@ namespace ts.Completions {
1601
1621
for ( const symbol of symbols ) {
1602
1622
if ( ! typeChecker . isArgumentsSymbol ( symbol ) &&
1603
1623
! some ( symbol . declarations , d => d . getSourceFile ( ) === sourceFile ) ) {
1604
- symbolToSortTextMap [ getSymbolId ( symbol ) ] = SortText . GlobalsOrKeywords ;
1624
+ symbolToSortTextIdMap [ getSymbolId ( symbol ) ] = SortTextId . GlobalsOrKeywords ;
1605
1625
}
1606
1626
}
1607
1627
@@ -1612,7 +1632,7 @@ namespace ts.Completions {
1612
1632
for ( const symbol of getPropertiesForCompletion ( thisType , typeChecker ) ) {
1613
1633
symbolToOriginInfoMap [ symbols . length ] = { kind : SymbolOriginInfoKind . ThisType } ;
1614
1634
symbols . push ( symbol ) ;
1615
- symbolToSortTextMap [ getSymbolId ( symbol ) ] = SortText . SuggestedClassMembers ;
1635
+ symbolToSortTextIdMap [ getSymbolId ( symbol ) ] = SortTextId . SuggestedClassMembers ;
1616
1636
}
1617
1637
}
1618
1638
}
@@ -1694,7 +1714,7 @@ namespace ts.Completions {
1694
1714
return false ;
1695
1715
}
1696
1716
1697
- /** Mutates `symbols`, `symbolToOriginInfoMap`, and `symbolToSortTextMap ` */
1717
+ /** Mutates `symbols`, `symbolToOriginInfoMap`, and `symbolToSortTextIdMap ` */
1698
1718
function collectAutoImports ( resolveModuleSpecifiers : boolean ) {
1699
1719
if ( ! shouldOfferImportCompletions ( ) ) return ;
1700
1720
Debug . assert ( ! detailsEntryId ?. data ) ;
@@ -1765,12 +1785,12 @@ namespace ts.Completions {
1765
1785
1766
1786
function pushAutoImportSymbol ( symbol : Symbol , origin : SymbolOriginInfoResolvedExport | SymbolOriginInfoExport ) {
1767
1787
const symbolId = getSymbolId ( symbol ) ;
1768
- if ( symbolToSortTextMap [ symbolId ] === SortText . GlobalsOrKeywords ) {
1788
+ if ( symbolToSortTextIdMap [ symbolId ] === SortTextId . GlobalsOrKeywords ) {
1769
1789
// If an auto-importable symbol is available as a global, don't add the auto import
1770
1790
return ;
1771
1791
}
1772
1792
symbolToOriginInfoMap [ symbols . length ] = origin ;
1773
- symbolToSortTextMap [ symbolId ] = importCompletionNode ? SortText . LocationPriority : SortText . AutoImportSuggestions ;
1793
+ symbolToSortTextIdMap [ symbolId ] = importCompletionNode ? SortTextId . LocationPriority : SortTextId . AutoImportSuggestions ;
1774
1794
symbols . push ( symbol ) ;
1775
1795
}
1776
1796
@@ -2085,7 +2105,7 @@ namespace ts.Completions {
2085
2105
localsContainer . locals ?. forEach ( ( symbol , name ) => {
2086
2106
symbols . push ( symbol ) ;
2087
2107
if ( localsContainer . symbol ?. exports ?. has ( name ) ) {
2088
- symbolToSortTextMap [ getSymbolId ( symbol ) ] = SortText . OptionalMember ;
2108
+ symbolToSortTextIdMap [ getSymbolId ( symbol ) ] = SortTextId . OptionalMember ;
2089
2109
}
2090
2110
} ) ;
2091
2111
return GlobalsSearch . Success ;
@@ -2533,7 +2553,8 @@ namespace ts.Completions {
2533
2553
function setSortTextToOptionalMember ( ) {
2534
2554
symbols . forEach ( m => {
2535
2555
if ( m . flags & SymbolFlags . Optional ) {
2536
- symbolToSortTextMap [ getSymbolId ( m ) ] = symbolToSortTextMap [ getSymbolId ( m ) ] || SortText . OptionalMember ;
2556
+ const symbolId = getSymbolId ( m ) ;
2557
+ symbolToSortTextIdMap [ symbolId ] = symbolToSortTextIdMap [ symbolId ] ?? SortTextId . OptionalMember ;
2537
2558
}
2538
2559
} ) ;
2539
2560
}
@@ -2545,7 +2566,7 @@ namespace ts.Completions {
2545
2566
}
2546
2567
for ( const contextualMemberSymbol of contextualMemberSymbols ) {
2547
2568
if ( membersDeclaredBySpreadAssignment . has ( contextualMemberSymbol . name ) ) {
2548
- symbolToSortTextMap [ getSymbolId ( contextualMemberSymbol ) ] = SortText . MemberDeclaredBySpreadAssignment ;
2569
+ symbolToSortTextIdMap [ getSymbolId ( contextualMemberSymbol ) ] = SortTextId . MemberDeclaredBySpreadAssignment ;
2549
2570
}
2550
2571
}
2551
2572
}
@@ -3091,4 +3112,9 @@ namespace ts.Completions {
3091
3112
addToSeen ( seenModules , getSymbolId ( sym ) ) &&
3092
3113
checker . getExportsOfModule ( sym ) . some ( e => symbolCanBeReferencedAtTypeLocation ( e , checker , seenModules ) ) ;
3093
3114
}
3115
+
3116
+ function isDeprecated ( symbol : Symbol , checker : TypeChecker ) {
3117
+ const declarations = skipAlias ( symbol , checker ) . declarations ;
3118
+ return ! ! length ( declarations ) && every ( declarations , isDeprecatedDeclaration ) ;
3119
+ }
3094
3120
}
0 commit comments