66import { ReflectionKind } from "../models/reflections/kind" ;
77import type { DeclarationReflection } from "../models/reflections/declaration" ;
88import { LiteralType } from "../models/types" ;
9+ import type { Options } from "./options" ;
910
1011export const SORT_STRATEGIES = [
1112 "source-order" ,
@@ -21,10 +22,43 @@ export const SORT_STRATEGIES = [
2122
2223export type SortStrategy = typeof SORT_STRATEGIES [ number ] ;
2324
25+ const defaultKindSortOrder = [
26+ ReflectionKind . Reference ,
27+ ReflectionKind . Project ,
28+ ReflectionKind . Module ,
29+ ReflectionKind . Namespace ,
30+ ReflectionKind . Enum ,
31+ ReflectionKind . EnumMember ,
32+ ReflectionKind . Class ,
33+ ReflectionKind . Interface ,
34+ ReflectionKind . TypeAlias ,
35+
36+ ReflectionKind . Constructor ,
37+ ReflectionKind . Property ,
38+ ReflectionKind . Variable ,
39+ ReflectionKind . Function ,
40+ ReflectionKind . Accessor ,
41+ ReflectionKind . Method ,
42+ ReflectionKind . ObjectLiteral ,
43+
44+ ReflectionKind . Parameter ,
45+ ReflectionKind . TypeParameter ,
46+ ReflectionKind . TypeLiteral ,
47+ ReflectionKind . CallSignature ,
48+ ReflectionKind . ConstructorSignature ,
49+ ReflectionKind . IndexSignature ,
50+ ReflectionKind . GetSignature ,
51+ ReflectionKind . SetSignature ,
52+ ] as const ;
53+
2454// Return true if a < b
2555const sorts : Record <
2656 SortStrategy ,
27- ( a : DeclarationReflection , b : DeclarationReflection ) => boolean
57+ (
58+ a : DeclarationReflection ,
59+ b : DeclarationReflection ,
60+ data : { kindSortOrder : ReflectionKind [ ] }
61+ ) => boolean
2862> = {
2963 "source-order" ( a , b ) {
3064 const aSymbol = a . project . getSymbolFromReflection ( a ) ;
@@ -107,53 +141,36 @@ const sorts: Record<
107141 "required-first" ( a , b ) {
108142 return ! a . flags . isOptional && b . flags . isOptional ;
109143 } ,
110- kind ( a , b ) {
111- const weights = [
112- ReflectionKind . Reference ,
113- ReflectionKind . Project ,
114- ReflectionKind . Module ,
115- ReflectionKind . Namespace ,
116- ReflectionKind . Enum ,
117- ReflectionKind . EnumMember ,
118- ReflectionKind . Class ,
119- ReflectionKind . Interface ,
120- ReflectionKind . TypeAlias ,
121-
122- ReflectionKind . Constructor ,
123- ReflectionKind . Property ,
124- ReflectionKind . Variable ,
125- ReflectionKind . Function ,
126- ReflectionKind . Accessor ,
127- ReflectionKind . Method ,
128- ReflectionKind . ObjectLiteral ,
129-
130- ReflectionKind . Parameter ,
131- ReflectionKind . TypeParameter ,
132- ReflectionKind . TypeLiteral ,
133- ReflectionKind . CallSignature ,
134- ReflectionKind . ConstructorSignature ,
135- ReflectionKind . IndexSignature ,
136- ReflectionKind . GetSignature ,
137- ReflectionKind . SetSignature ,
138- ] as const ;
139-
140- return weights . indexOf ( a . kind ) < weights . indexOf ( b . kind ) ;
144+ kind ( a , b , { kindSortOrder } ) {
145+ return kindSortOrder . indexOf ( a . kind ) < kindSortOrder . indexOf ( b . kind ) ;
141146 } ,
142147} ;
143148
144- export function sortReflections (
145- reflections : DeclarationReflection [ ] ,
146- strategies : readonly SortStrategy [ ]
147- ) {
148- reflections . sort ( ( a , b ) => {
149- for ( const s of strategies ) {
150- if ( sorts [ s ] ( a , b ) ) {
151- return - 1 ;
152- }
153- if ( sorts [ s ] ( b , a ) ) {
154- return 1 ;
155- }
149+ export function getSortFunction ( opts : Options ) {
150+ const kindSortOrder = opts
151+ . getValue ( "kindSortOrder" )
152+ . map ( ( k ) => ReflectionKind [ k ] ) ;
153+
154+ for ( const kind of defaultKindSortOrder ) {
155+ if ( ! kindSortOrder . includes ( kind ) ) {
156+ kindSortOrder . push ( kind ) ;
156157 }
157- return 0 ;
158- } ) ;
158+ }
159+
160+ const strategies = opts . getValue ( "sort" ) ;
161+ const data = { kindSortOrder } ;
162+
163+ return function sortReflections ( reflections : DeclarationReflection [ ] ) {
164+ reflections . sort ( ( a , b ) => {
165+ for ( const s of strategies ) {
166+ if ( sorts [ s ] ( a , b , data ) ) {
167+ return - 1 ;
168+ }
169+ if ( sorts [ s ] ( b , a , data ) ) {
170+ return 1 ;
171+ }
172+ }
173+ return 0 ;
174+ } ) ;
175+ } ;
159176}
0 commit comments