diff --git a/src/lib/application.ts b/src/lib/application.ts index b2cf9fdeb..3d1c7a31a 100644 --- a/src/lib/application.ts +++ b/src/lib/application.ts @@ -24,7 +24,7 @@ import { DUMMY_APPLICATION_OWNER } from './utils/component'; import { Options, BindOption } from './utils'; -import { TypeDocAndTSOptions } from './utils/options/declaration'; +import { TypeDocAndTSOptions, TypeDocOptions } from './utils/options/declaration'; /** * The default TypeDoc main application class. @@ -115,7 +115,13 @@ export class Application extends ChildableComponent< * @param options The desired options to set. */ bootstrap(options: Partial = {}): { hasErrors: boolean, inputFiles: string[] } { - this.options.setValues(options); // Ignore result, plugins might declare an option + for (const [key, val] of Object.entries(options)) { + try { + this.options.setValue(key as keyof TypeDocOptions, val); + } catch { + // Ignore errors, plugins haven't been loaded yet and may declare an option. + } + } this.options.read(new Logger()); const logger = this.loggerType; @@ -130,11 +136,13 @@ export class Application extends ChildableComponent< this.plugins.load(); this.options.reset(); - this.options.setValues(options).mapErr(errors => { - for (const error of errors) { + for (const [key, val] of Object.entries(options)) { + try { + this.options.setValue(key as keyof TypeDocOptions, val); + } catch (error) { this.logger.error(error.message); } - }); + } this.options.read(this.logger); return { diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 211f0e86c..4fc0b7e87 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -4,7 +4,7 @@ * script will be used to switch this flag to false when publishing, then immediately back * to true after a successful publish. */ -type Strict = true; +type InternalOnly = true; /** * Helper type to convert `T` to `F` if strict mode is on. @@ -21,14 +21,14 @@ type Strict = true; * function over(flag: string): string { return flag } * ``` */ -export type IfStrict = Strict extends true ? T : F; +export type IfInternal = InternalOnly extends true ? T : F; /** * Helper type to convert `T` to `never` if strict mode is on. * - * See {@link IfStrict} for the rationale. + * See {@link IfInternal} for the rationale. */ -export type NeverIfStrict = IfStrict; +export type NeverIfInternal = IfInternal; export { Options, @@ -50,4 +50,3 @@ export { } from './fs'; export { Logger, LogLevel, ConsoleLogger, CallbackLogger } from './loggers'; export { PluginHost } from './plugins'; -export { Result } from './result'; diff --git a/src/lib/utils/options/declaration.ts b/src/lib/utils/options/declaration.ts index 4b60ced40..48cb165ac 100644 --- a/src/lib/utils/options/declaration.ts +++ b/src/lib/utils/options/declaration.ts @@ -1,6 +1,5 @@ import * as _ from 'lodash'; import { CompilerOptions } from 'typescript'; -import { Result } from '../result'; import { IgnoredTsOptionKeys } from './sources/typescript'; /** @@ -240,44 +239,44 @@ export type DeclarationOptionToOptionType = * @param value * @param option */ -export function convert(value: unknown, option: T): Result, string>; -export function convert(value: unknown, option: T): Result { +export function convert(value: unknown, option: T): DeclarationOptionToOptionType; +export function convert(value: unknown, option: T): unknown { switch (option.type) { case undefined: case ParameterType.String: - return Result.Ok(value == null ? '' : String(value)); + return value == null ? '' : String(value); case ParameterType.Number: - return Result.Ok(parseInt(String(value), 10) || 0); + return parseInt(String(value), 10) || 0; case ParameterType.Boolean: - return Result.Ok(Boolean(value)); + return Boolean(value); case ParameterType.Array: if (Array.isArray(value)) { - return Result.Ok(value.map(String)); + return value.map(String); } else if (typeof value === 'string') { - return Result.Ok(value.split(',')); + return value.split(','); } - return Result.Ok([]); + return []; case ParameterType.Map: const optionMap = option as MapDeclarationOption; const key = String(value).toLowerCase(); if (optionMap.map instanceof Map) { if (optionMap.map.has(key)) { - return Result.Ok(optionMap.map.get(key)); + return optionMap.map.get(key); } if ([...optionMap.map.values()].includes(value)) { - return Result.Ok(value); + return value; } } else { if (optionMap.map.hasOwnProperty(key)) { - return Result.Ok(optionMap.map[key]); + return optionMap.map[key]; } if (Object.values(optionMap.map).includes(value)) { - return Result.Ok(value); + return value; } } - return Result.Err(optionMap.mapError ?? getMapError(optionMap.map, optionMap.name)); + throw new Error(optionMap.mapError ?? getMapError(optionMap.map, optionMap.name)); case ParameterType.Mixed: - return Result.Ok(value); + return value; } } diff --git a/src/lib/utils/options/options.ts b/src/lib/utils/options/options.ts index 7ce309e99..981358d54 100644 --- a/src/lib/utils/options/options.ts +++ b/src/lib/utils/options/options.ts @@ -3,11 +3,10 @@ import * as ts from 'typescript'; import { DeclarationOption, ParameterScope, ParameterType, convert, TypeDocOptions, KeyToDeclaration, TypeDocAndTSOptions, TypeDocOptionMap } from './declaration'; import { Logger } from '../loggers'; -import { Result, Ok, Err } from '../result'; import { insertPrioritySorted } from '../array'; import { addTSOptions, addTypeDocOptions } from './sources'; import { Application } from '../../..'; -import { NeverIfStrict } from '..'; +import { NeverIfInternal } from '..'; /** * Describes an option reader that discovers user configuration and converts it to the @@ -149,7 +148,7 @@ export class Options { * Adds an option declaration to the container. * @param declaration */ - addDeclaration(declaration: NeverIfStrict>): void; + addDeclaration(declaration: NeverIfInternal>): void; addDeclaration(declaration: Readonly): void { const names = [declaration.name]; @@ -224,7 +223,7 @@ export class Options { * @param name */ isDefault(name: keyof TypeDocOptions): boolean; - isDefault(name: NeverIfStrict): boolean; + isDefault(name: NeverIfInternal): boolean; isDefault(name: string): boolean { // getValue will throw if the declaration does not exist. return this.getValue(name as keyof TypeDocOptions) === this.getDeclaration(name)!.defaultValue; @@ -242,32 +241,18 @@ export class Options { * @param name */ getValue(name: K): TypeDocOptions[K]; - getValue(name: NeverIfStrict): unknown; + getValue(name: NeverIfInternal): unknown; getValue(name: string): unknown { - return this.tryGetValue(name as keyof TypeDocOptions).match({ - ok: v => v, - err(err) { throw err; } - }); - } - - /** - * Tries to get the given option key, returns an [[Ok]] result if the option has been - * declared with a TypeDoc scope, or an [[Err]] result otherwise. - * @param name - */ - tryGetValue(name: K): Result; - tryGetValue(name: NeverIfStrict): Result; - tryGetValue(name: string): Result { const declaration = this.getDeclaration(name); if (!declaration) { - return Err(new Error(`Unknown option '${name}'`)); + throw new Error(`Unknown option '${name}'`); } if (declaration.scope === ParameterScope.TypeScript) { - return Err(new Error('TypeScript options must be fetched with getCompilerOptions.')); + throw new Error('TypeScript options must be fetched with getCompilerOptions.'); } - return Ok(this._values[declaration.name]); + return this._values[declaration.name]; } /** @@ -278,61 +263,48 @@ export class Options { } /** - * Sets the given declared option. Returns a result with the Err set if the option fails, - * otherwise Ok(void). + * Sets the given declared option. Throws if setting the option fails. * @param name * @param value */ - setValue(name: K, value: TypeDocAndTSOptions[K]): Result; - setValue(name: NeverIfStrict, value: NeverIfStrict): Result; - setValue(name: string, value: unknown): Result { + setValue(name: K, value: TypeDocAndTSOptions[K]): void; + setValue(name: NeverIfInternal, value: NeverIfInternal): void; + setValue(name: string, value: unknown): void { const declaration = this.getDeclaration(name); if (!declaration) { - return Err(Error(`Tried to set an option (${name}) that was not declared.`)); + throw new Error(`Tried to set an option (${name}) that was not declared.`); } - return convert(value, declaration).match({ - ok: value => { - const bag = declaration.scope === ParameterScope.TypeScript - ? this._compilerOptions - : this._values; - bag[declaration.name] = value; - return Ok(void 0); - }, - err: err => Err(Error(err)) - }); + const converted = convert(value, declaration); + const bag = declaration.scope === ParameterScope.TypeScript + ? this._compilerOptions + : this._values; + bag[declaration.name] = converted; } /** - * Sets all the given option values, returns a result with an array of errors for - * keys which failed to be set. + * Sets all the given option values, throws if any value fails to be set. * @param obj + * @deprecated will be removed in 0.19. Use setValue in a loop instead. */ - setValues(obj: Partial): Result { - const errors: Error[] = []; + setValues(obj: NeverIfInternal>): void { + this._logger.warn('Options.setValues is deprecated and will be removed in 0.19.'); for (const [name, value] of Object.entries(obj)) { - this.setValue(name as keyof TypeDocOptions, value).match({ - ok() {}, - err(error) { - errors.push(error); - } - }); + this.setValue(name as keyof TypeDocOptions, value); } - return errors.length ? Err(errors) : Ok(void 0); } /** * Sets the value of a given option to its default value. - * @param declaration The option whoes value should be reset. + * @param declaration The option whose value should be reset. */ private setOptionValueToDefault(declaration: Readonly): void { if (declaration.scope !== ParameterScope.TypeScript) { - // No nead to convert the defaultValue for a map type as it has to be of a specific type + // No need to convert the defaultValue for a map type as it has to be of a specific type if (declaration.type === ParameterType.Map) { this._values[declaration.name] = declaration.defaultValue; } else { - this._values[declaration.name] = convert(declaration.defaultValue, declaration) - .expect(`Failed to validate default value for ${declaration.name}`); + this._values[declaration.name] = convert(declaration.defaultValue, declaration); } } } @@ -356,7 +328,7 @@ export function BindOption(name: K): * @privateRemarks * This overload is intended for plugin use only with looser type checks. Do not use internally. */ -export function BindOption(name: NeverIfStrict): +export function BindOption(name: NeverIfInternal): (target: { application: Application } | { options: Options }, key: PropertyKey) => void; export function BindOption(name: string) { diff --git a/src/lib/utils/options/readers/arguments.ts b/src/lib/utils/options/readers/arguments.ts index 697bab9bb..c41da6d97 100644 --- a/src/lib/utils/options/readers/arguments.ts +++ b/src/lib/utils/options/readers/arguments.ts @@ -1,7 +1,6 @@ import { OptionsReader, Options } from '..'; import { Logger } from '../../loggers'; import { ParameterType } from '../declaration'; -import { Result } from '../..'; /** * Obtains option values from command-line arguments @@ -19,13 +18,19 @@ export class ArgumentsReader implements OptionsReader { read(container: Options, logger: Logger): void { // Make container's type more lax, we do the appropriate checks manually. const options = container as Options & { - setValue(name: string, value: unknown): Result; + setValue(name: string, value: unknown): void; getValue(name: string): unknown; }; const seen = new Set(); let index = 0; - const error = (error: Error) => logger.error(error.message); + const trySet = (name: string, value: unknown) => { + try { + options.setValue(name, value); + } catch (err) { + logger.error(err.message); + } + }; while (index < this.args.length) { const name = this.args[index]; @@ -35,22 +40,19 @@ export class ArgumentsReader implements OptionsReader { if (decl) { if (seen.has(decl.name) && decl.type === ParameterType.Array) { - options.setValue( - decl.name, - (options.getValue(decl.name) as string[]).concat(this.args[index]) - ).mapErr(error); + trySet(decl.name, (options.getValue(decl.name) as string[]).concat(this.args[index])); } else if (decl.type === ParameterType.Boolean) { const value = String(this.args[index]).toLowerCase(); if (value === 'true' || value === 'false') { - options.setValue(decl.name, value === 'true').mapErr(error); + trySet(decl.name, value === 'true'); } else { - options.setValue(decl.name, true).mapErr(error); + trySet(decl.name, true); // Bool option didn't consume the next argument as expected. index--; } } else { - options.setValue(decl.name, this.args[index]).mapErr(error); + trySet(decl.name, this.args[index]); } seen.add(decl.name); } else { diff --git a/src/lib/utils/options/readers/tsconfig.ts b/src/lib/utils/options/readers/tsconfig.ts index 07c28d01b..42d1a458a 100644 --- a/src/lib/utils/options/readers/tsconfig.ts +++ b/src/lib/utils/options/readers/tsconfig.ts @@ -37,7 +37,7 @@ export class TSConfigReader implements OptionsReader { this._tryReadOptions(tsconfigOpt, container); } - private _tryReadOptions(file: string, container: Options, logger?: Logger): void { + private _tryReadOptions(file: string, container: Options & { setValue(name: string, value: unknown): void }, logger?: Logger): void { let fileToRead: string | undefined = file; if (!isFile(fileToRead)) { fileToRead = ts.findConfigFile(file, isFile, file.toLowerCase().endsWith('.json') ? basename(file) : undefined); @@ -58,7 +58,7 @@ export class TSConfigReader implements OptionsReader { {}, fileToRead); - container.setValue('inputFiles', fileNames).unwrap(); + container.setValue('inputFiles', fileNames); for (const key of IGNORED) { delete options[key]; } @@ -71,16 +71,20 @@ export class TSConfigReader implements OptionsReader { delete typedocOptions.options; } - container.setValues(options).mapErr(errors => { - for (const err of errors) { - logger?.error(err.message); + for (const [key, val] of Object.entries(options)) { + try { + container.setValue(key, val); + } catch (error) { + logger?.error(error.message); } - }); - container.setValues(typedocOptions || {}).mapErr(errors => { - for (const err of errors) { - logger?.error(err.message); + } + for (const [key, val] of Object.entries(typedocOptions || {})) { + try { + container.setValue(key, val); + } catch (error) { + logger?.error(error.message); } - }); + } } } diff --git a/src/lib/utils/options/readers/typedoc.ts b/src/lib/utils/options/readers/typedoc.ts index 44b867a43..e43cfc221 100644 --- a/src/lib/utils/options/readers/typedoc.ts +++ b/src/lib/utils/options/readers/typedoc.ts @@ -44,7 +44,7 @@ export class TypeDocReader implements OptionsReader { * @param container * @param logger */ - private readFile(file: string, container: Options, logger: Logger, seen: Set) { + private readFile(file: string, container: Options & { setValue(key: string, value: unknown): void }, logger: Logger, seen: Set) { if (seen.has(file)) { logger.error(`Tried to load the options file ${file} multiple times.`); return; @@ -74,14 +74,13 @@ export class TypeDocReader implements OptionsReader { delete data['src']; } - container.setValues(data).match({ - ok() {}, - err(errors) { - for (const err of errors) { - logger.error(err.message); - } + for (const [key, val] of Object.entries(data)) { + try { + container.setValue(key, val); + } catch (error) { + logger.error(error.message); } - }); + } } /** diff --git a/src/lib/utils/result.ts b/src/lib/utils/result.ts deleted file mode 100644 index f92397dba..000000000 --- a/src/lib/utils/result.ts +++ /dev/null @@ -1,95 +0,0 @@ - -/** - * A wrapper class for either an Ok result or a resulting Err. - * Modeled after Rust's std::result::Result type. - */ -export class Result { - /** - * Construct with the result of a successful operation. - * @param data - */ - static Ok(data: T) { - return new Result([true, data]); - } - - /** - * Construct with the result of an unsuccessful operation. - * @param err - */ - static Err(err: E) { - return new Result([false, err]); - } - - private _data: [true, T] | [false, E]; - private constructor(data: Result['_data']) { - this._data = data; - } - - /** - * Unwraps a result type, throwing an error with the given message if the result is an Err. - * @param message - */ - expect(message: string): T { - if (this._data[0]) { - return this._data[1]; - } - throw new Error(message); - } - - /** - * Unwraps a result type, throwing if the result is an Err. - */ - unwrap(): T { - return this.expect('Tried to unwrap as Ok an Err.'); - } - - /** - * Unwraps an error type, throwing if the result is Ok. - */ - unwrapErr(): E { - if (this._data[0]) { - throw new Error('Tried to unwrap an Err as Ok.'); - } - return this._data[1]; - } - - /** - * Match all possible values of the result. - * @param funcs - */ - match(funcs: { ok(data: T): T2, err(err: E): E2 }): T2 | E2 { - if (this._data[0]) { - return funcs.ok(this._data[1]); - } - return funcs.err(this._data[1]); - } - - /** - * Map the Ok type of a result to a new type. - * @param func - */ - map(func: (data: T) => T2): Result { - return this.match({ - ok: data => Result.Ok(func(data)), - err: err => Result.Err(err) - }); - } - - /** - * Map the Err type of a result to a new type. - * @param func - */ - mapErr(func: (err: E) => E2): Result { - return this.match({ - ok: data => Result.Ok(data), - err: err => Result.Err(func(err)) - }); - } - - toString() { - return `[${this._data[0] ? 'Ok' : 'Err'} ${this._data[1]}]`; - } -} - -export const Ok = Result.Ok; -export const Err = Result.Err; diff --git a/src/test/converter.test.ts b/src/test/converter.test.ts index 5b03710fa..08c2e0cbf 100644 --- a/src/test/converter.test.ts +++ b/src/test/converter.test.ts @@ -26,16 +26,16 @@ describe('Converter', function() { const checks: [string, () => void, () => void][] = [ ['specs', () => { }, () => { }], ['specs.d', - () => app.options.setValue('includeDeclarations', true).unwrap(), - () => app.options.setValue('includeDeclarations', false).unwrap() + () => app.options.setValue('includeDeclarations', true), + () => app.options.setValue('includeDeclarations', false) ], ['specs-without-exported', - () => app.options.setValue('excludeNotExported', true).unwrap(), - () => app.options.setValue('excludeNotExported', false).unwrap() + () => app.options.setValue('excludeNotExported', true), + () => app.options.setValue('excludeNotExported', false) ], ['specs-with-lump-categories', - () => app.options.setValue('categorizeByGroup', false).unwrap(), - () => app.options.setValue('categorizeByGroup', true).unwrap() + () => app.options.setValue('categorizeByGroup', false), + () => app.options.setValue('categorizeByGroup', true) ] ]; diff --git a/src/test/typedoc.test.ts b/src/test/typedoc.test.ts index e80a49607..344f5d273 100644 --- a/src/test/typedoc.test.ts +++ b/src/test/typedoc.test.ts @@ -26,7 +26,7 @@ describe('TypeDoc', function() { }); it('honors the exclude argument even on a fixed directory list', function() { const inputFiles = Path.join(__dirname, 'converter', 'class'); - application.options.setValue('exclude', ['**/class.ts']).unwrap(); + application.options.setValue('exclude', ['**/class.ts']); const expanded = application.expandInputFiles([inputFiles]); Assert(!expanded.includes(Path.join(inputFiles, 'class.ts'))); @@ -34,14 +34,14 @@ describe('TypeDoc', function() { }); it('honors the exclude argument even on a fixed file list', function() { const inputFiles = Path.join(__dirname, 'converter', 'class', 'class.ts'); - application.options.setValue('exclude', ['**/class.ts']).unwrap(); + application.options.setValue('exclude', ['**/class.ts']); const expanded = application.expandInputFiles([inputFiles]); Assert(!expanded.includes(inputFiles)); }); it('supports multiple excludes', function() { const inputFiles = Path.join(__dirname, 'converter'); - application.options.setValue('exclude', ['**/+(class|access).ts']).unwrap(); + application.options.setValue('exclude', ['**/+(class|access).ts']); const expanded = application.expandInputFiles([inputFiles]); Assert(!expanded.includes(Path.join(inputFiles, 'class', 'class.ts'))); @@ -50,7 +50,7 @@ describe('TypeDoc', function() { }); it('supports array of excludes', function() { const inputFiles = Path.join(__dirname, 'converter'); - application.options.setValue('exclude', [ '**/class.ts', '**/access.ts' ]).unwrap(); + application.options.setValue('exclude', [ '**/class.ts', '**/access.ts' ]); const expanded = application.expandInputFiles([inputFiles]); Assert(!expanded.includes(Path.join(inputFiles, 'class', 'class.ts'))); @@ -59,15 +59,15 @@ describe('TypeDoc', function() { }); it('supports excluding directories beginning with dots', function() { const inputFiles = __dirname; - application.options.setValue('exclude', ['**/+(.dot)/**']).unwrap(); + application.options.setValue('exclude', ['**/+(.dot)/**']); const expanded = application.expandInputFiles([inputFiles]); Assert(!expanded.includes(Path.join(inputFiles, '.dot', 'index.ts'))); Assert(!expanded.includes(inputFiles)); }); it('Honors the exclude option even if a module is imported', () => { - application.options.setValue('exclude', ['**/b.ts']).unwrap(); - application.options.setValue('module', ModuleKind.CommonJS).unwrap(); + application.options.setValue('exclude', ['**/b.ts']); + application.options.setValue('module', ModuleKind.CommonJS); function handler(context: Context) { Assert.deepStrictEqual(context.fileNames, [ @@ -80,7 +80,7 @@ describe('TypeDoc', function() { it('supports directory excludes', function() { const inputFiles = Path.join(__dirname, 'converter'); - application.options.setValue('exclude', [ '**/alias' ]).unwrap(); + application.options.setValue('exclude', [ '**/alias' ]); const expanded = application.expandInputFiles([inputFiles]); Assert.strictEqual(expanded.includes(Path.join(inputFiles, 'class', 'class.ts')), true); @@ -90,7 +90,7 @@ describe('TypeDoc', function() { it('supports negations in directory excludes', function() { const inputFiles = Path.join(__dirname, 'converter'); - application.options.setValue('exclude', [ '**/!(alias)/' ]).unwrap(); + application.options.setValue('exclude', [ '**/!(alias)/' ]); const expanded = application.expandInputFiles([inputFiles]); Assert.strictEqual(expanded.includes(Path.join(inputFiles, 'class', 'class.ts')), false); diff --git a/src/test/utils/options/declaration.test.ts b/src/test/utils/options/declaration.test.ts index c154c5542..4b70ab29d 100644 --- a/src/test/utils/options/declaration.test.ts +++ b/src/test/utils/options/declaration.test.ts @@ -1,6 +1,5 @@ import { convert, DeclarationOption, ParameterType, MapDeclarationOption } from '../../../lib/utils/options/declaration'; -import { deepStrictEqual as equal } from 'assert'; -import { Result } from '../../../lib/utils'; +import { deepStrictEqual as equal, throws } from 'assert'; describe('Options - Default convert function', () => { const optionWithType = (type: ParameterType) => ({ @@ -11,29 +10,29 @@ describe('Options - Default convert function', () => { }) as DeclarationOption; it('Converts to numbers', () => { - equal(convert('123', optionWithType(ParameterType.Number)), Result.Ok(123)); - equal(convert('a', optionWithType(ParameterType.Number)), Result.Ok(0)); - equal(convert(NaN, optionWithType(ParameterType.Number)), Result.Ok(0)); + equal(convert('123', optionWithType(ParameterType.Number)), 123); + equal(convert('a', optionWithType(ParameterType.Number)), 0); + equal(convert(NaN, optionWithType(ParameterType.Number)), 0); }); it('Converts to strings', () => { - equal(convert('123', optionWithType(ParameterType.String)), Result.Ok('123')); - equal(convert(123, optionWithType(ParameterType.String)), Result.Ok('123')); - equal(convert(['1', '2'], optionWithType(ParameterType.String)), Result.Ok('1,2')); - equal(convert(null, optionWithType(ParameterType.String)), Result.Ok('')); - equal(convert(void 0, optionWithType(ParameterType.String)), Result.Ok('')); + equal(convert('123', optionWithType(ParameterType.String)), '123'); + equal(convert(123, optionWithType(ParameterType.String)), '123'); + equal(convert(['1', '2'], optionWithType(ParameterType.String)), '1,2'); + equal(convert(null, optionWithType(ParameterType.String)), ''); + equal(convert(void 0, optionWithType(ParameterType.String)), ''); }); it('Converts to booleans', () => { - equal(convert('a', optionWithType(ParameterType.Boolean)), Result.Ok(true)); - equal(convert([1], optionWithType(ParameterType.Boolean)), Result.Ok(true)); - equal(convert(false, optionWithType(ParameterType.Boolean)), Result.Ok(false)); + equal(convert('a', optionWithType(ParameterType.Boolean)), true); + equal(convert([1], optionWithType(ParameterType.Boolean)), true); + equal(convert(false, optionWithType(ParameterType.Boolean)), false); }); it('Converts to arrays', () => { - equal(convert('12,3', optionWithType(ParameterType.Array)), Result.Ok(['12', '3'])); - equal(convert(['12,3'], optionWithType(ParameterType.Array)), Result.Ok(['12,3'])); - equal(convert(true, optionWithType(ParameterType.Array)), Result.Ok([])); + equal(convert('12,3', optionWithType(ParameterType.Array)), ['12', '3']); + equal(convert(['12,3'], optionWithType(ParameterType.Array)), ['12,3']); + equal(convert(true, optionWithType(ParameterType.Array)), []); }); it('Converts to mapped types', () => { @@ -47,9 +46,9 @@ describe('Options - Default convert function', () => { }, defaultValue: 1 }; - equal(convert('a', declaration), Result.Ok(1)); - equal(convert('b', declaration), Result.Ok(2)); - equal(convert(2, declaration), Result.Ok(2)); + equal(convert('a', declaration), 1); + equal(convert('b', declaration), 2); + equal(convert(2, declaration), 2); }); it('Converts to mapped types with a map', () => { @@ -63,9 +62,9 @@ describe('Options - Default convert function', () => { ]), defaultValue: 1 }; - equal(convert('a', declaration), Result.Ok(1)); - equal(convert('b', declaration), Result.Ok(2)); - equal(convert(2, declaration), Result.Ok(2)); + equal(convert('a', declaration), 1); + equal(convert('b', declaration), 2); + equal(convert(2, declaration), 2); }); it('Uses the mapError if provided for errors', () => { @@ -77,7 +76,7 @@ describe('Options - Default convert function', () => { defaultValue: 1, mapError: 'Test error' }; - equal(convert('a', declaration), Result.Err(declaration.mapError)); + throws(() => convert('a', declaration), new Error(declaration.mapError)); }); it('Generates a nice error if no mapError is provided', () => { @@ -88,7 +87,7 @@ describe('Options - Default convert function', () => { map: new Map([['a', 1], ['b', 2]]), defaultValue: 1 }; - equal(convert('c', declaration), Result.Err('test must be one of a, b')); + throws(() => convert('c', declaration), new Error('test must be one of a, b')); }); it('Correctly handles enum types in the map error', () => { @@ -100,11 +99,11 @@ describe('Options - Default convert function', () => { map: Enum, defaultValue: Enum.a } as const; - equal(convert('c', declaration), Result.Err('test must be one of a, b')); + throws(() => convert('c', declaration), new Error('test must be one of a, b')); }); it('Passes through mixed', () => { const data = Symbol(); - equal(convert(data, optionWithType(ParameterType.Mixed)), Result.Ok(data)); + equal(convert(data, optionWithType(ParameterType.Mixed)), data); }); }); diff --git a/src/test/utils/options/options.test.ts b/src/test/utils/options/options.test.ts index 9b47d5c5d..0909c906a 100644 --- a/src/test/utils/options/options.test.ts +++ b/src/test/utils/options/options.test.ts @@ -70,7 +70,7 @@ describe('Options', () => { }); it('Errors if converting a set value errors', () => { - throws(() => options.setValue('mode', 'nonsense' as any).unwrap()); + throws(() => options.setValue('mode', 'nonsense' as any)); }); it('Supports directly getting values', () => { diff --git a/src/test/utils/options/readers/tsconfig.test.ts b/src/test/utils/options/readers/tsconfig.test.ts index 8ed918ece..a77214d92 100644 --- a/src/test/utils/options/readers/tsconfig.test.ts +++ b/src/test/utils/options/readers/tsconfig.test.ts @@ -13,7 +13,7 @@ describe('Options - TSConfigReader', () => { function testError(name: string, file: string) { it(name, () => { options.reset(); - options.setValue('tsconfig', file).unwrap(); + options.setValue('tsconfig', file); const logger = new Logger(); options.read(logger); equal(logger.hasErrors(), true, 'No error was logged'); @@ -33,7 +33,7 @@ describe('Options - TSConfigReader', () => { })(new Logger()); options.addDefaultDeclarations(); - options.setValue('tsconfig', join(__dirname, 'data/does_not_exist.json')).unwrap(); + options.setValue('tsconfig', join(__dirname, 'data/does_not_exist.json')); const logger = new Logger(); options.addReader(new TSConfigReader()); options.read(logger); @@ -42,7 +42,7 @@ describe('Options - TSConfigReader', () => { it('Also reads files according to --project', () => { options.reset(); - options.setValue('project', join(__dirname, 'data/valid.tsconfig.json')).unwrap(); + options.setValue('project', join(__dirname, 'data/valid.tsconfig.json')); const logger = new Logger(); options.read(logger); equal(options.getValue('help'), true); diff --git a/src/test/utils/options/readers/typedoc.test.ts b/src/test/utils/options/readers/typedoc.test.ts index 0d363b405..cc4e2f959 100644 --- a/src/test/utils/options/readers/typedoc.test.ts +++ b/src/test/utils/options/readers/typedoc.test.ts @@ -12,7 +12,7 @@ describe('Options - TypeDocReader', () => { function test(name: string, input: string, cb: () => void) { it(name, () => { options.reset(); - options.setValue('options', input).unwrap(); + options.setValue('options', input); options.read(new ConsoleLogger()); cb(); }); @@ -34,7 +34,7 @@ describe('Options - TypeDocReader', () => { function testError(name: string, file: string) { it(name, () => { options.reset(); - options.setValue('options', file).unwrap(); + options.setValue('options', file); const logger = new Logger(); options.read(logger); equal(logger.hasErrors(), true, 'No error was logged'); diff --git a/src/test/utils/result.test.ts b/src/test/utils/result.test.ts deleted file mode 100644 index 6148c733d..000000000 --- a/src/test/utils/result.test.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { equal, fail, throws } from 'assert'; -import { Result } from '../../lib/utils'; - -describe('Result', () => { - const okResult = Result.Ok('ok'); - const errResult = Result.Err('error'); - const returnOne = () => 1; - - it('Unwraps Ok', () => { - equal(okResult.unwrap(), 'ok'); - }); - - it('Throws if unwrapping Err as Ok', () => { - throws(() => errResult.unwrap()); - }); - - it ('Unwraps Err', () => { - equal(errResult.unwrapErr(), 'error'); - }); - - it('Throws if unwrapping Ok as Err', () => { - throws(() => okResult.unwrapErr()); - }); - - it('Matches Ok', () => { - okResult.match({ - ok: data => equal(data, 'ok'), - err: fail - }); - }); - - it('Matches Err', () => { - errResult.match({ - ok: fail, - err: data => equal(data, 'error') - }); - }); - - it('Maps Ok', () => { - equal(okResult.map(returnOne).unwrap(), 1); - }); - - it('Maps Ok when Err', () => { - equal(errResult.map(returnOne).unwrapErr(), 'error'); - }); - - it('Maps Err', () => { - equal(errResult.mapErr(returnOne).unwrapErr(), 1); - }); - - it('Maps Err when Ok', () => { - equal(okResult.mapErr(returnOne).unwrap(), 'ok'); - }); - - it('Has a nice display string', () => { - equal(okResult.toString(), '[Ok ok]'); - equal(errResult.toString(), '[Err error]'); - }); -});