Skip to content

Commit c296503

Browse files
authored
fix(options): Map type options should not have their default value validated (#1250)
* No need to convert defaultValue for options of type map * Added test for map declaration option with foreign default value
1 parent 2e63096 commit c296503

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/lib/utils/options/options.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as _ from 'lodash';
22
import * as ts from 'typescript';
33

4-
import { DeclarationOption, ParameterScope, convert, TypeDocOptions, KeyToDeclaration, TypeDocAndTSOptions, TypeDocOptionMap } from './declaration';
4+
import { DeclarationOption, ParameterScope, ParameterType, convert, TypeDocOptions, KeyToDeclaration, TypeDocAndTSOptions, TypeDocOptionMap } from './declaration';
55
import { Logger } from '../loggers';
66
import { Result, Ok, Err } from '../result';
77
import { insertPrioritySorted } from '../array';
@@ -109,10 +109,7 @@ export class Options {
109109
*/
110110
reset() {
111111
for (const declaration of this._declarations.values()) {
112-
if (declaration.scope !== ParameterScope.TypeScript) {
113-
this._values[declaration.name] = convert(declaration.defaultValue, declaration)
114-
.expect(`Failed to validate default value for ${declaration.name}`);
115-
}
112+
this.setOptionValueToDefault(declaration);
116113
}
117114
this._compilerOptions = {};
118115
}
@@ -169,10 +166,7 @@ export class Options {
169166
}
170167
}
171168

172-
if (declaration.scope !== ParameterScope.TypeScript) {
173-
this._values[declaration.name] = convert(declaration.defaultValue, declaration)
174-
.expect(`Failed to validate default value for ${declaration.name}`);
175-
}
169+
this.setOptionValueToDefault(declaration);
176170
}
177171

178172
/**
@@ -320,6 +314,22 @@ export class Options {
320314
}
321315
return errors.length ? Err(errors) : Ok(void 0);
322316
}
317+
318+
/**
319+
* Sets the value of a given option to its default value.
320+
* @param declaration The option whoes value should be reset.
321+
*/
322+
private setOptionValueToDefault(declaration: Readonly<DeclarationOption>): void {
323+
if (declaration.scope !== ParameterScope.TypeScript) {
324+
// No nead to convert the defaultValue for a map type as it has to be of a specific type
325+
if (declaration.type === ParameterType.Map) {
326+
this._values[declaration.name] = declaration.defaultValue;
327+
} else {
328+
this._values[declaration.name] = convert(declaration.defaultValue, declaration)
329+
.expect(`Failed to validate default value for ${declaration.name}`);
330+
}
331+
}
332+
}
323333
}
324334

325335
/**

src/test/utils/options/options.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ describe('Options', () => {
2525
options.removeDeclarationByName(declaration.name);
2626
});
2727

28+
it('Does not error if a map declaration has a default value that is not part of the map of possible values', () => {
29+
logger.resetErrors();
30+
options.addDeclaration({
31+
name: 'testMapDeclarationWithForeignDefaultValue',
32+
help: '',
33+
type: ParameterType.Map,
34+
map: new Map([
35+
['a', 1],
36+
['b', 2]
37+
]),
38+
defaultValue: 0
39+
});
40+
equal(logger.hasErrors(), false);
41+
});
42+
2843
it('Supports removing a declaration by name', () => {
2944
options.addDeclaration({ name: 'not-an-option', help: '' });
3045
options.removeDeclarationByName('not-an-option');

0 commit comments

Comments
 (0)