From c9f77b99a3d409d5abe6c9964ce3b30e2bffd0a1 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Fri, 1 Nov 2024 21:09:32 +0530 Subject: [PATCH 1/6] Convert lock unlock to generics --- packages/private-apis/src/implementation.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/private-apis/src/implementation.ts b/packages/private-apis/src/implementation.ts index ee2b9693959a35..d3de683ce88ada 100644 --- a/packages/private-apis/src/implementation.ts +++ b/packages/private-apis/src/implementation.ts @@ -136,14 +136,16 @@ export const __dangerousOptInToUnstableAPIsOnlyForCoreModules = ( * @param object The object to bind the private data to. * @param privateData The private data to bind to the object. */ -function lock( object: Record< symbol, WeakKey >, privateData: unknown ) { +function lock< T >( object: T, privateData: unknown ) { if ( ! object ) { throw new Error( 'Cannot lock an undefined object.' ); } - if ( ! ( __private in object ) ) { - object[ __private ] = {}; + const _object = object as Record< symbol, WeakKey >; + + if ( ! ( __private in _object ) ) { + _object[ __private ] = {}; } - lockedData.set( object[ __private ], privateData ); + lockedData.set( _object[ __private ], privateData ); } /** @@ -169,17 +171,19 @@ function lock( object: Record< symbol, WeakKey >, privateData: unknown ) { * @param object The object to unlock the private data from. * @return The private data bound to the object. */ -function unlock( object: Record< symbol, WeakKey > ) { +function unlock< T >( object: unknown ): T { if ( ! object ) { throw new Error( 'Cannot unlock an undefined object.' ); } - if ( ! ( __private in object ) ) { + const _object = object as Record< symbol, WeakKey >; + + if ( ! ( __private in _object ) ) { throw new Error( 'Cannot unlock an object that was not locked before. ' ); } - return lockedData.get( object[ __private ] ); + return lockedData.get( _object[ __private ] ); } const lockedData = new WeakMap(); From 1ed564c8e88a38066fa95c02453f8f852f8914a9 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Fri, 1 Nov 2024 21:42:10 +0530 Subject: [PATCH 2/6] Set object type from generic for unlock --- packages/private-apis/src/implementation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/private-apis/src/implementation.ts b/packages/private-apis/src/implementation.ts index d3de683ce88ada..6ab1a8fb1c630c 100644 --- a/packages/private-apis/src/implementation.ts +++ b/packages/private-apis/src/implementation.ts @@ -171,7 +171,7 @@ function lock< T >( object: T, privateData: unknown ) { * @param object The object to unlock the private data from. * @return The private data bound to the object. */ -function unlock< T >( object: unknown ): T { +function unlock< T >( object: T ): T { if ( ! object ) { throw new Error( 'Cannot unlock an undefined object.' ); } From 2f1aac28d12e07773a82421ff622e81c7527d119 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Fri, 1 Nov 2024 22:39:35 +0530 Subject: [PATCH 3/6] Fix types affected by updated signature of lock/unlock --- packages/components/src/private-apis.ts | 9 ++++++--- packages/core-data/src/hooks/use-entity-records.ts | 1 + .../src/components/dataviews-filters/add-filter.tsx | 5 ++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/components/src/private-apis.ts b/packages/components/src/private-apis.ts index bea16b719a463d..848f0228c86c48 100644 --- a/packages/components/src/private-apis.ts +++ b/packages/components/src/private-apis.ts @@ -10,8 +10,7 @@ import { Tabs } from './tabs'; import { kebabCase } from './utils/strings'; import { lock } from './lock-unlock'; -export const privateApis = {}; -lock( privateApis, { +const privateData = { __experimentalPopoverLegacyPositionToPlacement, createPrivateSlotFill, ComponentsContext, @@ -19,4 +18,8 @@ lock( privateApis, { Theme, Menu, kebabCase, -} ); +}; + +export const privateApis = {} as typeof privateData; + +lock( privateApis, privateData ); diff --git a/packages/core-data/src/hooks/use-entity-records.ts b/packages/core-data/src/hooks/use-entity-records.ts index e2659b88bc0198..d99e47fda5d2fb 100644 --- a/packages/core-data/src/hooks/use-entity-records.ts +++ b/packages/core-data/src/hooks/use-entity-records.ts @@ -182,6 +182,7 @@ export function useEntityRecordsWithPermissions< RecordType >( const permissions = useSelect( ( select ) => { + // @ts-expect-error getEntityRecordsPermissions doesn't exist in the core store types const { getEntityRecordsPermissions } = unlock( select( coreStore ) ); diff --git a/packages/dataviews/src/components/dataviews-filters/add-filter.tsx b/packages/dataviews/src/components/dataviews-filters/add-filter.tsx index 94aebb71ea5874..6af57a053ce168 100644 --- a/packages/dataviews/src/components/dataviews-filters/add-filter.tsx +++ b/packages/dataviews/src/components/dataviews-filters/add-filter.tsx @@ -39,7 +39,10 @@ export function AddFilterMenu( { } ) { const inactiveFilters = filters.filter( ( filter ) => ! filter.isVisible ); return ( - + { inactiveFilters.map( ( filter ) => { return ( Date: Wed, 6 Nov 2024 13:25:37 +0530 Subject: [PATCH 4/6] Improve signature --- packages/private-apis/src/implementation.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/private-apis/src/implementation.ts b/packages/private-apis/src/implementation.ts index a2a53e6b5ec261..5a5fb3f39fa183 100644 --- a/packages/private-apis/src/implementation.ts +++ b/packages/private-apis/src/implementation.ts @@ -137,7 +137,7 @@ export const __dangerousOptInToUnstableAPIsOnlyForCoreModules = ( * @param object The object to bind the private data to. * @param privateData The private data to bind to the object. */ -function lock< T >( object: T, privateData: unknown ) { +function lock( object: unknown, privateData: unknown ) { if ( ! object ) { throw new Error( 'Cannot lock an undefined object.' ); } @@ -172,7 +172,7 @@ function lock< T >( object: T, privateData: unknown ) { * @param object The object to unlock the private data from. * @return The private data bound to the object. */ -function unlock< T >( object: T ): T { +function unlock< T = any >( object: unknown ): T { if ( ! object ) { throw new Error( 'Cannot unlock an undefined object.' ); } From 65c1be71fd2ac624f14a4a2f59c026cc286a9957 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Wed, 6 Nov 2024 13:25:59 +0530 Subject: [PATCH 5/6] Remove expected errors no longer needed --- packages/core-data/src/hooks/use-entity-records.ts | 1 - .../src/components/dataviews-filters/add-filter.tsx | 5 +---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/core-data/src/hooks/use-entity-records.ts b/packages/core-data/src/hooks/use-entity-records.ts index d99e47fda5d2fb..e2659b88bc0198 100644 --- a/packages/core-data/src/hooks/use-entity-records.ts +++ b/packages/core-data/src/hooks/use-entity-records.ts @@ -182,7 +182,6 @@ export function useEntityRecordsWithPermissions< RecordType >( const permissions = useSelect( ( select ) => { - // @ts-expect-error getEntityRecordsPermissions doesn't exist in the core store types const { getEntityRecordsPermissions } = unlock( select( coreStore ) ); diff --git a/packages/dataviews/src/components/dataviews-filters/add-filter.tsx b/packages/dataviews/src/components/dataviews-filters/add-filter.tsx index 6af57a053ce168..94aebb71ea5874 100644 --- a/packages/dataviews/src/components/dataviews-filters/add-filter.tsx +++ b/packages/dataviews/src/components/dataviews-filters/add-filter.tsx @@ -39,10 +39,7 @@ export function AddFilterMenu( { } ) { const inactiveFilters = filters.filter( ( filter ) => ! filter.isVisible ); return ( - + { inactiveFilters.map( ( filter ) => { return ( Date: Wed, 6 Nov 2024 13:28:37 +0530 Subject: [PATCH 6/6] Restore the type for component private APIs --- packages/components/src/private-apis.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/components/src/private-apis.ts b/packages/components/src/private-apis.ts index 848f0228c86c48..bea16b719a463d 100644 --- a/packages/components/src/private-apis.ts +++ b/packages/components/src/private-apis.ts @@ -10,7 +10,8 @@ import { Tabs } from './tabs'; import { kebabCase } from './utils/strings'; import { lock } from './lock-unlock'; -const privateData = { +export const privateApis = {}; +lock( privateApis, { __experimentalPopoverLegacyPositionToPlacement, createPrivateSlotFill, ComponentsContext, @@ -18,8 +19,4 @@ const privateData = { Theme, Menu, kebabCase, -}; - -export const privateApis = {} as typeof privateData; - -lock( privateApis, privateData ); +} );