Skip to content

Commit

Permalink
Quality: Promote lint rule to error when validating store string lite…
Browse files Browse the repository at this point in the history
…rals (#32537)

* Quality: Promote lint rule for store string literals to error

* Try to replace `core/data` with selectors from related stores

* Disable rule violation in the `@wordpress/server-side-render` component
  • Loading branch information
gziolo authored Jun 21, 2021
1 parent 1c01f3c commit ee69822
Show file tree
Hide file tree
Showing 23 changed files with 74 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = {
},
],
'@wordpress/no-unsafe-wp-apis': 'off',
'@wordpress/data-no-store-string-literals': 'warn',
'@wordpress/data-no-store-string-literals': 'error',
'import/default': 'error',
'import/named': 'error',
'no-restricted-imports': [
Expand Down
5 changes: 0 additions & 5 deletions packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* WordPress dependencies
*/
import '@wordpress/rich-text';

/**
* Internal dependencies
*/
Expand Down
5 changes: 2 additions & 3 deletions packages/block-library/src/categories/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ export default function CategoriesEdit( {
} ) {
const selectId = useInstanceId( CategoriesEdit, 'blocks-category-select' );
const { categories, isRequesting } = useSelect( ( select ) => {
const { getEntityRecords } = select( coreStore );
const { isResolving } = select( 'core/data' );
const { getEntityRecords, isResolving } = select( coreStore );
const query = { per_page: -1, hide_empty: true };
return {
categories: getEntityRecords( 'taxonomy', 'category', query ),
isRequesting: isResolving( 'core', 'getEntityRecords', [
isRequesting: isResolving( 'getEntityRecords', [
'taxonomy',
'category',
query,
Expand Down
6 changes: 2 additions & 4 deletions packages/block-library/src/embed/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const EmbedEdit = ( props ) => {

const [ url, setURL ] = useState( attributesUrl );
const [ isEditingURL, setIsEditingURL ] = useState( false );
const { invalidateResolution } = useDispatch( 'core/data' );
const { invalidateResolution } = useDispatch( coreStore );

const {
preview,
Expand Down Expand Up @@ -214,9 +214,7 @@ const EmbedEdit = ( props ) => {
onChange={ ( event ) => setURL( event.target.value ) }
fallback={ () => fallback( url, onReplace ) }
tryAgain={ () => {
invalidateResolution( 'core', 'getEmbedPreview', [
url,
] );
invalidateResolution( 'getEmbedPreview', [ url ] );
} }
/>
</View>
Expand Down
2 changes: 0 additions & 2 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* WordPress dependencies
*/
import '@wordpress/core-data';
import '@wordpress/block-editor';
import {
registerBlockType,
setDefaultBlockName,
Expand Down
54 changes: 22 additions & 32 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from './locks';
import { createBatch } from './batch';
import { getDispatch } from './controls';
import { CORE_STORE_NAME as coreStoreName } from './utils/constants';
import { STORE_NAME } from './name';

/**
* Returns an action object used in signalling that authors have been received.
Expand Down Expand Up @@ -183,7 +183,7 @@ export function* deleteEntityRecord(
}

const lock = yield* __unstableAcquireStoreLock(
coreStoreName,
STORE_NAME,
[ 'entities', 'data', kind, name, recordId ],
{ exclusive: true }
);
Expand Down Expand Up @@ -247,27 +247,22 @@ export function* deleteEntityRecord(
* @return {Object} Action object.
*/
export function* editEntityRecord( kind, name, recordId, edits, options = {} ) {
const entity = yield controls.select(
coreStoreName,
'getEntity',
kind,
name
);
const entity = yield controls.select( STORE_NAME, 'getEntity', kind, name );
if ( ! entity ) {
throw new Error(
`The entity being edited (${ kind }, ${ name }) does not have a loaded config.`
);
}
const { transientEdits = {}, mergedEdits = {} } = entity;
const record = yield controls.select(
coreStoreName,
STORE_NAME,
'getRawEntityRecord',
kind,
name,
recordId
);
const editedRecord = yield controls.select(
coreStoreName,
STORE_NAME,
'getEditedEntityRecord',
kind,
name,
Expand Down Expand Up @@ -312,7 +307,7 @@ export function* editEntityRecord( kind, name, recordId, edits, options = {} ) {
* an entity record, if any.
*/
export function* undo() {
const undoEdit = yield controls.select( coreStoreName, 'getUndoEdit' );
const undoEdit = yield controls.select( STORE_NAME, 'getUndoEdit' );
if ( ! undoEdit ) {
return;
}
Expand All @@ -330,7 +325,7 @@ export function* undo() {
* edit to an entity record, if any.
*/
export function* redo() {
const redoEdit = yield controls.select( coreStoreName, 'getRedoEdit' );
const redoEdit = yield controls.select( STORE_NAME, 'getRedoEdit' );
if ( ! redoEdit ) {
return;
}
Expand Down Expand Up @@ -380,7 +375,7 @@ export function* saveEntityRecord(
const recordId = record[ entityIdKey ];

const lock = yield* __unstableAcquireStoreLock(
coreStoreName,
STORE_NAME,
[ 'entities', 'data', kind, name, recordId || uuid() ],
{ exclusive: true }
);
Expand All @@ -391,7 +386,7 @@ export function* saveEntityRecord(
if ( typeof value === 'function' ) {
const evaluatedValue = value(
yield controls.select(
coreStoreName,
STORE_NAME,
'getEditedEntityRecord',
kind,
name,
Expand Down Expand Up @@ -425,7 +420,7 @@ export function* saveEntityRecord(
recordId ? '/' + recordId : ''
}`;
const persistedRecord = yield controls.select(
coreStoreName,
STORE_NAME,
'getRawEntityRecord',
kind,
name,
Expand All @@ -438,12 +433,12 @@ export function* saveEntityRecord(
// but ideally this should all be handled in the back end,
// so the client just sends and receives objects.
const currentUser = yield controls.select(
coreStoreName,
STORE_NAME,
'getCurrentUser'
);
const currentUserId = currentUser ? currentUser.id : undefined;
const autosavePost = yield controls.select(
coreStoreName,
STORE_NAME,
'getAutosave',
persistedRecord.type,
persistedRecord.id,
Expand Down Expand Up @@ -614,20 +609,15 @@ export function* __experimentalBatch( requests ) {
const api = {
saveEntityRecord( kind, name, record, options ) {
return batch.add( ( add ) =>
dispatch( coreStoreName ).saveEntityRecord(
kind,
name,
record,
{
...options,
__unstableFetch: add,
}
)
dispatch( STORE_NAME ).saveEntityRecord( kind, name, record, {
...options,
__unstableFetch: add,
} )
);
},
saveEditedEntityRecord( kind, name, recordId, options ) {
return batch.add( ( add ) =>
dispatch( coreStoreName ).saveEditedEntityRecord(
dispatch( STORE_NAME ).saveEditedEntityRecord(
kind,
name,
recordId,
Expand All @@ -640,7 +630,7 @@ export function* __experimentalBatch( requests ) {
},
deleteEntityRecord( kind, name, recordId, query, options ) {
return batch.add( ( add ) =>
dispatch( coreStoreName ).deleteEntityRecord(
dispatch( STORE_NAME ).deleteEntityRecord(
kind,
name,
recordId,
Expand Down Expand Up @@ -671,7 +661,7 @@ export function* __experimentalBatch( requests ) {
export function* saveEditedEntityRecord( kind, name, recordId, options ) {
if (
! ( yield controls.select(
coreStoreName,
STORE_NAME,
'hasEditsForEntityRecord',
kind,
name,
Expand All @@ -681,7 +671,7 @@ export function* saveEditedEntityRecord( kind, name, recordId, options ) {
return;
}
const edits = yield controls.select(
coreStoreName,
STORE_NAME,
'getEntityRecordNonTransientEdits',
kind,
name,
Expand Down Expand Up @@ -709,7 +699,7 @@ export function* __experimentalSaveSpecifiedEntityEdits(
) {
if (
! ( yield controls.select(
coreStoreName,
STORE_NAME,
'hasEditsForEntityRecord',
kind,
name,
Expand All @@ -719,7 +709,7 @@ export function* __experimentalSaveSpecifiedEntityEdits(
return;
}
const edits = yield controls.select(
coreStoreName,
STORE_NAME,
'getEntityRecordNonTransientEdits',
kind,
name,
Expand Down
4 changes: 2 additions & 2 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import { addEntities } from './actions';
import { CORE_STORE_NAME as coreStoreName } from './utils/constants';
import { STORE_NAME } from './name';

export const DEFAULT_ENTITY_KEY = 'id';

Expand Down Expand Up @@ -250,7 +250,7 @@ export const getMethodName = (
*/
export function* getKindEntities( kind ) {
let entities = yield controls.select(
coreStoreName,
STORE_NAME,
'getEntitiesByKind',
kind
);
Expand Down
10 changes: 5 additions & 5 deletions packages/core-data/src/entity-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { parse, __unstableSerializeAndClean } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { CORE_STORE_NAME as coreStoreName } from './utils/constants';
import { STORE_NAME } from './name';

const EMPTY_ARRAY = [];

Expand Down Expand Up @@ -100,7 +100,7 @@ export function useEntityProp( kind, type, prop, _id ) {
const { value, fullValue } = useSelect(
( select ) => {
const { getEntityRecord, getEditedEntityRecord } = select(
coreStoreName
STORE_NAME
);
const entity = getEntityRecord( kind, type, id ); // Trigger resolver.
const editedEntity = getEditedEntityRecord( kind, type, id );
Expand All @@ -113,7 +113,7 @@ export function useEntityProp( kind, type, prop, _id ) {
},
[ kind, type, id, prop ]
);
const { editEntityRecord } = useDispatch( coreStoreName );
const { editEntityRecord } = useDispatch( STORE_NAME );
const setValue = useCallback(
( newValue ) => {
editEntityRecord( kind, type, id, {
Expand Down Expand Up @@ -149,7 +149,7 @@ export function useEntityBlockEditor( kind, type, { id: _id } = {} ) {
const id = _id ?? providerId;
const { content, blocks } = useSelect(
( select ) => {
const { getEditedEntityRecord } = select( coreStoreName );
const { getEditedEntityRecord } = select( STORE_NAME );
const editedEntity = getEditedEntityRecord( kind, type, id );
return {
blocks: editedEntity.blocks,
Expand All @@ -159,7 +159,7 @@ export function useEntityBlockEditor( kind, type, { id: _id } = {} ) {
[ kind, type, id ]
);
const { __unstableCreateUndoLevel, editEntityRecord } = useDispatch(
coreStoreName
STORE_NAME
);

useEffect( () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/core-data/src/locks/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { controls } from '@wordpress/data';
/**
* Internal dependencies
*/
import { CORE_STORE_NAME as coreStoreName } from '../utils/constants';
import { STORE_NAME } from '../name';

export function* __unstableAcquireStoreLock( store, path, { exclusive } ) {
const promise = yield* __unstableEnqueueLockRequest( store, path, {
Expand Down Expand Up @@ -42,13 +42,13 @@ export function* __unstableProcessPendingLockRequests() {
type: 'PROCESS_PENDING_LOCK_REQUESTS',
};
const lockRequests = yield controls.select(
coreStoreName,
STORE_NAME,
'__unstableGetPendingLockRequests'
);
for ( const request of lockRequests ) {
const { store, path, exclusive, notifyAcquired } = request;
const isAvailable = yield controls.select(
coreStoreName,
STORE_NAME,
'__unstableIsLockAvailable',
store,
path,
Expand Down
14 changes: 7 additions & 7 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { apiFetch } from '@wordpress/data-controls';
* Internal dependencies
*/
import { regularFetch } from './controls';
import { CORE_STORE_NAME as coreStoreName } from './utils/constants';
import { STORE_NAME } from './name';

/**
* Internal dependencies
Expand Down Expand Up @@ -86,7 +86,7 @@ export function* getEntityRecord( kind, name, key = '', query ) {
}

const lock = yield* __unstableAcquireStoreLock(
coreStoreName,
STORE_NAME,
[ 'entities', 'data', kind, name, key ],
{ exclusive: false }
);
Expand Down Expand Up @@ -123,7 +123,7 @@ export function* getEntityRecord( kind, name, key = '', query ) {
// fields, so it's tested here, prior to initiating the REST request,
// and without causing `getEntityRecords` resolution to occur.
const hasRecords = yield controls.select(
coreStoreName,
STORE_NAME,
'hasEntityRecords',
kind,
name,
Expand Down Expand Up @@ -175,7 +175,7 @@ export function* getEntityRecords( kind, name, query = {} ) {
}

const lock = yield* __unstableAcquireStoreLock(
coreStoreName,
STORE_NAME,
[ 'entities', 'data', kind, name ],
{ exclusive: false }
);
Expand Down Expand Up @@ -370,7 +370,7 @@ export function* canUserEditEntityRecord( kind, name, recordId ) {
*/
export function* getAutosaves( postType, postId ) {
const { rest_base: restBase } = yield controls.resolveSelect(
coreStoreName,
STORE_NAME,
'getPostType',
postType
);
Expand All @@ -394,7 +394,7 @@ export function* getAutosaves( postType, postId ) {
*/
export function* getAutosave( postType, postId ) {
yield controls.resolveSelect(
coreStoreName,
STORE_NAME,
'getAutosaves',
postType,
postId
Expand Down Expand Up @@ -427,7 +427,7 @@ export function* __experimentalGetTemplateForLink( link ) {

yield getEntityRecord( 'postType', 'wp_template', template.id );
const record = yield controls.select(
coreStoreName,
STORE_NAME,
'getEntityRecord',
'postType',
'wp_template',
Expand Down
Loading

0 comments on commit ee69822

Please sign in to comment.