@@ -3,10 +3,10 @@ import * as ts from 'typescript';
3
3
4
4
import { DeclarationOption , ParameterScope , ParameterType , convert , TypeDocOptions , KeyToDeclaration , TypeDocAndTSOptions , TypeDocOptionMap } from './declaration' ;
5
5
import { Logger } from '../loggers' ;
6
- import { Result , Ok , Err } from '../result' ;
7
6
import { insertPrioritySorted } from '../array' ;
8
7
import { addTSOptions , addTypeDocOptions } from './sources' ;
9
8
import { Application } from '../../..' ;
9
+ import { NeverIfInternal } from '..' ;
10
10
11
11
/**
12
12
* Describes an option reader that discovers user configuration and converts it to the
@@ -148,7 +148,7 @@ export class Options {
148
148
* Adds an option declaration to the container.
149
149
* @param declaration
150
150
*/
151
- addDeclaration ( declaration : Readonly < DeclarationOption > ) : void ;
151
+ addDeclaration ( declaration : NeverIfInternal < Readonly < DeclarationOption > > ) : void ;
152
152
153
153
addDeclaration ( declaration : Readonly < DeclarationOption > ) : void {
154
154
const names = [ declaration . name ] ;
@@ -172,10 +172,15 @@ export class Options {
172
172
/**
173
173
* Adds the given declarations to the container
174
174
* @param declarations
175
+ *
176
+ * @privateRemarks
177
+ * This function explicitly provides a way out of the strict typing enforced
178
+ * by {@link addDeclaration}. It should only be used with careful validation
179
+ * of the declaration map. Internally, it is only used for adding TS options
175
180
*/
176
181
addDeclarations ( declarations : readonly DeclarationOption [ ] ) : void {
177
182
for ( const decl of declarations ) {
178
- this . addDeclaration ( decl ) ;
183
+ this . addDeclaration ( decl as any ) ;
179
184
}
180
185
}
181
186
@@ -217,11 +222,11 @@ export class Options {
217
222
* Checks if the given option has a set value or if the value is the default value.
218
223
* @param name
219
224
*/
220
- isDefault ( name : keyof TypeDocAndTSOptions ) : boolean ;
221
- isDefault ( name : string ) : boolean ;
225
+ isDefault ( name : keyof TypeDocOptions ) : boolean ;
226
+ isDefault ( name : NeverIfInternal < string > ) : boolean ;
222
227
isDefault ( name : string ) : boolean {
223
228
// getValue will throw if the declaration does not exist.
224
- return this . getValue ( name ) === this . getDeclaration ( name ) ! . defaultValue ;
229
+ return this . getValue ( name as keyof TypeDocOptions ) === this . getDeclaration ( name ) ! . defaultValue ;
225
230
}
226
231
227
232
/**
@@ -236,32 +241,18 @@ export class Options {
236
241
* @param name
237
242
*/
238
243
getValue < K extends keyof TypeDocOptions > ( name : K ) : TypeDocOptions [ K ] ;
239
- getValue ( name : string ) : unknown ;
244
+ getValue ( name : NeverIfInternal < string > ) : unknown ;
240
245
getValue ( name : string ) : unknown {
241
- return this . tryGetValue ( name ) . match ( {
242
- ok : v => v ,
243
- err ( err ) { throw err ; }
244
- } ) ;
245
- }
246
-
247
- /**
248
- * Tries to get the given option key, returns an [[Ok]] result if the option has been
249
- * declared with a TypeDoc scope, or an [[Err]] result otherwise.
250
- * @param name
251
- */
252
- tryGetValue < K extends keyof TypeDocOptions > ( name : K ) : Result < TypeDocOptions [ K ] , Error > ;
253
- tryGetValue ( name : string ) : Result < unknown , Error > ;
254
- tryGetValue ( name : string ) : Result < unknown , Error > {
255
246
const declaration = this . getDeclaration ( name ) ;
256
247
if ( ! declaration ) {
257
- return Err ( new Error ( `Unknown option '${ name } '` ) ) ;
248
+ throw new Error ( `Unknown option '${ name } '` ) ;
258
249
}
259
250
260
251
if ( declaration . scope === ParameterScope . TypeScript ) {
261
- return Err ( new Error ( 'TypeScript options must be fetched with getCompilerOptions.' ) ) ;
252
+ throw new Error ( 'TypeScript options must be fetched with getCompilerOptions.' ) ;
262
253
}
263
254
264
- return Ok ( this . _values [ declaration . name ] ) ;
255
+ return this . _values [ declaration . name ] ;
265
256
}
266
257
267
258
/**
@@ -272,47 +263,35 @@ export class Options {
272
263
}
273
264
274
265
/**
275
- * Sets the given declared option. Returns a result with the Err set if the option fails,
276
- * otherwise Ok(void).
266
+ * Sets the given declared option. Throws if setting the option fails.
277
267
* @param name
278
268
* @param value
279
269
*/
280
- setValue < K extends keyof TypeDocAndTSOptions > ( name : K , value : TypeDocAndTSOptions [ K ] ) : Result < void , Error > ;
281
- setValue ( name : string , value : unknown ) : Result < void , Error > ;
282
- setValue ( name : string , value : unknown ) : Result < void , Error > {
270
+ setValue < K extends keyof TypeDocAndTSOptions > ( name : K , value : TypeDocAndTSOptions [ K ] ) : void ;
271
+ setValue ( name : NeverIfInternal < string > , value : NeverIfInternal < unknown > ) : void ;
272
+ setValue ( name : string , value : unknown ) : void {
283
273
const declaration = this . getDeclaration ( name ) ;
284
274
if ( ! declaration ) {
285
- return Err ( Error ( `Tried to set an option (${ name } ) that was not declared.` ) ) ;
275
+ throw new Error ( `Tried to set an option (${ name } ) that was not declared.` ) ;
286
276
}
287
277
288
- return convert ( value , declaration ) . match ( {
289
- ok : value => {
290
- const bag = declaration . scope === ParameterScope . TypeScript
291
- ? this . _compilerOptions
292
- : this . _values ;
293
- bag [ declaration . name ] = value ;
294
- return Ok ( void 0 ) ;
295
- } ,
296
- err : err => Err ( Error ( err ) )
297
- } ) ;
278
+ const converted = convert ( value , declaration ) ;
279
+ const bag = declaration . scope === ParameterScope . TypeScript
280
+ ? this . _compilerOptions
281
+ : this . _values ;
282
+ bag [ declaration . name ] = converted ;
298
283
}
299
284
300
285
/**
301
- * Sets all the given option values, returns a result with an array of errors for
302
- * keys which failed to be set.
286
+ * Sets all the given option values, throws if any value fails to be set.
303
287
* @param obj
288
+ * @deprecated will be removed in 0.19. Use setValue in a loop instead.
304
289
*/
305
- setValues ( obj : Partial < TypeDocAndTSOptions > ) : Result < void , Error [ ] > {
306
- const errors : Error [ ] = [ ] ;
290
+ setValues ( obj : NeverIfInternal < Partial < TypeDocAndTSOptions > > ) : void {
291
+ this . _logger . warn ( 'Options.setValues is deprecated and will be removed in 0.19.' ) ;
307
292
for ( const [ name , value ] of Object . entries ( obj ) ) {
308
- this . setValue ( name , value ) . match ( {
309
- ok ( ) { } ,
310
- err ( error ) {
311
- errors . push ( error ) ;
312
- }
313
- } ) ;
293
+ this . setValue ( name as keyof TypeDocOptions , value ) ;
314
294
}
315
- return errors . length ? Err ( errors ) : Ok ( void 0 ) ;
316
295
}
317
296
318
297
/**
@@ -325,8 +304,7 @@ export class Options {
325
304
if ( declaration . type === ParameterType . Map ) {
326
305
this . _values [ declaration . name ] = declaration . defaultValue ;
327
306
} else {
328
- this . _values [ declaration . name ] = convert ( declaration . defaultValue , declaration )
329
- . expect ( `Failed to validate default value for ${ declaration . name } ` ) ;
307
+ this . _values [ declaration . name ] = convert ( declaration . defaultValue , declaration ) ;
330
308
}
331
309
}
332
310
}
@@ -350,17 +328,17 @@ export function BindOption<K extends keyof TypeDocOptionMap>(name: K):
350
328
* @privateRemarks
351
329
* This overload is intended for plugin use only with looser type checks. Do not use internally.
352
330
*/
353
- export function BindOption ( name : string ) :
331
+ export function BindOption ( name : NeverIfInternal < string > ) :
354
332
( target : { application : Application } | { options : Options } , key : PropertyKey ) => void ;
355
333
356
334
export function BindOption ( name : string ) {
357
335
return function ( target : { application : Application } | { options : Options } , key : PropertyKey ) {
358
336
Object . defineProperty ( target , key , {
359
337
get ( this : { application : Application } | { options : Options } ) {
360
338
if ( 'options' in this ) {
361
- return this . options . getValue ( name ) ;
339
+ return this . options . getValue ( name as keyof TypeDocOptions ) ;
362
340
} else {
363
- return this . application . options . getValue ( name ) ;
341
+ return this . application . options . getValue ( name as keyof TypeDocOptions ) ;
364
342
}
365
343
} ,
366
344
enumerable : true ,
0 commit comments