@@ -10,10 +10,12 @@ import {
10
10
type NeverIfInternal ,
11
11
type NormalizedPath ,
12
12
type NormalizedPathOrModule ,
13
+ type NormalizedPathOrModuleOrFunction ,
13
14
type TranslatedString ,
14
15
} from "#utils" ;
15
16
import type { TranslationProxy } from "../../internationalization/internationalization.js" ;
16
17
import { createGlobString , normalizePath } from "../paths.js" ;
18
+ import type { Application } from "../../application.js" ;
17
19
18
20
/** @enum */
19
21
export const EmitStrategy = {
@@ -122,7 +124,8 @@ export type TypeDocOptions = {
122
124
TypeDocOptionMap [ K ] extends ManuallyValidatedOption <
123
125
infer ManuallyValidated
124
126
> ? ManuallyValidated :
125
- TypeDocOptionMap [ K ] extends NormalizedPath [ ] | NormalizedPathOrModule [ ] | GlobString [ ] ? string [ ] :
127
+ TypeDocOptionMap [ K ] extends
128
+ NormalizedPath [ ] | NormalizedPathOrModule [ ] | NormalizedPathOrModuleOrFunction [ ] | GlobString [ ] ? string [ ] :
126
129
TypeDocOptionMap [ K ] extends NormalizedPath ? string :
127
130
TypeDocOptionMap [ K ] extends
128
131
| string
@@ -150,6 +153,8 @@ export type TypeDocOptionValues = {
150
153
| string
151
154
| string [ ]
152
155
| GlobString [ ]
156
+ | NormalizedPathOrModule [ ]
157
+ | NormalizedPathOrModuleOrFunction [ ]
153
158
| number
154
159
| boolean
155
160
| Record < string , boolean > ? TypeDocOptionMap [ K ] :
@@ -183,7 +188,7 @@ export interface TypeDocOptionMap {
183
188
options : NormalizedPath ;
184
189
tsconfig : NormalizedPath ;
185
190
compilerOptions : unknown ;
186
- plugin : NormalizedPathOrModule [ ] ;
191
+ plugin : NormalizedPathOrModuleOrFunction [ ] ;
187
192
lang : string ;
188
193
locales : ManuallyValidatedOption < Record < string , Record < string , string > > > ;
189
194
packageOptions : ManuallyValidatedOption <
@@ -404,7 +409,9 @@ export type KeyToDeclaration<K extends keyof TypeDocOptionMap> = TypeDocOptionMa
404
409
TypeDocOptionMap [ K ] extends string | NormalizedPath ? StringDeclarationOption :
405
410
TypeDocOptionMap [ K ] extends number ? NumberDeclarationOption :
406
411
TypeDocOptionMap [ K ] extends GlobString [ ] ? GlobArrayDeclarationOption :
407
- TypeDocOptionMap [ K ] extends string [ ] | NormalizedPath [ ] | NormalizedPathOrModule [ ] ? ArrayDeclarationOption :
412
+ TypeDocOptionMap [ K ] extends
413
+ string [ ] | NormalizedPath [ ] | NormalizedPathOrModule [ ] | NormalizedPathOrModuleOrFunction [ ] ?
414
+ ArrayDeclarationOption :
408
415
unknown extends TypeDocOptionMap [ K ] ? MixedDeclarationOption | ObjectDeclarationOption :
409
416
TypeDocOptionMap [ K ] extends ManuallyValidatedOption < unknown > ?
410
417
| ( MixedDeclarationOption & {
@@ -452,8 +459,14 @@ export enum ParameterType {
452
459
PathArray ,
453
460
/**
454
461
* Resolved according to the config directory if it starts with `.`
462
+ * @deprecated since 0.28.8, will be removed in 0.29
455
463
*/
456
464
ModuleArray ,
465
+ /**
466
+ * Resolved according to the config directory if it starts with `.`
467
+ * @internal - only intended for use with the plugin option
468
+ */
469
+ PluginArray ,
457
470
/**
458
471
* Relative to the config directory.
459
472
*/
@@ -567,7 +580,8 @@ export interface ArrayDeclarationOption extends DeclarationOptionBase {
567
580
type :
568
581
| ParameterType . Array
569
582
| ParameterType . PathArray
570
- | ParameterType . ModuleArray ;
583
+ | ParameterType . ModuleArray
584
+ | ParameterType . PluginArray ;
571
585
572
586
/**
573
587
* If not specified defaults to an empty array.
@@ -674,6 +688,7 @@ export interface ParameterTypeToOptionTypeMap {
674
688
[ ParameterType . Array ] : string [ ] ;
675
689
[ ParameterType . PathArray ] : NormalizedPath [ ] ;
676
690
[ ParameterType . ModuleArray ] : NormalizedPathOrModule [ ] ;
691
+ [ ParameterType . PluginArray ] : Array < NormalizedPathOrModule | ( ( app : Application ) => void | Promise < void > ) > ;
677
692
[ ParameterType . GlobArray ] : GlobString [ ] ;
678
693
[ ParameterType . Flags ] : Record < string , boolean > ;
679
694
@@ -685,7 +700,7 @@ export type DeclarationOptionToOptionType<T extends DeclarationOption> = T exten
685
700
T extends FlagsDeclarationOption < infer U > ? U :
686
701
ParameterTypeToOptionTypeMap [ Exclude < T [ "type" ] , undefined > ] ;
687
702
688
- function toStringArray ( value : unknown , option : DeclarationOption ) {
703
+ function toStringArray ( value : unknown , option : DeclarationOption ) : string [ ] {
689
704
if ( Array . isArray ( value ) && value . every ( v => typeof v === "string" ) ) {
690
705
return value ;
691
706
} else if ( typeof value === "string" ) {
@@ -695,6 +710,19 @@ function toStringArray(value: unknown, option: DeclarationOption) {
695
710
throw new Error ( i18n . option_0_must_be_an_array_of_string ( option . name ) ) ;
696
711
}
697
712
713
+ function toStringOrFunctionArray (
714
+ value : unknown ,
715
+ option : DeclarationOption ,
716
+ ) : Array < string | ( ( app : Application ) => void | Promise < void > ) > {
717
+ if ( Array . isArray ( value ) && value . every ( v => typeof v === "string" || typeof v === "function" ) ) {
718
+ return value ;
719
+ } else if ( typeof value === "string" ) {
720
+ return [ value ] ;
721
+ }
722
+
723
+ throw new Error ( i18n . option_0_must_be_an_array_of_string_or_functions ( option . name ) ) ;
724
+ }
725
+
698
726
const converters : {
699
727
[ K in ParameterType ] : (
700
728
value : unknown ,
@@ -763,6 +791,13 @@ const converters: {
763
791
option . validate ?.( resolved ) ;
764
792
return resolved ;
765
793
} ,
794
+ [ ParameterType . PluginArray ] ( value , option , configPath ) {
795
+ const arrayValue = toStringOrFunctionArray ( value , option ) ;
796
+ const resolved = arrayValue . map ( plugin =>
797
+ typeof plugin === "function" ? plugin : resolveModulePath ( plugin , configPath )
798
+ ) ;
799
+ return resolved ;
800
+ } ,
766
801
[ ParameterType . GlobArray ] ( value , option , configPath ) {
767
802
const toGlobString = ( v : unknown ) => {
768
803
const s = String ( v ) ;
@@ -942,6 +977,12 @@ const defaultGetters: {
942
977
}
943
978
return [ ] ;
944
979
} ,
980
+ [ ParameterType . PluginArray ] ( option ) {
981
+ if ( option . defaultValue ) {
982
+ return resolveModulePaths ( option . defaultValue , process . cwd ( ) ) ;
983
+ }
984
+ return [ ] ;
985
+ } ,
945
986
[ ParameterType . GlobArray ] ( option ) {
946
987
return ( option . defaultValue ?? [ ] ) . map ( g => createGlobString ( normalizePath ( process . cwd ( ) ) , g ) ) ;
947
988
} ,
@@ -959,12 +1000,14 @@ export function getDefaultValue(option: DeclarationOption) {
959
1000
}
960
1001
961
1002
function resolveModulePaths ( modules : readonly string [ ] , configPath : string ) : NormalizedPathOrModule [ ] {
962
- return modules . map ( ( path ) => {
963
- if ( path . startsWith ( "." ) ) {
964
- return normalizePath ( resolve ( configPath , path ) ) ;
965
- }
966
- return normalizePath ( path ) ;
967
- } ) ;
1003
+ return modules . map ( path => resolveModulePath ( path , configPath ) ) ;
1004
+ }
1005
+
1006
+ function resolveModulePath ( path : string , configPath : string ) : NormalizedPathOrModule {
1007
+ if ( path . startsWith ( "." ) ) {
1008
+ return normalizePath ( resolve ( configPath , path ) ) ;
1009
+ }
1010
+ return normalizePath ( path ) ;
968
1011
}
969
1012
970
1013
function isTsNumericEnum ( map : Record < string , any > ) {
0 commit comments