Skip to content

Commit 6a762f0

Browse files
committed
feat: Expose @Option for plugin use
Also exposes the necessary types + values to declare and set options. Closes #1163 Closes #1165
1 parent 1fd2185 commit 6a762f0

File tree

9 files changed

+75
-6
lines changed

9 files changed

+75
-6
lines changed

src/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,36 @@ export { NavigationItem } from './lib/output/models/NavigationItem';
1313
export { UrlMapping } from './lib/output/models/UrlMapping';
1414

1515
export {
16+
SourceFileMode
17+
} from './lib/converter';
18+
19+
export {
20+
Option,
1621
Options,
1722
OptionsReader,
1823
ParameterHint,
1924
ParameterScope,
2025
ParameterType,
26+
2127
TypeDocOptions,
28+
TypeDocAndTSOptions,
29+
TypeDocOptionMap,
30+
KeyToDeclaration,
2231

2332
TSConfigReader,
2433
TypeDocReader,
25-
ArgumentsReader
34+
ArgumentsReader,
35+
36+
DeclarationOption,
37+
38+
DeclarationOptionBase,
39+
StringDeclarationOption,
40+
NumberDeclarationOption,
41+
BooleanDeclarationOption,
42+
ArrayDeclarationOption,
43+
MixedDeclarationOption,
44+
MapDeclarationOption,
45+
DeclarationOptionToOptionType
2646
} from './lib/utils/options';
2747

2848
export { JSONOutput } from './lib/serialization';

src/lib/application.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import { Option, Options, ParameterType } from './utils';
2727
import { ParameterHint } from './utils/options';
2828
import { TypeDocAndTSOptions } from './utils/options/declaration';
29+
import { addDecoratedOptions } from './utils/options/sources';
2930

3031
/**
3132
* The default TypeDoc main application class.
@@ -158,6 +159,8 @@ export class Application extends ChildableComponent<
158159
}
159160

160161
this.plugins.load();
162+
// Load decorated options from the plugins.
163+
addDecoratedOptions(this.options);
161164

162165
this.options.reset();
163166
this.options.setValues(options).mapErr(errors => {

src/lib/converter/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export { Converter } from './converter';
33

44
export { convertDefaultValue, convertExpression } from './convert-expression';
55

6+
export { SourceFileMode } from './nodes';
7+
68
import './nodes/index';
79
import './types/index';
810
import './plugins/index';

src/lib/converter/nodes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export { AccessorConverter } from './accessor';
22
export { AliasConverter } from './alias';
3-
export { BlockConverter } from './block';
3+
export { BlockConverter, SourceFileMode } from './block';
44
export { ClassConverter } from './class';
55
export { ConstructorConverter } from './constructor';
66
export { EnumConverter } from './enum';

src/lib/utils/options/declaration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export type TypeDocAndTSOptions = TypeDocOptions
3232
& Pick<CompilerOptions, Exclude<KnownKeys<CompilerOptions>, IgnoredTsOptionKeys>>;
3333

3434
/**
35-
* Describes all TypeDoc options.
35+
* Describes all TypeDoc options. Used internally to provide better types when fetching options.
36+
* External consumers should likely use either [[TypeDocAndTSOptions]] or [[TypeDocOptions]].
3637
*/
3738
export interface TypeDocOptionMap {
3839
options: string;

src/lib/utils/options/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
export { Options, OptionsReader } from './options';
22
export { Option } from './sources';
33
export { ArgumentsReader, TypeDocReader, TSConfigReader } from './readers';
4-
export { TypeDocOptions, ParameterType, ParameterHint, ParameterScope } from './declaration';
4+
export {
5+
TypeDocOptions,
6+
TypeDocAndTSOptions,
7+
8+
TypeDocOptionMap,
9+
KeyToDeclaration,
10+
11+
ParameterType,
12+
ParameterHint,
13+
ParameterScope,
14+
15+
DeclarationOption,
16+
17+
DeclarationOptionBase,
18+
StringDeclarationOption,
19+
NumberDeclarationOption,
20+
BooleanDeclarationOption,
21+
ArrayDeclarationOption,
22+
MixedDeclarationOption,
23+
MapDeclarationOption,
24+
DeclarationOptionToOptionType
25+
} from './declaration';

src/lib/utils/options/options.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ export class Options {
160160
}
161161

162162
for (const name of names) {
163-
if (this.getDeclaration(name)) {
163+
// Check for registering the same declaration twice, should not be an error.
164+
const decl = this.getDeclaration(name);
165+
if (decl && decl !== declaration) {
164166
this._logger.error(`The option ${name} has already been registered`);
165167
} else {
166168
this._declarations.set(name.toLowerCase(), declaration);

src/lib/utils/options/sources/decorator.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ export function addDecoratedOptions(options: Options) {
1212
* Declares the given option and binds it to the decorated property.
1313
* @param option
1414
*/
15-
export function Option<K extends keyof TypeDocOptionMap>(option: { name: K } & KeyToDeclaration<K>) {
15+
export function Option<K extends keyof TypeDocOptionMap>(option: { name: K } & KeyToDeclaration<K>);
16+
17+
/**
18+
* Declares the given option and binds it to the decorated property without strict checks.
19+
*
20+
* @privateRemarks
21+
* Intended for plugin use only. SHOULD NOT BE USED INTERNALLY.
22+
* @param option
23+
*/
24+
export function Option<K extends keyof TypeDocOptionMap>(option: DeclarationOption);
25+
26+
export function Option(option: DeclarationOption) {
1627
declared.push(option);
1728

1829
return function(target: { application: Application } | { options: Options }, key: PropertyKey) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ describe('Options', () => {
1616
equal(logger.hasErrors(), true);
1717
});
1818

19+
it('Does not error if the same declaration is registered twice', () => {
20+
logger.resetErrors();
21+
const declaration = { name: 'test-declaration', help: '' };
22+
options.addDeclaration(declaration);
23+
options.addDeclaration(declaration);
24+
equal(logger.hasErrors(), false);
25+
options.removeDeclarationByName(declaration.name);
26+
});
27+
1928
it('Supports removing a declaration by name', () => {
2029
options.addDeclaration({ name: 'not-an-option', help: '' });
2130
options.removeDeclarationByName('not-an-option');

0 commit comments

Comments
 (0)