diff --git a/packages/interactivity/src/directives.tsx b/packages/interactivity/src/directives.tsx index 07328df8af210b..b0b78bb27efe50 100644 --- a/packages/interactivity/src/directives.tsx +++ b/packages/interactivity/src/directives.tsx @@ -13,10 +13,8 @@ import { deepSignal, peek, type DeepSignal } from 'deepsignal'; /** * Internal dependencies */ -import { useWatch, useInit } from './utils'; +import { useWatch, useInit, kebabToCamelCase, warn } from './utils'; import { directive, getScope, getEvaluate } from './hooks'; -import { kebabToCamelCase } from './utils/kebab-to-camelcase'; -import { warn } from './utils/warn'; // Assigned objects should be ignore during proxification. const contextAssignedObjects = new WeakMap(); diff --git a/packages/interactivity/src/hooks.tsx b/packages/interactivity/src/hooks.tsx index 2dfc08a43f6fa8..01b5fbf043149e 100644 --- a/packages/interactivity/src/hooks.tsx +++ b/packages/interactivity/src/hooks.tsx @@ -20,7 +20,7 @@ import type { VNode, Context, RefObject } from 'preact'; * Internal dependencies */ import { store, stores, universalUnlock } from './store'; -import { warn } from './utils/warn'; +import { warn } from './utils'; interface DirectiveEntry { value: string | object; namespace: string; diff --git a/packages/interactivity/src/utils/test/utils.js b/packages/interactivity/src/test/utils.ts similarity index 94% rename from packages/interactivity/src/utils/test/utils.js rename to packages/interactivity/src/test/utils.ts index 2416b03e342ee9..ff564fa7c4c250 100644 --- a/packages/interactivity/src/utils/test/utils.js +++ b/packages/interactivity/src/test/utils.ts @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import { kebabToCamelCase } from '../kebab-to-camelcase'; +import { kebabToCamelCase } from '../utils'; describe( 'kebabToCamelCase', () => { it( 'should work exactly as the PHP version', async () => { diff --git a/packages/interactivity/src/utils.ts b/packages/interactivity/src/utils.ts index e201ff15a68927..ecf22899309e97 100644 --- a/packages/interactivity/src/utils.ts +++ b/packages/interactivity/src/utils.ts @@ -290,3 +290,48 @@ export const createRootFragment = ( }, } ); }; + +/** + * Transforms a kebab-case string to camelCase. + * + * @param str The kebab-case string to transform to camelCase. + * @return The transformed camelCase string. + */ +export function kebabToCamelCase( str: string ): string { + return str + .replace( /^-+|-+$/g, '' ) + .toLowerCase() + .replace( /-([a-z])/g, function ( _match, group1: string ) { + return group1.toUpperCase(); + } ); +} + +const logged: Set< string > = new Set(); + +/** + * Shows a warning with `message` if environment is not `production`. + * + * Based on the `@wordpress/warning` package. + * + * @param message Message to show in the warning. + */ +export const warn = ( message: string ): void => { + // @ts-expect-error + if ( typeof SCRIPT_DEBUG !== 'undefined' && SCRIPT_DEBUG === true ) { + if ( logged.has( message ) ) { + return; + } + + // eslint-disable-next-line no-console + console.warn( message ); + + // Throwing an error and catching it immediately to improve debugging + // A consumer can use 'pause on caught exceptions' + try { + throw Error( message ); + } catch ( e ) { + // Do nothing. + } + logged.add( message ); + } +}; diff --git a/packages/interactivity/src/utils/kebab-to-camelcase.ts b/packages/interactivity/src/utils/kebab-to-camelcase.ts deleted file mode 100644 index b6c3c6f3071245..00000000000000 --- a/packages/interactivity/src/utils/kebab-to-camelcase.ts +++ /dev/null @@ -1,14 +0,0 @@ -/** - * Transforms a kebab-case string to camelCase. - * - * @param str The kebab-case string to transform to camelCase. - * @return The transformed camelCase string. - */ -export function kebabToCamelCase( str: string ): string { - return str - .replace( /^-+|-+$/g, '' ) - .toLowerCase() - .replace( /-([a-z])/g, function ( _match, group1: string ) { - return group1.toUpperCase(); - } ); -} diff --git a/packages/interactivity/src/utils/warn.ts b/packages/interactivity/src/utils/warn.ts deleted file mode 100644 index 45a764a46707b6..00000000000000 --- a/packages/interactivity/src/utils/warn.ts +++ /dev/null @@ -1,21 +0,0 @@ -const logged: Set< string > = new Set(); - -export const warn = ( message: string ): void => { - // @ts-expect-error - if ( typeof SCRIPT_DEBUG !== 'undefined' && SCRIPT_DEBUG === true ) { - if ( logged.has( message ) ) { - return; - } - - // eslint-disable-next-line no-console - console.warn( message ); - - // Adding a stack trace to the warning message to help with debugging. - try { - throw Error( message ); - } catch ( e ) { - // Do nothing. - } - logged.add( message ); - } -}; diff --git a/packages/interactivity/src/vdom.ts b/packages/interactivity/src/vdom.ts index d5238cde49d8e7..30179209cca677 100644 --- a/packages/interactivity/src/vdom.ts +++ b/packages/interactivity/src/vdom.ts @@ -6,7 +6,7 @@ import { h, type ComponentChild, type JSX } from 'preact'; * Internal dependencies */ import { directivePrefix as p } from './constants'; -import { warn } from './utils/warn'; +import { warn } from './utils'; const ignoreAttr = `data-${ p }-ignore`; const islandAttr = `data-${ p }-interactive`; diff --git a/test/unit/jest.config.js b/test/unit/jest.config.js index 38459b631fea4b..4a19d5bb37f316 100644 --- a/test/unit/jest.config.js +++ b/test/unit/jest.config.js @@ -40,7 +40,7 @@ module.exports = { '^.+\\.[jt]sx?$': '/test/unit/scripts/babel-transformer.js', }, transformIgnorePatterns: [ - '/node_modules/(?!(docker-compose|yaml)/)', + '/node_modules/(?!(docker-compose|yaml|preact|@preact|deepsignal)/)', '\\.pnp\\.[^\\/]+$', ], snapshotSerializers: [