/
declaration.ts
729 lines (664 loc) · 22.4 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
import type { Theme as ShikiTheme } from "shiki";
import type { LogLevel } from "../loggers";
import type { SortStrategy } from "../sort";
import { isAbsolute, join, resolve } from "path";
import type { EntryPointStrategy } from "../entry-point";
import type { ReflectionKind } from "../../models/reflections/kind";
/** @enum */
export const EmitStrategy = {
both: "both", // Emit both documentation and JS
docs: "docs", // Emit documentation, but not JS (default)
none: "none", // Emit nothing, just convert and run validation
} as const;
/** @hidden */
export type EmitStrategy = typeof EmitStrategy[keyof typeof EmitStrategy];
/**
* Determines how TypeDoc searches for comments.
* @enum
*/
export const CommentStyle = {
JSDoc: "jsdoc",
Block: "block",
Line: "line",
All: "all",
} as const;
export type CommentStyle = typeof CommentStyle[keyof typeof CommentStyle];
/**
* 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]: unknown extends TypeDocOptionMap[K]
? unknown
: TypeDocOptionMap[K] extends ManuallyValidatedOption<
infer ManuallyValidated
>
? ManuallyValidated
: TypeDocOptionMap[K] extends string | string[] | number | boolean
? TypeDocOptionMap[K]
: TypeDocOptionMap[K] extends Record<string, boolean>
? Partial<TypeDocOptionMap[K]> | boolean
:
| keyof TypeDocOptionMap[K]
| TypeDocOptionMap[K][keyof 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, and does not allow partials of flag values.
*/
export type TypeDocOptionValues = {
[K in keyof TypeDocOptionMap]: unknown extends TypeDocOptionMap[K]
? unknown
: TypeDocOptionMap[K] extends ManuallyValidatedOption<
infer ManuallyValidated
>
? ManuallyValidated
: TypeDocOptionMap[K] extends
| string
| string[]
| number
| boolean
| Record<string, boolean>
? TypeDocOptionMap[K]
: TypeDocOptionMap[K][keyof TypeDocOptionMap[K]];
};
/**
* Describes all TypeDoc options. Used internally to provide better types when fetching options.
* External consumers should likely use {@link TypeDocOptions} instead.
*/
export interface TypeDocOptionMap {
options: string;
tsconfig: string;
entryPoints: string[];
entryPointStrategy: typeof EntryPointStrategy;
exclude: string[];
externalPattern: string[];
excludeExternals: boolean;
excludePrivate: boolean;
excludeProtected: boolean;
excludeNotDocumented: boolean;
excludeInternal: boolean;
disableSources: boolean;
basePath: string;
includes: string;
media: string;
emit: typeof EmitStrategy;
watch: boolean;
preserveWatchOutput: boolean;
out: string;
json: string;
pretty: boolean;
theme: string;
lightHighlightTheme: ShikiTheme;
darkHighlightTheme: ShikiTheme;
customCss: string;
visibilityFilters: ManuallyValidatedOption<{
protected?: boolean;
private?: boolean;
inherited?: boolean;
external?: boolean;
[tag: `@${string}`]: boolean;
}>;
name: string;
includeVersion: boolean;
readme: string;
defaultCategory: string;
categoryOrder: string[];
categorizeByGroup: boolean;
cname: string;
sort: SortStrategy[];
gitRevision: string;
gitRemote: string;
gaID: string;
githubPages: boolean;
htmlLang: string;
hideGenerator: boolean;
searchInComments: boolean;
cleanOutputDir: boolean;
commentStyle: typeof CommentStyle;
excludeTags: `@${string}`[];
blockTags: `@${string}`[];
inlineTags: `@${string}`[];
modifierTags: `@${string}`[];
help: boolean;
version: boolean;
showConfig: boolean;
plugin: string[];
searchCategoryBoosts: ManuallyValidatedOption<Record<string, number>>;
searchGroupBoosts: ManuallyValidatedOption<Record<string, number>>;
logger: unknown; // string | Function
logLevel: typeof LogLevel;
markedOptions: unknown;
compilerOptions: unknown;
// Validation
treatWarningsAsErrors: boolean;
intentionallyNotExported: string[];
validation: ValidationOptions;
requiredToBeDocumented: (keyof typeof ReflectionKind)[];
}
/**
* Wrapper type for values in TypeDocOptionMap which are represented with an unknown option type, but
* have a validation function that checks that they are the given type.
*/
export type ManuallyValidatedOption<T> = { __validated: T };
export type ValidationOptions = {
/**
* If set, TypeDoc will produce warnings when a symbol is referenced by the documentation,
* but is not included in the documentation.
*/
notExported: boolean;
/**
* If set, TypeDoc will produce warnings about \{&link\} tags which will produce broken links.
*/
invalidLink: boolean;
/**
* If set, TypeDoc will produce warnings about declarations that do not have doc comments
*/
notDocumented: boolean;
};
/**
* 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 ManuallyValidatedOption<unknown>
? MixedDeclarationOption & { validate(value: unknown): void }
: TypeDocOptionMap[K] extends Record<string, boolean>
? FlagsDeclarationOption<TypeDocOptionMap[K]>
: TypeDocOptionMap[K] extends Record<string | number, infer U>
? MapDeclarationOption<U>
: never;
export enum ParameterHint {
File,
Directory,
}
export enum ParameterType {
String,
/**
* Resolved according to the config directory.
*/
Path,
Number,
Boolean,
Map,
Mixed,
Array,
/**
* Resolved according to the config directory.
*/
PathArray,
/**
* Resolved according to the config directory if it starts with `.`
*/
ModuleArray,
/**
* Resolved according to the config directory unless it starts with `**`, after skipping any leading `!` and `#` characters.
*/
GlobArray,
/**
* An object with true/false flags
*/
Flags,
}
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 {
/**
* Specifies the resolution strategy. If `Path` is provided, values will be resolved according to their
* location in a file. If `String` or no value is provided, values will not be resolved.
*/
type?: ParameterType.String | ParameterType.Path;
/**
* If not specified defaults to the empty string for both `String` and `Path`.
*/
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
| ParameterType.PathArray
| ParameterType.ModuleArray
| ParameterType.GlobArray;
/**
* If not specified defaults to an empty array.
*/
defaultValue?: readonly 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 interface FlagsDeclarationOption<T extends Record<string, boolean>>
extends DeclarationOptionBase {
type: ParameterType.Flags;
/**
* All of the possible flags, with their default values set.
*/
defaults: T;
}
export type DeclarationOption =
| StringDeclarationOption
| NumberDeclarationOption
| BooleanDeclarationOption
| MixedDeclarationOption
| MapDeclarationOption<unknown>
| ArrayDeclarationOption
| FlagsDeclarationOption<Record<string, boolean>>;
export interface ParameterTypeToOptionTypeMap {
[ParameterType.String]: string;
[ParameterType.Path]: string;
[ParameterType.Number]: number;
[ParameterType.Boolean]: boolean;
[ParameterType.Mixed]: unknown;
[ParameterType.Array]: string[];
[ParameterType.PathArray]: string[];
[ParameterType.ModuleArray]: string[];
[ParameterType.GlobArray]: string[];
[ParameterType.Flags]: Record<string, boolean>;
// Special.. avoid this if possible.
[ParameterType.Map]: unknown;
}
export type DeclarationOptionToOptionType<T extends DeclarationOption> =
T extends MapDeclarationOption<infer U>
? U
: T extends FlagsDeclarationOption<infer U>
? U
: ParameterTypeToOptionTypeMap[Exclude<T["type"], undefined>];
const converters: {
[K in ParameterType]: (
value: unknown,
option: DeclarationOption & { type: K },
configPath: string
) => ParameterTypeToOptionTypeMap[K];
} = {
[ParameterType.String](value, option) {
const stringValue = value == null ? "" : String(value);
option.validate?.(stringValue);
return stringValue;
},
[ParameterType.Path](value, option, configPath) {
const stringValue =
value == null ? "" : resolve(configPath, String(value));
option.validate?.(stringValue);
return stringValue;
},
[ParameterType.Number](value, option) {
const numValue = parseInt(String(value), 10) || 0;
if (!valueIsWithinBounds(numValue, option.minValue, option.maxValue)) {
throw new Error(
getBoundsError(option.name, option.minValue, option.maxValue)
);
}
option.validate?.(numValue);
return numValue;
},
[ParameterType.Boolean](value) {
return !!value;
},
[ParameterType.Array](value, option) {
let strArrValue = new Array<string>();
if (Array.isArray(value)) {
strArrValue = value.map(String);
} else if (typeof value === "string") {
strArrValue = [value];
}
option.validate?.(strArrValue);
return strArrValue;
},
[ParameterType.PathArray](value, option, configPath) {
let strArrValue = new Array<string>();
if (Array.isArray(value)) {
strArrValue = value.map(String);
} else if (typeof value === "string") {
strArrValue = [value];
}
strArrValue = strArrValue.map((path) => resolve(configPath, path));
option.validate?.(strArrValue);
return strArrValue;
},
[ParameterType.ModuleArray](value, option, configPath) {
let strArrValue = new Array<string>();
if (Array.isArray(value)) {
strArrValue = value.map(String);
} else if (typeof value === "string") {
strArrValue = [value];
}
strArrValue = resolveModulePaths(strArrValue, configPath);
option.validate?.(strArrValue);
return strArrValue;
},
[ParameterType.GlobArray](value, option, configPath) {
let strArrValue = new Array<string>();
if (Array.isArray(value)) {
strArrValue = value.map(String);
} else if (typeof value === "string") {
strArrValue = [value];
}
strArrValue = resolveGlobPaths(strArrValue, configPath);
option.validate?.(strArrValue);
return strArrValue;
},
[ParameterType.Map](value, option) {
const key = String(value);
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) {
if (isTsNumericEnum(option.map) && typeof value === "number") {
return value;
}
return option.map[key];
} else if (Object.values(option.map).includes(value)) {
return value;
}
throw new Error(
option.mapError ?? getMapError(option.map, option.name)
);
},
[ParameterType.Mixed](value, option) {
option.validate?.(value);
return value;
},
[ParameterType.Flags](value, option) {
if (typeof value === "boolean") {
value = Object.fromEntries(
Object.keys(option.defaults).map((key) => [key, value])
);
}
if (typeof value !== "object" || value == null) {
throw new Error(
`Expected an object with flag values for ${option.name} or true/false`
);
}
const obj = { ...value } as Record<string, unknown>;
for (const key of Object.keys(obj)) {
if (!Object.prototype.hasOwnProperty.call(option.defaults, key)) {
throw new Error(
`The flag '${key}' is not valid for ${
option.name
}, expected one of: ${Object.keys(option.defaults).join(
", "
)}`
);
}
if (typeof obj[key] !== "boolean") {
// Explicit null/undefined, switch to default.
if (obj[key] == null) {
obj[key] = option.defaults[key];
} else {
throw new Error(
`Flag values for ${option.name} must be a boolean.`
);
}
}
}
return obj as Record<string, boolean>;
},
};
/**
* 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(
value: unknown,
option: DeclarationOption,
configPath: string
): unknown {
const _converters = converters as Record<
ParameterType,
(v: unknown, o: DeclarationOption, c: string) => unknown
>;
return _converters[option.type ?? ParameterType.String](
value,
option,
configPath
);
}
const defaultGetters: {
[K in ParameterType]: (
option: DeclarationOption & { type: K }
) => ParameterTypeToOptionTypeMap[K];
} = {
[ParameterType.String](option) {
return option.defaultValue ?? "";
},
[ParameterType.Path](option) {
const defaultStr = option.defaultValue ?? "";
if (defaultStr == "") {
return "";
}
return isAbsolute(defaultStr)
? defaultStr
: join(process.cwd(), defaultStr);
},
[ParameterType.Number](option) {
return option.defaultValue ?? 0;
},
[ParameterType.Boolean](option) {
return option.defaultValue ?? false;
},
[ParameterType.Map](option) {
return option.defaultValue;
},
[ParameterType.Mixed](option) {
return option.defaultValue;
},
[ParameterType.Array](option) {
return option.defaultValue?.slice() ?? [];
},
[ParameterType.PathArray](option) {
return (
option.defaultValue?.map((value) =>
resolve(process.cwd(), value)
) ?? []
);
},
[ParameterType.ModuleArray](option) {
return (
option.defaultValue?.map((value) =>
value.startsWith(".") ? resolve(process.cwd(), value) : value
) ?? []
);
},
[ParameterType.GlobArray](option) {
return resolveGlobPaths(option.defaultValue ?? [], process.cwd());
},
[ParameterType.Flags](option) {
return { ...option.defaults };
},
};
export function getDefaultValue(option: DeclarationOption) {
const getters = defaultGetters as Record<
ParameterType,
(o: DeclarationOption) => unknown
>;
return getters[option.type ?? ParameterType.String](option);
}
function resolveGlobPaths(globs: readonly string[], configPath: string) {
return globs.map((path) => {
const start = path.match(/^[!#]+/)?.[0] ?? "";
const remaining = path.substring(start.length);
if (!remaining.startsWith("**")) {
return start + resolve(configPath, remaining);
}
return start + remaining;
});
}
function resolveModulePaths(modules: readonly string[], configPath: string) {
return modules.map((path) => {
if (path.startsWith(".")) {
return resolve(configPath, path);
}
return path;
});
}
function isTsNumericEnum(map: Record<string, any>) {
return Object.values(map).every((key) => map[map[key]] === key);
}
/**
* 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);
// 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) && isTsNumericEnum(map)) {
// 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 {
return `${name} must be <= ${maxValue}`;
}
}
/**
* 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;
}
}