Skip to content

Commit 6e6b3b6

Browse files
committed
Typed event emitters
1 parent e35608f commit 6e6b3b6

38 files changed

+408
-1924
lines changed

CHANGELOG.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
# Beta (full release: 2024-06-21)
2-
3-
### To Do
4-
5-
- Update website docs - consider if reworking website to just be a TypeDoc generated site is a good idea
6-
`@license`, `@import`, `@hideGroups` `@hideCategories` sitemapBaseUrl, markedOptions -> markdownItOptions, markdownItLoader, navigation
7-
sort - documents-first, documents-last, alphabetical-ignoring-documents
8-
searchInDocuments, useHostedBaseUrlForAbsoluteLinks
1+
# Unreleased
92

103
### Breaking Changes
114

@@ -28,6 +21,8 @@
2821
This change was extended to apply not only to type aliases, but also other function-likes declared with variables and callable properties.
2922
As a part of this change, comments on the implementation signature of overloaded functions will now be added to the function reflection, and will
3023
not be inherited by signatures of that function, #2521.
24+
- API: TypeDoc now uses a typed event emitter to provide improved type safety, this found a bug where `Converter.EVENT_CREATE_DECLARATION`
25+
was emitted for `ProjectReflection` in some circumstances.
3126
- API: `MapOptionDeclaration.mapError` has been removed.
3227
- API: Deprecated `BindOption` decorator has been removed.
3328
- API: `DeclarationReflection.indexSignature` has been renamed to `DeclarationReflection.indexSignatures`.

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export { Application } from "./lib/application";
1+
export { Application, type ApplicationEvents } from "./lib/application";
22

3-
export { EventDispatcher, Event } from "./lib/utils/events";
3+
export { EventDispatcher } from "./lib/utils/events";
44
export { resetReflectionID } from "./lib/models/reflections/abstract";
55
/**
66
* All symbols documented under the Models namespace are also available in the root import.
@@ -29,6 +29,7 @@ export {
2929
type MeaningKeyword,
3030
type ExternalResolveResult,
3131
type ExternalSymbolResolver,
32+
type ConverterEvents,
3233
} from "./lib/converter";
3334

3435
export {
@@ -47,6 +48,7 @@ export type {
4748
RenderTemplate,
4849
RendererHooks,
4950
NavigationElement,
51+
RendererEvents,
5052
} from "./lib/output";
5153

5254
export {
@@ -94,11 +96,10 @@ export type {
9496
JsDocCompatibility,
9597
} from "./lib/utils";
9698

97-
export type { EventMap, EventCallback } from "./lib/utils/events";
98-
9999
export {
100100
JSONOutput,
101101
Serializer,
102+
type SerializerEvents,
102103
Deserializer,
103104
type Deserializable,
104105
type DeserializerComponent,

src/lib/application-events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export const ApplicationEvents = {
22
BOOTSTRAP_END: "bootstrapEnd",
33
REVIVE: "reviveProject",
44
VALIDATE_PROJECT: "validateProject",
5-
};
5+
} as const;

src/lib/application.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ const DEFAULT_READERS = [
7474
new TSConfigReader(),
7575
];
7676

77+
export interface ApplicationEvents {
78+
bootstrapEnd: [Application];
79+
reviveProject: [ProjectReflection];
80+
validateProject: [ProjectReflection];
81+
}
82+
7783
/**
7884
* The default TypeDoc main application class.
7985
*
@@ -95,7 +101,8 @@ const DEFAULT_READERS = [
95101
@Component({ name: "application", internal: true })
96102
export class Application extends ChildableComponent<
97103
Application,
98-
AbstractComponent<Application>
104+
AbstractComponent<Application, {}>,
105+
ApplicationEvents
99106
> {
100107
/**
101108
* The converter used to create the declaration reflections.
@@ -187,8 +194,8 @@ export class Application extends ChildableComponent<
187194
}
188195
super(null!); // We own ourselves
189196

190-
this.converter = this.addComponent<Converter>("converter", Converter);
191-
this.renderer = this.addComponent<Renderer>("renderer", Renderer);
197+
this.converter = new Converter(this);
198+
this.renderer = new Renderer(this);
192199
this.logger.i18n = this.i18n;
193200
}
194201

src/lib/converter/components.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ import type { Converter } from "./converter";
33

44
export { Component };
55

6-
export abstract class ConverterComponent extends AbstractComponent<Converter> {}
6+
export abstract class ConverterComponent extends AbstractComponent<
7+
Converter,
8+
{}
9+
> {}

src/lib/converter/context.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,6 @@ export class Context {
262262
this.project.registerReflection(reflection, symbol, void 0);
263263
}
264264

265-
/**
266-
* Trigger a node reflection event.
267-
*
268-
* All events are dispatched on the current converter instance.
269-
*
270-
* @param name The name of the event that should be triggered.
271-
* @param reflection The triggering reflection.
272-
* @param node The triggering TypeScript node if available.
273-
*/
274-
trigger(name: string, reflection: Reflection, node?: ts.Node) {
275-
this.converter.trigger(name, this, reflection, node);
276-
}
277-
278265
/** @internal */
279266
setActiveProgram(program: ts.Program | undefined) {
280267
this._program = program;

src/lib/converter/converter.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import {
55
Comment,
66
type CommentDisplayPart,
77
type ContainerReflection,
8+
type DeclarationReflection,
89
DocumentReflection,
10+
type ParameterReflection,
911
ProjectReflection,
1012
type Reflection,
1113
ReflectionKind,
1214
type ReflectionSymbolId,
15+
type SignatureReflection,
1316
type SomeType,
17+
type TypeParameterReflection,
1418
} from "../models/index";
1519
import { Context } from "./context";
1620
import { ConverterComponent } from "./components";
@@ -49,6 +53,31 @@ import {
4953
import { basename, dirname, resolve } from "path";
5054
import type { FileRegistry } from "../models/FileRegistry";
5155

56+
export interface ConverterEvents {
57+
begin: [Context];
58+
end: [Context];
59+
createDeclaration: [Context, DeclarationReflection];
60+
createSignature: [
61+
Context,
62+
SignatureReflection,
63+
(
64+
| ts.SignatureDeclaration
65+
| ts.IndexSignatureDeclaration
66+
| ts.JSDocSignature
67+
)?,
68+
ts.Signature?,
69+
];
70+
createParameter: [Context, ParameterReflection, ts.Node?];
71+
createTypeParameter: [
72+
Context,
73+
TypeParameterReflection,
74+
ts.TypeParameterDeclaration?,
75+
];
76+
resolveBegin: [Context];
77+
resolveReflection: [Context, Reflection];
78+
resolveEnd: [Context];
79+
}
80+
5281
/**
5382
* Compiles source files using TypeScript and converts compiler symbols to reflections.
5483
*/
@@ -59,7 +88,8 @@ import type { FileRegistry } from "../models/FileRegistry";
5988
})
6089
export class Converter extends ChildableComponent<
6190
Application,
62-
ConverterComponent
91+
ConverterComponent,
92+
ConverterEvents
6393
> {
6494
/** @internal */
6595
@Option("externalPattern")
@@ -440,10 +470,6 @@ export class Converter extends ChildableComponent<
440470
? context.getComment(symbol, context.project.kind)
441471
: context.getFileComment(node);
442472
this.processDocumentTags(context.project, context.project);
443-
context.trigger(
444-
Converter.EVENT_CREATE_DECLARATION,
445-
context.project,
446-
);
447473
moduleContext = context;
448474
} else {
449475
const reflection = context.createDeclarationReflection(

src/lib/converter/factories/index-signature.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ export function convertIndexSignatures(context: Context, symbol: ts.Symbol) {
4646
context.scope.indexSignatures ||= [];
4747
context.scope.indexSignatures.push(index);
4848

49-
context.trigger(
49+
context.converter.trigger(
5050
ConverterEvents.CREATE_SIGNATURE,
51+
context,
5152
index,
5253
indexDeclaration,
5354
);

src/lib/converter/factories/signature.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@ function convertParameters(
211211
paramRefl.comment ||= context.getComment(param, paramRefl.kind);
212212

213213
context.registerReflection(paramRefl, param);
214-
context.trigger(ConverterEvents.CREATE_PARAMETER, paramRefl);
214+
context.converter.trigger(
215+
ConverterEvents.CREATE_PARAMETER,
216+
context,
217+
paramRefl,
218+
);
215219

216220
let type: ts.Type | ts.TypeNode | undefined;
217221
if (declaration) {
@@ -289,7 +293,11 @@ export function convertParameterNodes(
289293
paramRefl,
290294
context.getSymbolAtLocation(param),
291295
);
292-
context.trigger(ConverterEvents.CREATE_PARAMETER, paramRefl);
296+
context.converter.trigger(
297+
ConverterEvents.CREATE_PARAMETER,
298+
context,
299+
paramRefl,
300+
);
293301

294302
paramRefl.type = context.converter.convertType(
295303
context.withScope(paramRefl),
@@ -377,7 +385,11 @@ function convertTypeParameters(
377385
}
378386

379387
context.registerReflection(paramRefl, param.getSymbol());
380-
context.trigger(ConverterEvents.CREATE_TYPE_PARAMETER, paramRefl);
388+
context.converter.trigger(
389+
ConverterEvents.CREATE_TYPE_PARAMETER,
390+
context,
391+
paramRefl,
392+
);
381393

382394
return paramRefl;
383395
});
@@ -418,7 +430,12 @@ export function createTypeParamReflection(
418430
paramRefl.comment = context.getJsDocComment(param.parent);
419431
}
420432

421-
context.trigger(ConverterEvents.CREATE_TYPE_PARAMETER, paramRefl, param);
433+
context.converter.trigger(
434+
ConverterEvents.CREATE_TYPE_PARAMETER,
435+
context,
436+
paramRefl,
437+
param,
438+
);
422439
return paramRefl;
423440
}
424441

@@ -458,8 +475,9 @@ export function convertTemplateParameterNodes(
458475
paramRefl.comment = context.getJsDocComment(param.parent);
459476
}
460477

461-
context.trigger(
478+
context.converter.trigger(
462479
ConverterEvents.CREATE_TYPE_PARAMETER,
480+
context,
463481
paramRefl,
464482
param,
465483
);

src/lib/converter/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { Context } from "./context";
2-
export { Converter } from "./converter";
2+
export { Converter, type ConverterEvents } from "./converter";
33
export type { CommentParserConfig } from "./comments/index";
44
export { convertDefaultValue, convertExpression } from "./convert-expression";
55
export type {

0 commit comments

Comments
 (0)