Skip to content

Commit 975aabe

Browse files
Ensure all SortText entries have the same length (#45292)
* Ensure all `SortText` entries have the same length. * Update Baselines and/or Applied Lint Fixes * Update the exact same enum to the exact same values in fourslash. 🙄 * Make `SortTextId` a const enum, switch to use an explicit offset in `SortTextId`. Co-authored-by: TypeScript Bot <[email protected]>
1 parent f30ba36 commit 975aabe

10 files changed

+4477
-4470
lines changed

src/services/completions.ts

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,43 @@ namespace ts.Completions {
66

77
export type Log = (message: string) => void;
88

9+
// NOTE: Make sure that each entry has the exact same number of digits
10+
// since many implementations will sort by string contents,
11+
// where "10" is considered less than "2".
912
export enum SortText {
10-
LocalDeclarationPriority = "0",
11-
LocationPriority = "1",
12-
OptionalMember = "2",
13-
MemberDeclaredBySpreadAssignment = "3",
14-
SuggestedClassMembers = "4",
15-
GlobalsOrKeywords = "5",
16-
AutoImportSuggestions = "6",
17-
JavascriptIdentifiers = "7",
18-
DeprecatedLocalDeclarationPriority = "8",
19-
DeprecatedLocationPriority = "9",
20-
DeprecatedOptionalMember = "10",
21-
DeprecatedMemberDeclaredBySpreadAssignment = "11",
22-
DeprecatedSuggestedClassMembers = "12",
23-
DeprecatedGlobalsOrKeywords = "13",
24-
DeprecatedAutoImportSuggestions = "14"
25-
}
26-
27-
enum SortTextId {
28-
LocalDeclarationPriority,
29-
LocationPriority,
30-
OptionalMember,
31-
MemberDeclaredBySpreadAssignment,
32-
SuggestedClassMembers,
33-
GlobalsOrKeywords,
34-
AutoImportSuggestions
35-
}
36-
37-
// for JavaScript identifiers since they are preferred over deprecated symbols
38-
const DeprecatedSortTextStart = SortTextId.AutoImportSuggestions + 2;
13+
LocalDeclarationPriority = "10",
14+
LocationPriority = "11",
15+
OptionalMember = "12",
16+
MemberDeclaredBySpreadAssignment = "13",
17+
SuggestedClassMembers = "14",
18+
GlobalsOrKeywords = "15",
19+
AutoImportSuggestions = "16",
20+
JavascriptIdentifiers = "17",
21+
DeprecatedLocalDeclarationPriority = "18",
22+
DeprecatedLocationPriority = "19",
23+
DeprecatedOptionalMember = "20",
24+
DeprecatedMemberDeclaredBySpreadAssignment = "21",
25+
DeprecatedSuggestedClassMembers = "22",
26+
DeprecatedGlobalsOrKeywords = "23",
27+
DeprecatedAutoImportSuggestions = "24"
28+
}
29+
30+
const enum SortTextId {
31+
LocalDeclarationPriority = 10,
32+
LocationPriority = 11,
33+
OptionalMember = 12,
34+
MemberDeclaredBySpreadAssignment = 13,
35+
SuggestedClassMembers = 14,
36+
GlobalsOrKeywords = 15,
37+
AutoImportSuggestions = 16,
38+
39+
// Don't use these directly.
40+
_JavaScriptIdentifiers = 17,
41+
_DeprecatedStart = 18,
42+
_First = LocalDeclarationPriority,
43+
44+
DeprecatedOffset = _DeprecatedStart - _First,
45+
}
3946

4047
/**
4148
* Special values for `CompletionInfo['source']` used to disambiguate
@@ -787,7 +794,7 @@ namespace ts.Completions {
787794

788795
const { name, needsConvertPropertyAccess } = info;
789796
const sortTextId = symbolToSortTextIdMap?.[getSymbolId(symbol)] ?? SortTextId.LocationPriority;
790-
const sortText = (isDeprecated(symbol, typeChecker) ? DeprecatedSortTextStart + sortTextId : sortTextId).toString() as SortText;
797+
const sortText = (isDeprecated(symbol, typeChecker) ? SortTextId.DeprecatedOffset + sortTextId : sortTextId).toString() as SortText;
791798
const entry = createCompletionEntry(
792799
symbol,
793800
sortText,

tests/baselines/reference/completionEntryForUnionMethod.baseline

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"name": "length",
1919
"kind": "property",
2020
"kindModifiers": "declare",
21-
"sortText": "1",
21+
"sortText": "11",
2222
"displayParts": [
2323
{
2424
"text": "(",
@@ -84,7 +84,7 @@
8484
"name": "toString",
8585
"kind": "method",
8686
"kindModifiers": "declare",
87-
"sortText": "1",
87+
"sortText": "11",
8888
"displayParts": [
8989
{
9090
"text": "(",
@@ -158,7 +158,7 @@
158158
"name": "toLocaleString",
159159
"kind": "method",
160160
"kindModifiers": "declare",
161-
"sortText": "1",
161+
"sortText": "11",
162162
"displayParts": [
163163
{
164164
"text": "(",
@@ -232,7 +232,7 @@
232232
"name": "pop",
233233
"kind": "method",
234234
"kindModifiers": "declare",
235-
"sortText": "1",
235+
"sortText": "11",
236236
"displayParts": [
237237
{
238238
"text": "(",
@@ -322,7 +322,7 @@
322322
"name": "push",
323323
"kind": "method",
324324
"kindModifiers": "declare",
325-
"sortText": "1",
325+
"sortText": "11",
326326
"displayParts": [
327327
{
328328
"text": "(",
@@ -439,7 +439,7 @@
439439
"name": "concat",
440440
"kind": "property",
441441
"kindModifiers": "declare",
442-
"sortText": "1",
442+
"sortText": "11",
443443
"displayParts": [
444444
{
445445
"text": "(",
@@ -941,7 +941,7 @@
941941
"name": "join",
942942
"kind": "method",
943943
"kindModifiers": "declare",
944-
"sortText": "1",
944+
"sortText": "11",
945945
"displayParts": [
946946
{
947947
"text": "(",
@@ -1054,7 +1054,7 @@
10541054
"name": "reverse",
10551055
"kind": "method",
10561056
"kindModifiers": "declare",
1057-
"sortText": "1",
1057+
"sortText": "11",
10581058
"displayParts": [
10591059
{
10601060
"text": "(",
@@ -1160,7 +1160,7 @@
11601160
"name": "shift",
11611161
"kind": "method",
11621162
"kindModifiers": "declare",
1163-
"sortText": "1",
1163+
"sortText": "11",
11641164
"displayParts": [
11651165
{
11661166
"text": "(",
@@ -1250,7 +1250,7 @@
12501250
"name": "slice",
12511251
"kind": "method",
12521252
"kindModifiers": "declare",
1253-
"sortText": "1",
1253+
"sortText": "11",
12541254
"displayParts": [
12551255
{
12561256
"text": "(",
@@ -1440,7 +1440,7 @@
14401440
"name": "sort",
14411441
"kind": "method",
14421442
"kindModifiers": "declare",
1443-
"sortText": "1",
1443+
"sortText": "11",
14441444
"displayParts": [
14451445
{
14461446
"text": "(",
@@ -1733,7 +1733,7 @@
17331733
"name": "splice",
17341734
"kind": "method",
17351735
"kindModifiers": "declare",
1736-
"sortText": "1",
1736+
"sortText": "11",
17371737
"displayParts": [
17381738
{
17391739
"text": "(",
@@ -1956,7 +1956,7 @@
19561956
"name": "unshift",
19571957
"kind": "method",
19581958
"kindModifiers": "declare",
1959-
"sortText": "1",
1959+
"sortText": "11",
19601960
"displayParts": [
19611961
{
19621962
"text": "(",
@@ -2073,7 +2073,7 @@
20732073
"name": "indexOf",
20742074
"kind": "method",
20752075
"kindModifiers": "declare",
2076-
"sortText": "1",
2076+
"sortText": "11",
20772077
"displayParts": [
20782078
{
20792079
"text": "(",
@@ -2223,7 +2223,7 @@
22232223
"name": "lastIndexOf",
22242224
"kind": "method",
22252225
"kindModifiers": "declare",
2226-
"sortText": "1",
2226+
"sortText": "11",
22272227
"displayParts": [
22282228
{
22292229
"text": "(",
@@ -2373,7 +2373,7 @@
23732373
"name": "every",
23742374
"kind": "property",
23752375
"kindModifiers": "declare",
2376-
"sortText": "1",
2376+
"sortText": "11",
23772377
"displayParts": [
23782378
{
23792379
"text": "(",
@@ -2961,7 +2961,7 @@
29612961
"name": "some",
29622962
"kind": "method",
29632963
"kindModifiers": "declare",
2964-
"sortText": "1",
2964+
"sortText": "11",
29652965
"displayParts": [
29662966
{
29672967
"text": "(",
@@ -3327,7 +3327,7 @@
33273327
"name": "forEach",
33283328
"kind": "method",
33293329
"kindModifiers": "declare",
3330-
"sortText": "1",
3330+
"sortText": "11",
33313331
"displayParts": [
33323332
{
33333333
"text": "(",
@@ -3693,7 +3693,7 @@
36933693
"name": "map",
36943694
"kind": "method",
36953695
"kindModifiers": "declare",
3696-
"sortText": "1",
3696+
"sortText": "11",
36973697
"displayParts": [
36983698
{
36993699
"text": "(",
@@ -4079,7 +4079,7 @@
40794079
"name": "filter",
40804080
"kind": "property",
40814081
"kindModifiers": "declare",
4082-
"sortText": "1",
4082+
"sortText": "11",
40834083
"displayParts": [
40844084
{
40854085
"text": "(",
@@ -4659,7 +4659,7 @@
46594659
"name": "reduce",
46604660
"kind": "property",
46614661
"kindModifiers": "declare",
4662-
"sortText": "1",
4662+
"sortText": "11",
46634663
"displayParts": [
46644664
{
46654665
"text": "(",
@@ -5395,7 +5395,7 @@
53955395
"name": "reduceRight",
53965396
"kind": "property",
53975397
"kindModifiers": "declare",
5398-
"sortText": "1",
5398+
"sortText": "11",
53995399
"displayParts": [
54005400
{
54015401
"text": "(",

0 commit comments

Comments
 (0)