Skip to content

Commit 7550dd2

Browse files
committed
Import __unstableSelectionHasUnmergeableBlock from the correct module and add a clear error on invalid destructuring
1 parent 0ac796d commit 7550dd2

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

packages/block-editor/src/components/block-list/use-block-props/use-block-class-names.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ import classnames from 'classnames';
66
/**
77
* WordPress dependencies
88
*/
9-
import {
10-
useSelect,
11-
__experimentalAccessKey as dataExperiments,
12-
} from '@wordpress/data';
9+
import { useSelect } from '@wordpress/data';
1310
import { isReusableBlock, getBlockType } from '@wordpress/blocks';
1411

1512
/**
1613
* Internal dependencies
1714
*/
1815
import { store as blockEditorStore } from '../../../store';
19-
import { unlock } from '../../../experiments';
16+
import {
17+
unlock,
18+
__experimentalAccessKey as blockEditorExperiments,
19+
} from '../../../experiments';
2020

21-
const { __unstableSelectionHasUnmergeableBlock } = unlock( dataExperiments );
21+
const { __unstableSelectionHasUnmergeableBlock } = unlock(
22+
blockEditorExperiments
23+
);
2224

2325
/**
2426
* Returns the class names used for the different states of the block.

packages/data/src/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,14 @@ function __experimentalPrivateSelector( { name }, selector ) {
244244
return ( ...args ) =>
245245
selector( defaultRegistry.stores[ name ].store.getState(), ...args );
246246
}
247-
function __experimentalPrivateDispatch( { name }, actionThunk ) {
248-
return defaultRegistry.stores[ name ].store.dispatch( actionThunk );
247+
function __experimentalPrivateActionCreator( { name }, actionCreator ) {
248+
return ( ...args ) =>
249+
defaultRegistry.stores[ name ].store.dispatch(
250+
actionCreator( ...args )
251+
);
249252
}
250253

251254
export const __experimentalAccessKey = experimentalAPIs.register( {
252255
__experimentalPrivateSelector,
253-
__experimentalPrivateDispatch,
256+
__experimentalPrivateActionCreator,
254257
} );

packages/experiments/src/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,20 @@ export const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (
5858
'your product will inevitably break on the next WordPress release.'
5959
);
6060
}
61+
const apis = {};
6162
registeredExperiments[ moduleName ] = {
6263
accessKey: {},
63-
apis: {},
64+
apis,
65+
apisProxy: new Proxy( apis, {
66+
get( target, name ) {
67+
if ( ! ( name in target ) ) {
68+
throw new Error(
69+
`No experimental API with name "${ name }" has been registered under the module ${ moduleName }.`
70+
);
71+
}
72+
return target[ name ];
73+
},
74+
} ),
6475
};
6576
return {
6677
register: ( experiments ) => {
@@ -73,7 +84,7 @@ export const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (
7384
unlock: ( accessKey ) => {
7485
for ( const experiment of Object.values( registeredExperiments ) ) {
7586
if ( experiment.accessKey === accessKey ) {
76-
return experiment.apis;
87+
return experiment.apisProxy;
7788
}
7889
}
7990

packages/experiments/src/test/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,20 @@ describe( '__dangerousOptInToUnstableAPIsOnlyForCoreModules', () => {
8181
dataExperimentalFunctions.__experimentalFunction
8282
).toHaveBeenCalled();
8383
} );
84+
it( 'Should throw an exception upon accessing a non-existing experimental API', () => {
85+
const { register, unlock } =
86+
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
87+
requiredConsent,
88+
'@wordpress/block-editor'
89+
);
90+
91+
const accessKey = register( {
92+
fooAPI: true,
93+
} );
94+
95+
expect( unlock( accessKey ).fooAPI ).toBe( true );
96+
expect( () => unlock( accessKey ).noSuchAPI ).toThrowError(
97+
/No experimental API with name "noSuchAPI" has been registered/
98+
);
99+
} );
84100
} );

0 commit comments

Comments
 (0)