-
Notifications
You must be signed in to change notification settings - Fork 676
/
sort.ts
160 lines (146 loc) · 4.91 KB
/
sort.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Module which handles sorting reflections according to a user specified strategy.
* @module
*/
import { ReflectionKind } from "../models/reflections/kind";
import type { DeclarationReflection } from "../models/reflections/declaration";
import { LiteralType } from "../models";
export const SORT_STRATEGIES = [
"source-order",
"alphabetical",
"enum-value-ascending",
"enum-value-descending",
"static-first",
"instance-first",
"visibility",
"required-first",
"kind",
] as const;
export type SortStrategy = typeof SORT_STRATEGIES[number];
// Return true if a < b
const sorts: Record<
SortStrategy,
(a: DeclarationReflection, b: DeclarationReflection) => boolean
> = {
"source-order"(a, b) {
const aSymbol = a.project.getSymbolFromReflection(a);
const bSymbol = b.project.getSymbolFromReflection(b);
// This is going to be somewhat ambiguous. No way around that. Treat the first
// declaration of a symbol as its ordering declaration.
const aDecl = aSymbol?.getDeclarations()?.[0];
const bDecl = bSymbol?.getDeclarations()?.[0];
if (aDecl && bDecl) {
const aFile = aDecl.getSourceFile().fileName;
const bFile = bDecl.getSourceFile().fileName;
if (aFile < bFile) {
return true;
}
if (aFile == bFile && aDecl.pos < bDecl.pos) {
return true;
}
return false;
}
// Someone is doing something weird. Fail to re-order. This *might* be a bug in TD
// but it could also be TS having some exported symbol without a declaration.
return false;
},
alphabetical(a, b) {
return a.name < b.name;
},
"enum-value-ascending"(a, b) {
if (
a.kind == ReflectionKind.EnumMember &&
b.kind == ReflectionKind.EnumMember
) {
const aValue =
a.type instanceof LiteralType ? a.type.value : -Infinity;
const bValue =
b.type instanceof LiteralType ? b.type.value : -Infinity;
return aValue! < bValue!;
}
return false;
},
"enum-value-descending"(a, b) {
if (
a.kind == ReflectionKind.EnumMember &&
b.kind == ReflectionKind.EnumMember
) {
const aValue =
a.type instanceof LiteralType ? a.type.value : -Infinity;
const bValue =
b.type instanceof LiteralType ? b.type.value : -Infinity;
return bValue! < aValue!;
}
return false;
},
"static-first"(a, b) {
return a.flags.isStatic && !b.flags.isStatic;
},
"instance-first"(a, b) {
return !a.flags.isStatic && b.flags.isStatic;
},
visibility(a, b) {
// Note: flags.isPublic may not be set on public members. It will only be set
// if the user explicitly marks members as public. Therefore, we can't use it
// here to get a reliable sort order.
if (a.flags.isPrivate) {
return false; // Not sorted before anything
}
if (a.flags.isProtected) {
return b.flags.isPrivate; // Sorted before privates
}
if (b.flags.isPrivate || b.flags.isProtected) {
return true; // We are public, sort before b if b is less visible
}
return false;
},
"required-first"(a, b) {
return !a.flags.isOptional && b.flags.isOptional;
},
kind(a, b) {
const weights = [
ReflectionKind.Reference,
ReflectionKind.Project,
ReflectionKind.Module,
ReflectionKind.Namespace,
ReflectionKind.Enum,
ReflectionKind.EnumMember,
ReflectionKind.Class,
ReflectionKind.Interface,
ReflectionKind.TypeAlias,
ReflectionKind.Constructor,
ReflectionKind.Event,
ReflectionKind.Property,
ReflectionKind.Variable,
ReflectionKind.Function,
ReflectionKind.Accessor,
ReflectionKind.Method,
ReflectionKind.ObjectLiteral,
ReflectionKind.Parameter,
ReflectionKind.TypeParameter,
ReflectionKind.TypeLiteral,
ReflectionKind.CallSignature,
ReflectionKind.ConstructorSignature,
ReflectionKind.IndexSignature,
ReflectionKind.GetSignature,
ReflectionKind.SetSignature,
] as const;
return weights.indexOf(a.kind) < weights.indexOf(b.kind);
},
};
export function sortReflections(
reflections: DeclarationReflection[],
strategies: readonly SortStrategy[]
) {
reflections.sort((a, b) => {
for (const s of strategies) {
if (sorts[s](a, b)) {
return -1;
}
if (sorts[s](b, a)) {
return 1;
}
}
return 0;
});
}