From 7a261169b996e7d6fc8fc306ed74c2feb1afd205 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 9 May 2024 18:13:15 +0200 Subject: [PATCH] Move warn to same resolve function --- packages/interactivity/src/hooks.tsx | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/interactivity/src/hooks.tsx b/packages/interactivity/src/hooks.tsx index 377e62064ef105..946919de7a9531 100644 --- a/packages/interactivity/src/hooks.tsx +++ b/packages/interactivity/src/hooks.tsx @@ -261,6 +261,12 @@ export const directive = ( // Resolve the path to some property of the store object. const resolve = ( path, namespace ) => { + if ( ! namespace ) { + warn( + `The "namespace" cannot be "{}", "null" or an emtpy string. Path: ${ path }` + ); + return; + } let resolvedStore = stores.get( namespace ); if ( typeof resolvedStore === 'undefined' ) { resolvedStore = store( namespace, undefined, { @@ -272,8 +278,13 @@ const resolve = ( path, namespace ) => { context: getScope().context[ namespace ], }; try { + // TODO: Support lazy/dynamically initialized stores return path.split( '.' ).reduce( ( acc, key ) => acc[ key ], current ); - } catch ( e ) {} + } catch ( e ) { + warn( + `The path "${ path }" could not be resolved in the "${ namespace }" store.` + ); + } }; // Generate the evaluate function. @@ -284,12 +295,6 @@ export const getEvaluate: GetEvaluate = if ( typeof path !== 'string' ) { throw new Error( 'The `value` prop should be a string path' ); } - if ( ! namespace ) { - // TODO: Support lazy/dynamically initialized stores - warn( - `The "namespace" cannot be "{}", "null" or an emtpy string. Path: ${ path }` - ); - } // If path starts with !, remove it and save a flag. const hasNegationOperator = path[ 0 ] === '!' && !! ( path = path.slice( 1 ) );