/
declaration.ts
431 lines (384 loc) · 12.5 KB
/
declaration.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import { Theme as ShikiTheme } from "shiki";
import { LogLevel } from "../loggers";
/**
* An interface describing all TypeDoc specific options. Generated from a
* map which contains more information about each option for better types when
* defining said options.
*/
export type TypeDocOptions = {
[K in keyof TypeDocOptionMap]: TypeDocOptionMap[K] extends Record<
string,
infer U
>
? Exclude<U, string> | keyof TypeDocOptionMap[K]
: TypeDocOptionMap[K];
};
/**
* Describes all TypeDoc specific options as returned by {@link Options.getValue}, this is
* slightly more restrictive than the {@link TypeDocOptions} since it does not allow both
* keys and values for mapped option types.
*/
export type TypeDocOptionValues = {
[K in keyof TypeDocOptionMap]: TypeDocOptionMap[K] extends Record<
string,
infer U
>
? Exclude<U, string>
: TypeDocOptionMap[K];
};
/**
* Describes all TypeDoc options. Used internally to provide better types when fetching options.
* External consumers should likely use [[TypeDocOptions]] instead.
*/
export interface TypeDocOptionMap {
options: string;
tsconfig: string;
entryPoints: string[];
exclude: string[];
externalPattern: string[];
excludeExternals: boolean;
excludePrivate: boolean;
excludeProtected: boolean;
excludeNotDocumented: boolean;
excludeInternal: boolean;
disableSources: boolean;
disableAliases: boolean;
includes: string;
media: string;
emit: boolean;
watch: boolean;
preserveWatchOutput: boolean;
out: string;
json: string;
pretty: boolean;
theme: string;
name: string;
includeVersion: boolean;
excludeTags: string[];
readme: string;
defaultCategory: string;
categoryOrder: string[];
categorizeByGroup: boolean;
gitRevision: string;
gitRemote: string;
gaID: string;
gaSite: string;
hideGenerator: boolean;
toc: string[];
disableOutputCheck: boolean;
help: boolean;
version: boolean;
showConfig: boolean;
plugin: string[];
logger: unknown; // string | Function
logLevel: typeof LogLevel;
listInvalidSymbolLinks: boolean;
markedOptions: unknown;
highlightTheme: ShikiTheme;
}
/**
* Converts a given TypeDoc option key to the type of the declaration expected.
*/
export type KeyToDeclaration<
K extends keyof TypeDocOptionMap
> = TypeDocOptionMap[K] extends boolean
? BooleanDeclarationOption
: TypeDocOptionMap[K] extends string
? StringDeclarationOption
: TypeDocOptionMap[K] extends number
? NumberDeclarationOption
: TypeDocOptionMap[K] extends string[]
? ArrayDeclarationOption
: unknown extends TypeDocOptionMap[K]
? MixedDeclarationOption
: TypeDocOptionMap[K] extends Record<string | number, infer U>
? MapDeclarationOption<U>
: never;
export enum ParameterHint {
File,
Directory,
}
export enum ParameterType {
String,
Number,
Boolean,
Map,
Mixed,
Array,
}
export interface DeclarationOptionBase {
/**
* The option name.
*/
name: string;
/**
* The help text to be displayed to the user when --help is passed.
*/
help: string;
/**
* The parameter type, used to convert user configuration values into the expected type.
* If not set, the type will be a string.
*/
type?: ParameterType;
}
export interface StringDeclarationOption extends DeclarationOptionBase {
type?: ParameterType.String;
/**
* If not specified defaults to the empty string.
*/
defaultValue?: string;
/**
* An optional hint for the type of input expected, will be displayed in the help output.
*/
hint?: ParameterHint;
/**
* An optional validation function that validates a potential value of this option.
* The function must throw an Error if the validation fails and should do nothing otherwise.
*/
validate?: (value: string) => void;
}
export interface NumberDeclarationOption extends DeclarationOptionBase {
type: ParameterType.Number;
/**
* Lowest possible value.
*/
minValue?: number;
/**
* Highest possible value.
*/
maxValue?: number;
/**
* If not specified defaults to 0.
*/
defaultValue?: number;
/**
* An optional validation function that validates a potential value of this option.
* The function must throw an Error if the validation fails and should do nothing otherwise.
*/
validate?: (value: number) => void;
}
export interface BooleanDeclarationOption extends DeclarationOptionBase {
type: ParameterType.Boolean;
/**
* If not specified defaults to false.
*/
defaultValue?: boolean;
}
export interface ArrayDeclarationOption extends DeclarationOptionBase {
type: ParameterType.Array;
/**
* If not specified defaults to an empty array.
*/
defaultValue?: string[];
/**
* An optional validation function that validates a potential value of this option.
* The function must throw an Error if the validation fails and should do nothing otherwise.
*/
validate?: (value: string[]) => void;
}
export interface MixedDeclarationOption extends DeclarationOptionBase {
type: ParameterType.Mixed;
/**
* If not specified defaults to undefined.
*/
defaultValue?: unknown;
/**
* An optional validation function that validates a potential value of this option.
* The function must throw an Error if the validation fails and should do nothing otherwise.
*/
validate?: (value: unknown) => void;
}
export interface MapDeclarationOption<T> extends DeclarationOptionBase {
type: ParameterType.Map;
/**
* Maps a given value to the option type. The map type may be a TypeScript enum.
* In that case, when generating an error message for a mismatched key, the numeric
* keys will not be listed.
*/
map: Map<string, T> | Record<string | number, T>;
/**
* Unlike the rest of the option types, there is no sensible generic default for mapped option types.
* The default value for a mapped type must be specified.
*/
defaultValue: T;
/**
* Optional override for the error reported when an invalid key is provided.
*/
mapError?: string;
}
export type DeclarationOption =
| StringDeclarationOption
| NumberDeclarationOption
| BooleanDeclarationOption
| MixedDeclarationOption
| MapDeclarationOption<unknown>
| ArrayDeclarationOption;
export type DeclarationOptionToOptionType<
T extends DeclarationOption
> = T extends StringDeclarationOption
? string
: T extends NumberDeclarationOption
? number
: T extends BooleanDeclarationOption
? boolean
: T extends MixedDeclarationOption
? unknown
: T extends MapDeclarationOption<infer U>
? U
: T extends ArrayDeclarationOption
? string[]
: never;
/**
* The default conversion function used by the Options container. Readers may
* re-use this conversion function or implement their own. The arguments reader
* implements its own since 'false' should not be converted to true for a boolean option.
* @param value The value to convert.
* @param option The option for which the value should be converted.
* @returns The result of the conversion. Might be the value or an error.
*/
export function convert<T extends DeclarationOption>(
value: unknown,
option: T
): DeclarationOptionToOptionType<T>;
export function convert<T>(value: unknown, option: MapDeclarationOption<T>): T;
export function convert(value: unknown, option: DeclarationOption): unknown {
switch (option.type) {
case undefined:
case ParameterType.String: {
const stringValue = value == null ? "" : String(value);
if (option.validate) {
option.validate(stringValue);
}
return stringValue;
}
case ParameterType.Number: {
const numValue = parseInt(String(value), 10) || 0;
if (
!valueIsWithinBounds(numValue, option.minValue, option.maxValue)
) {
throw new Error(
getBoundsError(
option.name,
option.minValue,
option.maxValue
)
);
}
if (option.validate) {
option.validate(numValue);
}
return numValue;
}
case ParameterType.Boolean:
return Boolean(value);
case ParameterType.Array: {
let strArrValue = new Array<string>();
if (Array.isArray(value)) {
strArrValue = value.map(String);
} else if (typeof value === "string") {
strArrValue = value.split(",");
}
if (option.validate) {
option.validate(strArrValue);
}
return strArrValue;
}
case ParameterType.Map: {
const key = String(value).toLowerCase();
if (option.map instanceof Map) {
if (option.map.has(key)) {
return option.map.get(key);
} else if ([...option.map.values()].includes(value)) {
return value;
}
} else if (key in option.map) {
return option.map[key];
} else if (Object.values(option.map).includes(value)) {
return value;
}
throw new Error(
option.mapError ?? getMapError(option.map, option.name)
);
}
case ParameterType.Mixed:
if (option.validate) {
option.validate(value);
}
return value;
}
}
/**
* Returns an error message for a map option, indicating that a given value was not one of the values within the map.
* @param map The values for the option.
* @param name The name of the option.
* @returns The error message.
*/
function getMapError(
map: MapDeclarationOption<unknown>["map"],
name: string
): string {
let keys = map instanceof Map ? [...map.keys()] : Object.keys(map);
const getString = (key: string) =>
String(map instanceof Map ? map.get(key) : map[key]);
// If the map is a TS numeric enum we need to filter out the numeric keys.
// TS numeric enums have the property that every key maps to a value, which maps back to that key.
if (
!(map instanceof Map) &&
keys.every((key) => getString(getString(key)) === key)
) {
// This works because TS enum keys may not be numeric.
keys = keys.filter((key) => Number.isNaN(parseInt(key, 10)));
}
return `${name} must be one of ${keys.join(", ")}`;
}
/**
* Returns an error message for a value that is out of bounds of the given min and/or max values.
* @param name The name of the thing the value represents.
* @param minValue The lower bound of the range of allowed values.
* @param maxValue The upper bound of the range of allowed values.
* @returns The error message.
*/
function getBoundsError(
name: string,
minValue?: number,
maxValue?: number
): string {
if (isFiniteNumber(minValue) && isFiniteNumber(maxValue)) {
return `${name} must be between ${minValue} and ${maxValue}`;
} else if (isFiniteNumber(minValue)) {
return `${name} must be >= ${minValue}`;
} else if (isFiniteNumber(maxValue)) {
return `${name} must be <= ${maxValue}`;
}
throw new Error("Unreachable");
}
/**
* Checks if the given value is a finite number.
* @param value The value being checked.
* @returns True, if the value is a finite number, otherwise false.
*/
function isFiniteNumber(value: unknown): value is number {
return Number.isFinite(value);
}
/**
* Checks if a value is between the bounds of the given min and/or max values.
* @param value The value being checked.
* @param minValue The lower bound of the range of allowed values.
* @param maxValue The upper bound of the range of allowed values.
* @returns True, if the value is within the given bounds, otherwise false.
*/
function valueIsWithinBounds(
value: number,
minValue?: number,
maxValue?: number
): boolean {
if (isFiniteNumber(minValue) && isFiniteNumber(maxValue)) {
return minValue <= value && value <= maxValue;
} else if (isFiniteNumber(minValue)) {
return minValue <= value;
} else if (isFiniteNumber(maxValue)) {
return value <= maxValue;
} else {
return true;
}
}