/
sort.ts
187 lines (171 loc) · 5.47 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* 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/types";
import type { Options } from "./options";
export const SORT_STRATEGIES = [
"source-order",
"alphabetical",
"enum-value-ascending",
"enum-value-descending",
"enum-member-source-order",
"static-first",
"instance-first",
"visibility",
"required-first",
"kind",
"external-last",
] as const;
export type SortStrategy = (typeof SORT_STRATEGIES)[number];
const defaultKindSortOrder = [
ReflectionKind.Reference,
ReflectionKind.Project,
ReflectionKind.Module,
ReflectionKind.Namespace,
ReflectionKind.Enum,
ReflectionKind.EnumMember,
ReflectionKind.Class,
ReflectionKind.Interface,
ReflectionKind.TypeAlias,
ReflectionKind.Constructor,
ReflectionKind.Property,
ReflectionKind.Variable,
ReflectionKind.Function,
ReflectionKind.Accessor,
ReflectionKind.Method,
ReflectionKind.Parameter,
ReflectionKind.TypeParameter,
ReflectionKind.TypeLiteral,
ReflectionKind.CallSignature,
ReflectionKind.ConstructorSignature,
ReflectionKind.IndexSignature,
ReflectionKind.GetSignature,
ReflectionKind.SetSignature,
] as const;
// Return true if a < b
const sorts: Record<
SortStrategy,
(
a: DeclarationReflection,
b: DeclarationReflection,
data: { kindSortOrder: ReflectionKind[] },
) => boolean
> = {
"source-order"(a, b) {
// This is going to be somewhat ambiguous. No way around that. Treat the first
// declaration of a symbol as its ordering declaration.
const aSymbol = a.project.getSymbolIdFromReflection(a);
const bSymbol = b.project.getSymbolIdFromReflection(b);
if (aSymbol && bSymbol) {
if (aSymbol.fileName < bSymbol.fileName) {
return true;
}
if (
aSymbol.fileName === bSymbol.fileName &&
aSymbol.pos < bSymbol.pos
) {
return true;
}
return false;
}
// Someone is doing something weird. Fail to re-order. This could happen if someone
// tries to sort with a reflection that has been removed from the project.
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;
},
"enum-member-source-order"(a, b, data) {
if (
a.kind === ReflectionKind.EnumMember &&
b.kind === ReflectionKind.EnumMember
) {
return sorts["source-order"](a, b, data);
}
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, { kindSortOrder }) {
return kindSortOrder.indexOf(a.kind) < kindSortOrder.indexOf(b.kind);
},
"external-last"(a, b) {
return !a.flags.isExternal && b.flags.isExternal;
},
};
export function getSortFunction(opts: Options) {
const kindSortOrder = opts
.getValue("kindSortOrder")
.map((k) => ReflectionKind[k]);
for (const kind of defaultKindSortOrder) {
if (!kindSortOrder.includes(kind)) {
kindSortOrder.push(kind);
}
}
const strategies = opts.getValue("sort");
const data = { kindSortOrder };
return function sortReflections(reflections: DeclarationReflection[]) {
reflections.sort((a, b) => {
for (const s of strategies) {
if (sorts[s](a, b, data)) {
return -1;
}
if (sorts[s](b, a, data)) {
return 1;
}
}
return 0;
});
};
}