From 56a0a7d3a3ed2532b81b18323e960a3e4767e887 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 16:00:14 +0530 Subject: [PATCH 01/21] feat: Migrate index.js, createHooks.js, createAddHook.js to TS --- .../{createAddHook.js => createAddHook.ts} | 39 +++++++++++------ .../src/{createHooks.js => createHooks.ts} | 32 +++++++++++--- packages/hooks/src/{index.js => index.ts} | 43 ++++++++----------- 3 files changed, 69 insertions(+), 45 deletions(-) rename packages/hooks/src/{createAddHook.js => createAddHook.ts} (74%) rename packages/hooks/src/{createHooks.js => createHooks.ts} (61%) rename packages/hooks/src/{index.js => index.ts} (53%) diff --git a/packages/hooks/src/createAddHook.js b/packages/hooks/src/createAddHook.ts similarity index 74% rename from packages/hooks/src/createAddHook.js rename to packages/hooks/src/createAddHook.ts index 1fcfcfab1a7d21..9ca986069dec94 100644 --- a/packages/hooks/src/createAddHook.js +++ b/packages/hooks/src/createAddHook.ts @@ -3,27 +3,39 @@ */ import validateNamespace from './validateNamespace.js'; import validateHookName from './validateHookName.js'; +import type { Callback, Hooks, StoreKey } from './index.js'; /** - * @callback AddHook * * Adds the hook to the appropriate hooks container. - * - * @param {string} hookName Name of hook to add - * @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`. - * @param {import('.').Callback} callback Function to call when the hook is run - * @param {number} [priority=10] Priority of this hook */ +export type AddHook = ( + hookName: string, + namespace: string, + callback: Callback, + priority?: number +) => void; + +export type Handler = { + callback: Callback; + priority: number; + namespace: string; +}; + +export type HookInfo = { + name: string; + currentIndex: number; +}; /** * Returns a function which, when invoked, will add a hook. * - * @param {import('.').Hooks} hooks Hooks instance. - * @param {import('.').StoreKey} storeKey + * @param hooks Hooks instance. + * @param storeKey * - * @return {AddHook} Function that adds a new hook. + * @return Function that adds a new hook. */ -function createAddHook( hooks, storeKey ) { +function createAddHook( hooks: Hooks, storeKey: StoreKey ): AddHook { return function addHook( hookName, namespace, callback, priority = 10 ) { const hooksStore = hooks[ storeKey ]; @@ -50,14 +62,13 @@ function createAddHook( hooks, storeKey ) { return; } - const handler = { callback, priority, namespace }; + const handler: Handler = { callback, priority, namespace }; if ( hooksStore[ hookName ] ) { // Find the correct insert index of the new hook. const handlers = hooksStore[ hookName ].handlers; - /** @type {number} */ - let i; + let i: number; for ( i = handlers.length; i > 0; i-- ) { if ( priority >= handlers[ i - 1 ].priority ) { break; @@ -76,7 +87,7 @@ function createAddHook( hooks, storeKey ) { // we're adding would come after the current callback, there's no // problem; otherwise we need to increase the execution index of // any other runs by 1 to account for the added element. - hooksStore.__current.forEach( ( hookInfo ) => { + hooksStore.__current.forEach( ( hookInfo: HookInfo ) => { if ( hookInfo.name === hookName && hookInfo.currentIndex >= i diff --git a/packages/hooks/src/createHooks.js b/packages/hooks/src/createHooks.ts similarity index 61% rename from packages/hooks/src/createHooks.js rename to packages/hooks/src/createHooks.ts index 1f9b1a8206b020..6ff61b5d60fa22 100644 --- a/packages/hooks/src/createHooks.js +++ b/packages/hooks/src/createHooks.ts @@ -9,6 +9,8 @@ import createCurrentHook from './createCurrentHook'; import createDoingHook from './createDoingHook'; import createDidHook from './createDidHook'; +import type { Store } from '.'; + /** * Internal class for constructing hooks. Use `createHooks()` function * @@ -17,12 +19,32 @@ import createDidHook from './createDidHook'; * @private */ export class _Hooks { + public actions: Store; + public filters: Store; + + public addAction: ReturnType< typeof createAddHook >; + public addFilter: ReturnType< typeof createAddHook >; + public removeAction: ReturnType< typeof createRemoveHook >; + public removeFilter: ReturnType< typeof createRemoveHook >; + public hasAction: ReturnType< typeof createHasHook >; + public hasFilter: ReturnType< typeof createHasHook >; + public removeAllActions: ReturnType< typeof createRemoveHook >; + public removeAllFilters: ReturnType< typeof createRemoveHook >; + public doAction: ReturnType< typeof createRunHook >; + public doActionAsync: ReturnType< typeof createRunHook >; + public applyFilters: ReturnType< typeof createRunHook >; + public applyFiltersAsync: ReturnType< typeof createRunHook >; + public currentAction: ReturnType< typeof createCurrentHook >; + public currentFilter: ReturnType< typeof createCurrentHook >; + public doingAction: ReturnType< typeof createDoingHook >; + public doingFilter: ReturnType< typeof createDoingHook >; + public didAction: ReturnType< typeof createDidHook >; + public didFilter: ReturnType< typeof createDidHook >; + constructor() { - /** @type {import('.').Store} actions */ this.actions = Object.create( null ); this.actions.__current = new Set(); - /** @type {import('.').Store} filters */ this.filters = Object.create( null ); this.filters.__current = new Set(); @@ -47,14 +69,14 @@ export class _Hooks { } } -/** @typedef {_Hooks} Hooks */ +export type Hooks = _Hooks; /** * Returns an instance of the hooks object. * - * @return {Hooks} A Hooks instance. + * @return A Hooks instance. */ -function createHooks() { +function createHooks(): Hooks { return new _Hooks(); } diff --git a/packages/hooks/src/index.js b/packages/hooks/src/index.ts similarity index 53% rename from packages/hooks/src/index.js rename to packages/hooks/src/index.ts index 1d13397e406c6b..9a6e6ad68027fa 100644 --- a/packages/hooks/src/index.js +++ b/packages/hooks/src/index.ts @@ -3,38 +3,29 @@ */ import createHooks from './createHooks'; -/** @typedef {(...args: any[])=>any} Callback */ +export type Callback = ( ...args: any[] ) => any; -/** - * @typedef Handler - * @property {Callback} callback The callback - * @property {string} namespace The namespace - * @property {number} priority The namespace - */ +export type Handler = { + callback: Callback; + namespace: string; + priority: number; +}; -/** - * @typedef Hook - * @property {Handler[]} handlers Array of handlers - * @property {number} runs Run counter - */ +export type Hook = { + handlers: Handler[]; + runs: number; +}; -/** - * @typedef Current - * @property {string} name Hook name - * @property {number} currentIndex The index - */ +export type Current = { + name: string; + currentIndex: number; +}; -/** - * @typedef {Record & {__current: Set}} Store - */ +export type Store = Record< string, Hook > & { __current: Set< Current > }; -/** - * @typedef {'actions' | 'filters'} StoreKey - */ +export type StoreKey = 'actions' | 'filters'; -/** - * @typedef {import('./createHooks').Hooks} Hooks - */ +export type Hooks = import('./createHooks').Hooks; export const defaultHooks = createHooks(); From 4b730f12f7081a8f655c8dca688fe1050a63c51f Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 16:02:05 +0530 Subject: [PATCH 02/21] feat: Convert createCurrentHook.js to TS --- ...createCurrentHook.js => createCurrentHook.ts} | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) rename packages/hooks/src/{createCurrentHook.js => createCurrentHook.ts} (58%) diff --git a/packages/hooks/src/createCurrentHook.js b/packages/hooks/src/createCurrentHook.ts similarity index 58% rename from packages/hooks/src/createCurrentHook.js rename to packages/hooks/src/createCurrentHook.ts index 3ada0322496004..a4f210b105f7b6 100644 --- a/packages/hooks/src/createCurrentHook.js +++ b/packages/hooks/src/createCurrentHook.ts @@ -1,14 +1,22 @@ +/** + * Internal dependencies + */ +import type { Hooks, StoreKey } from '.'; + /** * Returns a function which, when invoked, will return the name of the * currently running hook, or `null` if no hook of the given type is currently * running. * - * @param {import('.').Hooks} hooks Hooks instance. - * @param {import('.').StoreKey} storeKey + * @param hooks Hooks instance. + * @param storeKey * - * @return {() => string | null} Function that returns the current hook name or null. + * @return Function that returns the current hook name or null. */ -function createCurrentHook( hooks, storeKey ) { +function createCurrentHook( + hooks: Hooks, + storeKey: StoreKey +): () => string | null { return function currentHook() { const hooksStore = hooks[ storeKey ]; const currentArray = Array.from( hooksStore.__current ); From 96a3ed355671862da027a46e16d70421a5d05f25 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 16:04:32 +0530 Subject: [PATCH 03/21] feat: Migrate the createDidHook.js to TS --- .../src/{createDidHook.js => createDidHook.ts} | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) rename packages/hooks/src/{createDidHook.js => createDidHook.ts} (61%) diff --git a/packages/hooks/src/createDidHook.js b/packages/hooks/src/createDidHook.ts similarity index 61% rename from packages/hooks/src/createDidHook.js rename to packages/hooks/src/createDidHook.ts index 882ced1760c131..310a811bf810fa 100644 --- a/packages/hooks/src/createDidHook.js +++ b/packages/hooks/src/createDidHook.ts @@ -2,27 +2,25 @@ * Internal dependencies */ import validateHookName from './validateHookName.js'; +import type { Hooks, StoreKey } from '.'; /** - * @callback DidHook * * Returns the number of times an action has been fired. * - * @param {string} hookName The hook name to check. - * - * @return {number | undefined} The number of times the hook has run. */ +export type DidHook = ( hookName: string ) => number | undefined; /** * Returns a function which, when invoked, will return the number of times a * hook has been called. * - * @param {import('.').Hooks} hooks Hooks instance. - * @param {import('.').StoreKey} storeKey + * @param hooks Hooks instance. + * @param storeKey * - * @return {DidHook} Function that returns a hook's call count. + * @return Function that returns a hook's call count. */ -function createDidHook( hooks, storeKey ) { +function createDidHook( hooks: Hooks, storeKey: StoreKey ): DidHook { return function didHook( hookName ) { const hooksStore = hooks[ storeKey ]; From f2c674971ba1c7e6d04febf3d2457f59bb3cbd40 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 16:07:49 +0530 Subject: [PATCH 04/21] feat: Migrate createDoingHook.ts to TS --- .../{createDoingHook.js => createDoingHook.ts} | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) rename packages/hooks/src/{createDoingHook.js => createDoingHook.ts} (65%) diff --git a/packages/hooks/src/createDoingHook.js b/packages/hooks/src/createDoingHook.ts similarity index 65% rename from packages/hooks/src/createDoingHook.js rename to packages/hooks/src/createDoingHook.ts index 9fccf38171f332..640df875ae002d 100644 --- a/packages/hooks/src/createDoingHook.js +++ b/packages/hooks/src/createDoingHook.ts @@ -1,24 +1,24 @@ /** - * @callback DoingHook + * Internal dependencies + */ +import type { Hooks, StoreKey } from '.'; +/** * Returns whether a hook is currently being executed. * - * @param {string} [hookName] The name of the hook to check for. If - * omitted, will check for any hook being executed. - * - * @return {boolean} Whether the hook is being executed. */ +export type DoingHook = ( hookName?: string ) => boolean; /** * Returns a function which, when invoked, will return whether a hook is * currently being executed. * - * @param {import('.').Hooks} hooks Hooks instance. - * @param {import('.').StoreKey} storeKey + * @param hooks Hooks instance. + * @param storeKey * * @return {DoingHook} Function that returns whether a hook is currently * being executed. */ -function createDoingHook( hooks, storeKey ) { +function createDoingHook( hooks: Hooks, storeKey: StoreKey ): DoingHook { return function doingHook( hookName ) { const hooksStore = hooks[ storeKey ]; From e18ab7a5d67161fe1985ab9a9cdcb61e2a9f6781 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 16:08:16 +0530 Subject: [PATCH 05/21] feat: Migrate createDoingHook.ts to TS --- packages/hooks/src/createDoingHook.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/hooks/src/createDoingHook.ts b/packages/hooks/src/createDoingHook.ts index 640df875ae002d..fe49809a0345aa 100644 --- a/packages/hooks/src/createDoingHook.ts +++ b/packages/hooks/src/createDoingHook.ts @@ -12,10 +12,10 @@ export type DoingHook = ( hookName?: string ) => boolean; * Returns a function which, when invoked, will return whether a hook is * currently being executed. * - * @param hooks Hooks instance. - * @param storeKey + * @param hooks Hooks instance. + * @param storeKey * - * @return {DoingHook} Function that returns whether a hook is currently + * @return Function that returns whether a hook is currently * being executed. */ function createDoingHook( hooks: Hooks, storeKey: StoreKey ): DoingHook { From e74acdfbebd68be518968d01e0f9964c348b69c0 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 16:08:31 +0530 Subject: [PATCH 06/21] feat: Migrate createDoingHook.ts to TS --- packages/hooks/src/createDoingHook.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/hooks/src/createDoingHook.ts b/packages/hooks/src/createDoingHook.ts index fe49809a0345aa..d644d4ef5e5cff 100644 --- a/packages/hooks/src/createDoingHook.ts +++ b/packages/hooks/src/createDoingHook.ts @@ -2,6 +2,7 @@ * Internal dependencies */ import type { Hooks, StoreKey } from '.'; + /** * Returns whether a hook is currently being executed. * From 33646b7d3ba4fdeba83e9023dfd456e55ed2ae16 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 17:20:37 +0530 Subject: [PATCH 07/21] feat: Migrate the createHasHook.js to TS --- .../{createHasHook.js => createHasHook.ts} | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) rename packages/hooks/src/{createHasHook.js => createHasHook.ts} (55%) diff --git a/packages/hooks/src/createHasHook.js b/packages/hooks/src/createHasHook.ts similarity index 55% rename from packages/hooks/src/createHasHook.js rename to packages/hooks/src/createHasHook.ts index 5ed82127f2cc77..2ae994def1e8b0 100644 --- a/packages/hooks/src/createHasHook.js +++ b/packages/hooks/src/createHasHook.ts @@ -1,25 +1,24 @@ /** - * @callback HasHook + * Internal dependencies + */ +import type { Hooks, StoreKey } from '.'; +/** * * Returns whether any handlers are attached for the given hookName and optional namespace. - * - * @param {string} hookName The name of the hook to check for. - * @param {string} [namespace] Optional. The unique namespace identifying the callback - * in the form `vendor/plugin/function`. - * - * @return {boolean} Whether there are handlers that are attached to the given hook. */ +export type HasHook = ( hookname: string, namespace?: string ) => boolean; + /** * Returns a function which, when invoked, will return whether any handlers are * attached to a particular hook. * - * @param {import('.').Hooks} hooks Hooks instance. - * @param {import('.').StoreKey} storeKey + * @param hooks Hooks instance. + * @param storeKey * - * @return {HasHook} Function that returns whether any handlers are + * @return Function that returns whether any handlers are * attached to a particular hook and optional namespace. */ -function createHasHook( hooks, storeKey ) { +function createHasHook( hooks: Hooks, storeKey: StoreKey ): HasHook { return function hasHook( hookName, namespace ) { const hooksStore = hooks[ storeKey ]; From 639653de85b426a753d6d2aefc5b0298f464b445 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 17:23:41 +0530 Subject: [PATCH 08/21] feat: Migrate createRemoveHook.js to TS --- ...reateRemoveHook.js => createRemoveHook.ts} | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) rename packages/hooks/src/{createRemoveHook.js => createRemoveHook.ts} (70%) diff --git a/packages/hooks/src/createRemoveHook.js b/packages/hooks/src/createRemoveHook.ts similarity index 70% rename from packages/hooks/src/createRemoveHook.js rename to packages/hooks/src/createRemoveHook.ts index 2719d83074d65c..f99046edb3b58b 100644 --- a/packages/hooks/src/createRemoveHook.js +++ b/packages/hooks/src/createRemoveHook.ts @@ -1,3 +1,8 @@ +/** + * Internal dependencies + */ +import type { Hooks, StoreKey } from '.'; + /** * Internal dependencies */ @@ -5,30 +10,31 @@ import validateNamespace from './validateNamespace.js'; import validateHookName from './validateHookName.js'; /** - * @callback RemoveHook * Removes the specified callback (or all callbacks) from the hook with a given hookName * and namespace. - * - * @param {string} hookName The name of the hook to modify. - * @param {string} namespace The unique namespace identifying the callback in the - * form `vendor/plugin/function`. - * - * @return {number | undefined} The number of callbacks removed. */ +export type RemoveHook = ( + hookName: string, + namespace: string +) => number | undefined; /** * Returns a function which, when invoked, will remove a specified hook or all * hooks by the given name. * - * @param {import('.').Hooks} hooks Hooks instance. - * @param {import('.').StoreKey} storeKey - * @param {boolean} [removeAll=false] Whether to remove all callbacks for a hookName, - * without regard to namespace. Used to create - * `removeAll*` functions. + * @param hooks Hooks instance. + * @param storeKey + * @param [removeAll=false] Whether to remove all callbacks for a hookName, + * without regard to namespace. Used to create + * `removeAll*` functions. * - * @return {RemoveHook} Function that removes hooks. + * @return Function that removes hooks. */ -function createRemoveHook( hooks, storeKey, removeAll = false ) { +function createRemoveHook( + hooks: Hooks, + storeKey: StoreKey, + removeAll: boolean = false +): RemoveHook { return function removeHook( hookName, namespace ) { const hooksStore = hooks[ storeKey ]; From 0dde6c7f201d09a132211e1d3261f9922e951c9b Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 17:31:36 +0530 Subject: [PATCH 09/21] feat: Migrate createRunHook.js to TS --- .../{createRunHook.js => createRunHook.ts} | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) rename packages/hooks/src/{createRunHook.js => createRunHook.ts} (78%) diff --git a/packages/hooks/src/createRunHook.js b/packages/hooks/src/createRunHook.ts similarity index 78% rename from packages/hooks/src/createRunHook.js rename to packages/hooks/src/createRunHook.ts index f2a56dbdc0d717..62077181a01f0c 100644 --- a/packages/hooks/src/createRunHook.js +++ b/packages/hooks/src/createRunHook.ts @@ -1,16 +1,31 @@ +/** + * Internal dependencies + */ +import type { Hooks, StoreKey } from '.'; + +export type RunHook = ( + hookName: string, + ...args: unknown[] +) => undefined | unknown; + /** * Returns a function which, when invoked, will execute all callbacks * registered to a hook of the specified type, optionally returning the final * value of the call chain. * - * @param {import('.').Hooks} hooks Hooks instance. - * @param {import('.').StoreKey} storeKey - * @param {boolean} returnFirstArg Whether each hook callback is expected to return its first argument. - * @param {boolean} async Whether the hook callback should be run asynchronously + * @param hooks Hooks instance. + * @param storeKey + * @param returnFirstArg Whether each hook callback is expected to return its first argument. + * @param async Whether the hook callback should be run asynchronously * - * @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks. + * @return Function that runs hook callbacks. */ -function createRunHook( hooks, storeKey, returnFirstArg, async ) { +function createRunHook( + hooks: Hooks, + storeKey: StoreKey, + returnFirstArg: boolean, + async: boolean +): RunHook { return function runHook( hookName, ...args ) { const hooksStore = hooks[ storeKey ]; From 0d6361322444864f9621aac383f782ac9bb5b780 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 17:33:33 +0530 Subject: [PATCH 10/21] feat: Migrate validateHookName.js to TS --- .../src/{validateHookName.js => validateHookName.ts} | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) rename packages/hooks/src/{validateHookName.js => validateHookName.ts} (65%) diff --git a/packages/hooks/src/validateHookName.js b/packages/hooks/src/validateHookName.ts similarity index 65% rename from packages/hooks/src/validateHookName.js rename to packages/hooks/src/validateHookName.ts index 03409384dab599..9bf5f354532921 100644 --- a/packages/hooks/src/validateHookName.js +++ b/packages/hooks/src/validateHookName.ts @@ -1,13 +1,13 @@ /** * Validate a hookName string. * - * @param {string} hookName The hook name to validate. Should be a non empty string containing - * only numbers, letters, dashes, periods and underscores. Also, - * the hook name cannot begin with `__`. + * @param hookName The hook name to validate. Should be a non empty string containing + * only numbers, letters, dashes, periods and underscores. Also, + * the hook name cannot begin with `__`. * - * @return {boolean} Whether the hook name is valid. + * @return Whether the hook name is valid. */ -function validateHookName( hookName ) { +function validateHookName( hookName: string ): boolean { if ( 'string' !== typeof hookName || '' === hookName ) { // eslint-disable-next-line no-console console.error( 'The hook name must be a non-empty string.' ); From 70405b6577ea4faec55934ab78a12943da3c0b8a Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 17:34:24 +0530 Subject: [PATCH 11/21] feat: Migrate validateNamespace.js to TS --- .../src/{validateNamespace.js => validateNamespace.ts} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename packages/hooks/src/{validateNamespace.js => validateNamespace.ts} (69%) diff --git a/packages/hooks/src/validateNamespace.js b/packages/hooks/src/validateNamespace.ts similarity index 69% rename from packages/hooks/src/validateNamespace.js rename to packages/hooks/src/validateNamespace.ts index fdf451e1053d21..11c1ff23b7c21a 100644 --- a/packages/hooks/src/validateNamespace.js +++ b/packages/hooks/src/validateNamespace.ts @@ -1,12 +1,12 @@ /** * Validate a namespace string. * - * @param {string} namespace The namespace to validate - should take the form - * `vendor/plugin/function`. + * @param namespace The namespace to validate - should take the form + * `vendor/plugin/function`. * - * @return {boolean} Whether the namespace is valid. + * @return Whether the namespace is valid. */ -function validateNamespace( namespace ) { +function validateNamespace( namespace: string ): boolean { if ( 'string' !== typeof namespace || '' === namespace ) { // eslint-disable-next-line no-console console.error( 'The namespace must be a non-empty string.' ); From 47f2321f3e2962e61140f1a1d9e0fcf1c9f40f2e Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 17:37:56 +0530 Subject: [PATCH 12/21] fix: Internal dependencies imports for JS converted to TS --- packages/hooks/src/createAddHook.ts | 6 +++--- packages/hooks/src/createDidHook.ts | 2 +- packages/hooks/src/createHooks.ts | 1 - packages/hooks/src/createRemoveHook.ts | 8 ++------ 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/hooks/src/createAddHook.ts b/packages/hooks/src/createAddHook.ts index 9ca986069dec94..f78ed42bfb0bc3 100644 --- a/packages/hooks/src/createAddHook.ts +++ b/packages/hooks/src/createAddHook.ts @@ -1,9 +1,9 @@ /** * Internal dependencies */ -import validateNamespace from './validateNamespace.js'; -import validateHookName from './validateHookName.js'; -import type { Callback, Hooks, StoreKey } from './index.js'; +import validateNamespace from './validateNamespace'; +import validateHookName from './validateHookName'; +import type { Callback, Hooks, StoreKey } from '.'; /** * diff --git a/packages/hooks/src/createDidHook.ts b/packages/hooks/src/createDidHook.ts index 310a811bf810fa..97c6dfdff54f04 100644 --- a/packages/hooks/src/createDidHook.ts +++ b/packages/hooks/src/createDidHook.ts @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import validateHookName from './validateHookName.js'; +import validateHookName from './validateHookName'; import type { Hooks, StoreKey } from '.'; /** diff --git a/packages/hooks/src/createHooks.ts b/packages/hooks/src/createHooks.ts index 6ff61b5d60fa22..4c9f3838096c5c 100644 --- a/packages/hooks/src/createHooks.ts +++ b/packages/hooks/src/createHooks.ts @@ -8,7 +8,6 @@ import createRunHook from './createRunHook'; import createCurrentHook from './createCurrentHook'; import createDoingHook from './createDoingHook'; import createDidHook from './createDidHook'; - import type { Store } from '.'; /** diff --git a/packages/hooks/src/createRemoveHook.ts b/packages/hooks/src/createRemoveHook.ts index f99046edb3b58b..36e2878c97d6ab 100644 --- a/packages/hooks/src/createRemoveHook.ts +++ b/packages/hooks/src/createRemoveHook.ts @@ -1,14 +1,10 @@ /** * Internal dependencies */ +import validateNamespace from './validateNamespace'; +import validateHookName from './validateHookName'; import type { Hooks, StoreKey } from '.'; -/** - * Internal dependencies - */ -import validateNamespace from './validateNamespace.js'; -import validateHookName from './validateHookName.js'; - /** * Removes the specified callback (or all callbacks) from the hook with a given hookName * and namespace. From d8b0a6b08672c1e11bf3a9057d0a31602b49cde8 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 20 Jun 2025 17:44:05 +0530 Subject: [PATCH 13/21] fix: Hooks import in index.ts file for Hooks type import from createHooks.ts --- packages/hooks/src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/hooks/src/index.ts b/packages/hooks/src/index.ts index 9a6e6ad68027fa..18aa22523edd29 100644 --- a/packages/hooks/src/index.ts +++ b/packages/hooks/src/index.ts @@ -1,6 +1,7 @@ /** * Internal dependencies */ +import type { Hooks as internalHooks } from './createHooks'; import createHooks from './createHooks'; export type Callback = ( ...args: any[] ) => any; @@ -25,7 +26,7 @@ export type Store = Record< string, Hook > & { __current: Set< Current > }; export type StoreKey = 'actions' | 'filters'; -export type Hooks = import('./createHooks').Hooks; +export type Hooks = internalHooks; export const defaultHooks = createHooks(); From c20bb22ce19e7cb95da34f4104a9a9e5a602f74a Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 4 Jul 2025 15:11:22 +0530 Subject: [PATCH 14/21] chore: Retain comments for AddHook Type --- packages/hooks/src/createAddHook.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/hooks/src/createAddHook.ts b/packages/hooks/src/createAddHook.ts index f78ed42bfb0bc3..3ea0bb9a58ce65 100644 --- a/packages/hooks/src/createAddHook.ts +++ b/packages/hooks/src/createAddHook.ts @@ -10,9 +10,21 @@ import type { Callback, Hooks, StoreKey } from '.'; * Adds the hook to the appropriate hooks container. */ export type AddHook = ( + /** + * Name of hook to add + */ hookName: string, + /** + * The unique namespace identifying the callback in the form. + */ namespace: string, + /** + * Function to call when the hook is run. + */ callback: Callback, + /** + * Priority of this hook + */ priority?: number ) => void; From ce884843bc24c04ac9fcbd81b4e60d3ca609380a Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 4 Jul 2025 15:15:13 +0530 Subject: [PATCH 15/21] chore: Retain DidHook type's comment --- packages/hooks/src/createDidHook.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/hooks/src/createDidHook.ts b/packages/hooks/src/createDidHook.ts index 97c6dfdff54f04..f917532e802951 100644 --- a/packages/hooks/src/createDidHook.ts +++ b/packages/hooks/src/createDidHook.ts @@ -9,7 +9,12 @@ import type { Hooks, StoreKey } from '.'; * Returns the number of times an action has been fired. * */ -export type DidHook = ( hookName: string ) => number | undefined; +export type DidHook = ( + /** + * The hook name to check. + */ + hookName: string +) => number | undefined; /** * Returns a function which, when invoked, will return the number of times a From 2f4449c0d53ca15471e25f8628eebe3bbca935bf Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 4 Jul 2025 15:16:52 +0530 Subject: [PATCH 16/21] chore: Retain DoingHook Type comment --- packages/hooks/src/createDoingHook.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/hooks/src/createDoingHook.ts b/packages/hooks/src/createDoingHook.ts index d644d4ef5e5cff..37612783e01890 100644 --- a/packages/hooks/src/createDoingHook.ts +++ b/packages/hooks/src/createDoingHook.ts @@ -7,7 +7,12 @@ import type { Hooks, StoreKey } from '.'; * Returns whether a hook is currently being executed. * */ -export type DoingHook = ( hookName?: string ) => boolean; +export type DoingHook = ( + /** + * The name of the hook to check for. + * If omitted, will check for any hook being executed. + */ hookName?: string +) => boolean; /** * Returns a function which, when invoked, will return whether a hook is From 72e75e951865666122d990dae130b09c7261901b Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 4 Jul 2025 15:21:23 +0530 Subject: [PATCH 17/21] chore: Retain HasHook Type comment --- packages/hooks/src/createHasHook.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/hooks/src/createHasHook.ts b/packages/hooks/src/createHasHook.ts index 2ae994def1e8b0..e3bbf65ac05703 100644 --- a/packages/hooks/src/createHasHook.ts +++ b/packages/hooks/src/createHasHook.ts @@ -6,7 +6,16 @@ import type { Hooks, StoreKey } from '.'; * * Returns whether any handlers are attached for the given hookName and optional namespace. */ -export type HasHook = ( hookname: string, namespace?: string ) => boolean; +export type HasHook = ( + /** + * The name of the hook to check for. + */ + hookname: string, + /** + * The unique namespace identifying the callback in the form `vendor/plugin/function`. + */ + namespace?: string +) => boolean; /** * Returns a function which, when invoked, will return whether any handlers are From 01a65437915e466d76801a565a16fdad489899a0 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 4 Jul 2025 15:24:31 +0530 Subject: [PATCH 18/21] chore: Retain RemoveHook Type comment --- packages/hooks/src/createRemoveHook.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/hooks/src/createRemoveHook.ts b/packages/hooks/src/createRemoveHook.ts index 36e2878c97d6ab..9910fc0ff1ec62 100644 --- a/packages/hooks/src/createRemoveHook.ts +++ b/packages/hooks/src/createRemoveHook.ts @@ -10,7 +10,14 @@ import type { Hooks, StoreKey } from '.'; * and namespace. */ export type RemoveHook = ( + /** + * The name of the hook to modify. + */ hookName: string, + /** + * The unique namespace identifying the callback in the + * form `vendor/plugin/function`. + */ namespace: string ) => number | undefined; From d0468bb3e04229b64840129e4e540a568ceb34f4 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 4 Jul 2025 15:26:52 +0530 Subject: [PATCH 19/21] chore: Retain RemoveHook Type comment --- packages/hooks/src/createRemoveHook.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/hooks/src/createRemoveHook.ts b/packages/hooks/src/createRemoveHook.ts index 9910fc0ff1ec62..2e14336120ddbf 100644 --- a/packages/hooks/src/createRemoveHook.ts +++ b/packages/hooks/src/createRemoveHook.ts @@ -15,8 +15,7 @@ export type RemoveHook = ( */ hookName: string, /** - * The unique namespace identifying the callback in the - * form `vendor/plugin/function`. + * The unique namespace identifying the callback in the form `vendor/plugin/function`. */ namespace: string ) => number | undefined; From d8c8cb9917aaeb4f4365059663e7b9f88457b4d5 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Fri, 4 Jul 2025 15:36:33 +0530 Subject: [PATCH 20/21] chore: Shift all the types from index.ts to types.ts --- packages/hooks/src/index.ts | 25 +------------------------ packages/hooks/src/types.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 packages/hooks/src/types.ts diff --git a/packages/hooks/src/index.ts b/packages/hooks/src/index.ts index 18aa22523edd29..b9684276c6f1c5 100644 --- a/packages/hooks/src/index.ts +++ b/packages/hooks/src/index.ts @@ -1,32 +1,9 @@ /** * Internal dependencies */ -import type { Hooks as internalHooks } from './createHooks'; import createHooks from './createHooks'; -export type Callback = ( ...args: any[] ) => any; - -export type Handler = { - callback: Callback; - namespace: string; - priority: number; -}; - -export type Hook = { - handlers: Handler[]; - runs: number; -}; - -export type Current = { - name: string; - currentIndex: number; -}; - -export type Store = Record< string, Hook > & { __current: Set< Current > }; - -export type StoreKey = 'actions' | 'filters'; - -export type Hooks = internalHooks; +export * from './types'; export const defaultHooks = createHooks(); diff --git a/packages/hooks/src/types.ts b/packages/hooks/src/types.ts new file mode 100644 index 00000000000000..9365da351ed6df --- /dev/null +++ b/packages/hooks/src/types.ts @@ -0,0 +1,28 @@ +/** + * Internal dependencies + */ +import type { Hooks as internalHooks } from './createHooks'; + +export type Callback = ( ...args: any[] ) => any; + +export type Handler = { + callback: Callback; + namespace: string; + priority: number; +}; + +export type Hook = { + handlers: Handler[]; + runs: number; +}; + +export type Current = { + name: string; + currentIndex: number; +}; + +export type Store = Record< string, Hook > & { __current: Set< Current > }; + +export type StoreKey = 'actions' | 'filters'; + +export type Hooks = internalHooks; From 17b97a09c4d0ea294fb3f2c07e8ee9ef565f9383 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Thu, 10 Jul 2025 18:28:25 +0530 Subject: [PATCH 21/21] fix: Use type imports from the types.ts --- packages/hooks/src/createAddHook.ts | 12 +----------- packages/hooks/src/createCurrentHook.ts | 2 +- packages/hooks/src/createDidHook.ts | 2 +- packages/hooks/src/createDoingHook.ts | 2 +- packages/hooks/src/createHasHook.ts | 2 +- packages/hooks/src/createHooks.ts | 2 +- packages/hooks/src/createRemoveHook.ts | 2 +- packages/hooks/src/createRunHook.ts | 2 +- packages/hooks/src/types.ts | 9 ++++++--- 9 files changed, 14 insertions(+), 21 deletions(-) diff --git a/packages/hooks/src/createAddHook.ts b/packages/hooks/src/createAddHook.ts index 3ea0bb9a58ce65..acc6ad555058c7 100644 --- a/packages/hooks/src/createAddHook.ts +++ b/packages/hooks/src/createAddHook.ts @@ -4,6 +4,7 @@ import validateNamespace from './validateNamespace'; import validateHookName from './validateHookName'; import type { Callback, Hooks, StoreKey } from '.'; +import type { Handler, HookInfo } from './types'; /** * @@ -28,17 +29,6 @@ export type AddHook = ( priority?: number ) => void; -export type Handler = { - callback: Callback; - priority: number; - namespace: string; -}; - -export type HookInfo = { - name: string; - currentIndex: number; -}; - /** * Returns a function which, when invoked, will add a hook. * diff --git a/packages/hooks/src/createCurrentHook.ts b/packages/hooks/src/createCurrentHook.ts index a4f210b105f7b6..60cfffc99fd609 100644 --- a/packages/hooks/src/createCurrentHook.ts +++ b/packages/hooks/src/createCurrentHook.ts @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import type { Hooks, StoreKey } from '.'; +import type { Hooks, StoreKey } from './types'; /** * Returns a function which, when invoked, will return the name of the diff --git a/packages/hooks/src/createDidHook.ts b/packages/hooks/src/createDidHook.ts index f917532e802951..e8ef6f5aed77d5 100644 --- a/packages/hooks/src/createDidHook.ts +++ b/packages/hooks/src/createDidHook.ts @@ -2,7 +2,7 @@ * Internal dependencies */ import validateHookName from './validateHookName'; -import type { Hooks, StoreKey } from '.'; +import type { Hooks, StoreKey } from './types'; /** * diff --git a/packages/hooks/src/createDoingHook.ts b/packages/hooks/src/createDoingHook.ts index 37612783e01890..85fbf442d63490 100644 --- a/packages/hooks/src/createDoingHook.ts +++ b/packages/hooks/src/createDoingHook.ts @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import type { Hooks, StoreKey } from '.'; +import type { Hooks, StoreKey } from './types'; /** * Returns whether a hook is currently being executed. diff --git a/packages/hooks/src/createHasHook.ts b/packages/hooks/src/createHasHook.ts index e3bbf65ac05703..73a217fcbe7ff5 100644 --- a/packages/hooks/src/createHasHook.ts +++ b/packages/hooks/src/createHasHook.ts @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import type { Hooks, StoreKey } from '.'; +import type { Hooks, StoreKey } from './types'; /** * * Returns whether any handlers are attached for the given hookName and optional namespace. diff --git a/packages/hooks/src/createHooks.ts b/packages/hooks/src/createHooks.ts index 4c9f3838096c5c..e114cc3849bf4d 100644 --- a/packages/hooks/src/createHooks.ts +++ b/packages/hooks/src/createHooks.ts @@ -8,7 +8,7 @@ import createRunHook from './createRunHook'; import createCurrentHook from './createCurrentHook'; import createDoingHook from './createDoingHook'; import createDidHook from './createDidHook'; -import type { Store } from '.'; +import type { Store } from './types'; /** * Internal class for constructing hooks. Use `createHooks()` function diff --git a/packages/hooks/src/createRemoveHook.ts b/packages/hooks/src/createRemoveHook.ts index 2e14336120ddbf..55b4b9dbcf8ce6 100644 --- a/packages/hooks/src/createRemoveHook.ts +++ b/packages/hooks/src/createRemoveHook.ts @@ -3,7 +3,7 @@ */ import validateNamespace from './validateNamespace'; import validateHookName from './validateHookName'; -import type { Hooks, StoreKey } from '.'; +import type { Hooks, StoreKey } from './types'; /** * Removes the specified callback (or all callbacks) from the hook with a given hookName diff --git a/packages/hooks/src/createRunHook.ts b/packages/hooks/src/createRunHook.ts index 62077181a01f0c..18b35649f321f7 100644 --- a/packages/hooks/src/createRunHook.ts +++ b/packages/hooks/src/createRunHook.ts @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import type { Hooks, StoreKey } from '.'; +import type { Hooks, StoreKey } from './types'; export type RunHook = ( hookName: string, diff --git a/packages/hooks/src/types.ts b/packages/hooks/src/types.ts index 9365da351ed6df..8038c7473198ee 100644 --- a/packages/hooks/src/types.ts +++ b/packages/hooks/src/types.ts @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import type { Hooks as internalHooks } from './createHooks'; +export type { Hooks } from './createHooks'; export type Callback = ( ...args: any[] ) => any; @@ -21,8 +21,11 @@ export type Current = { currentIndex: number; }; +export type HookInfo = { + name: string; + currentIndex: number; +}; + export type Store = Record< string, Hook > & { __current: Set< Current > }; export type StoreKey = 'actions' | 'filters'; - -export type Hooks = internalHooks;