From d97d3b856ebcbc996bfd8d12b741e709f1ab1f33 Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Sat, 25 Feb 2023 16:17:23 +1300 Subject: [PATCH] feat: add validator run event to allow plugins to perform custom validation --- src/lib/application-events.ts | 3 ++- src/lib/application.ts | 8 ++++++++ src/lib/utils/events.ts | 11 ++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/lib/application-events.ts b/src/lib/application-events.ts index f69fe683e8..10f0ff60dd 100644 --- a/src/lib/application-events.ts +++ b/src/lib/application-events.ts @@ -1,3 +1,4 @@ export const ApplicationEvents = { BOOTSTRAP_END: "bootstrapEnd", -}; + VALIDATION_RUN: "validationRun", +} as const; diff --git a/src/lib/application.ts b/src/lib/application.ts index 12710837d2..ea8f6e2773 100644 --- a/src/lib/application.ts +++ b/src/lib/application.ts @@ -107,6 +107,12 @@ export class Application extends ChildableComponent< */ static readonly EVENT_BOOTSTRAP_END = ApplicationEvents.BOOTSTRAP_END; + /** + * Emitted when validation is being run. + * The listener will be given an instance of {@link ProjectReflection}. + */ + static readonly EVENT_VALIDATION_RUN = ApplicationEvents.VALIDATION_RUN; + /** * Create a new TypeDoc application instance. */ @@ -431,6 +437,8 @@ export class Application extends ChildableComponent< validateLinks(project, this.logger); } + this.trigger(Application.EVENT_VALIDATION_RUN, project); + this.logger.verbose(`Validation took ${Date.now() - start}ms`); } diff --git a/src/lib/utils/events.ts b/src/lib/utils/events.ts index 9e2453d95e..6e0798c3f3 100644 --- a/src/lib/utils/events.ts +++ b/src/lib/utils/events.ts @@ -7,6 +7,9 @@ // The Events object is a typesafe conversion of Backbones Events object: // https://github.com/jashkenas/backbone/blob/05fde9e201f7e2137796663081105cd6dad12a98/backbone.js#L119-L374 +import type { ProjectReflection } from "../models/index"; +import type { Application } from "../application"; + const uniqueId = (function () { const prefixes: Record = Object.create(null); return function (prefix: string) { @@ -433,6 +436,12 @@ export class EventDispatcher { context?: any, priority?: number ): this; + on( + name: (typeof Application)["EVENT_VALIDATION_RUN"], + callback?: (project: ProjectReflection) => void, + context?: any, + priority?: number + ): this; on( name: string, callback: EventCallback, @@ -441,7 +450,7 @@ export class EventDispatcher { ): this; on( nameOrMap: EventMap | string, - callback: EventCallback, + callback: () => void, context?: any, priority?: number ) {