From ccdaa2c9ac7f630643a353ebcca3df84bacb7e31 Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Sat, 25 Feb 2023 16:17:23 +1300 Subject: [PATCH 1/2] 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 f69fe683e..10f0ff60d 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 12710837d..ea8f6e277 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 9e2453d95..f5e20c72b 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?: EventCallback, context?: any, priority?: number ) { From 15bcd0daed1863f874a60f9583e41d1f29cd7dcf Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Sun, 26 Feb 2023 19:00:34 +1300 Subject: [PATCH 2/2] fix: remove override and adjust event name --- src/lib/application-events.ts | 4 ++-- src/lib/application.ts | 4 ++-- src/lib/utils/events.ts | 11 +---------- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/lib/application-events.ts b/src/lib/application-events.ts index 10f0ff60d..7fc75b25d 100644 --- a/src/lib/application-events.ts +++ b/src/lib/application-events.ts @@ -1,4 +1,4 @@ export const ApplicationEvents = { BOOTSTRAP_END: "bootstrapEnd", - VALIDATION_RUN: "validationRun", -} as const; + VALIDATE_PROJECT: "validateProject", +}; diff --git a/src/lib/application.ts b/src/lib/application.ts index ea8f6e277..16c529d6b 100644 --- a/src/lib/application.ts +++ b/src/lib/application.ts @@ -111,7 +111,7 @@ export class Application extends ChildableComponent< * Emitted when validation is being run. * The listener will be given an instance of {@link ProjectReflection}. */ - static readonly EVENT_VALIDATION_RUN = ApplicationEvents.VALIDATION_RUN; + static readonly EVENT_VALIDATE_PROJECT = ApplicationEvents.VALIDATE_PROJECT; /** * Create a new TypeDoc application instance. @@ -437,7 +437,7 @@ export class Application extends ChildableComponent< validateLinks(project, this.logger); } - this.trigger(Application.EVENT_VALIDATION_RUN, project); + this.trigger(Application.EVENT_VALIDATE_PROJECT, 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 f5e20c72b..9e2453d95 100644 --- a/src/lib/utils/events.ts +++ b/src/lib/utils/events.ts @@ -7,9 +7,6 @@ // 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) { @@ -436,12 +433,6 @@ 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, @@ -450,7 +441,7 @@ export class EventDispatcher { ): this; on( nameOrMap: EventMap | string, - callback?: EventCallback, + callback: EventCallback, context?: any, priority?: number ) {