Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code quality: Replace hardcoded store keys with store definitions #27088

Closed
16 of 17 tasks
gziolo opened this issue Nov 19, 2020 · 29 comments · Fixed by #32537
Closed
16 of 17 tasks

Code quality: Replace hardcoded store keys with store definitions #27088

gziolo opened this issue Nov 19, 2020 · 29 comments · Fixed by #32537
Assignees
Labels
[Package] Data /packages/data [Status] In Progress Tracking issues with work in progress [Type] Code Quality Issues or PRs that relate to code quality [Type] Tracking Issue Tactical breakdown of efforts across the codebase and/or tied to Overview issues.

Comments

@gziolo
Copy link
Member

gziolo commented Nov 19, 2020

Description

With #26655 it's possible to use store definitions as param for select/useSelect/withSelect and dispatch/useDispatch/withDispatch API method calls exposed from @wordpress/data. It resolves the issue with the need to use unscoped import statements for packages that define stores used as explained in #8981 (comment):

To be clear, all packages that use a given datastore should import it in the entry point by convention. It's something that could be improved because if it's missed, it might indeed cause errors. At the moment, there is no simple way to validate it happens. Example:

import '@wordpress/core-data';
import '@wordpress/block-editor';
import '@wordpress/editor';
import '@wordpress/keyboard-shortcuts';
import '@wordpress/reusable-blocks';
import '@wordpress/viewport';
import '@wordpress/notices';

The next step is to use newly exposed store definitions from the corresponding packages similar to how it was applied to the store exposef from @wordpress/viewport in #26655. Example:

diff --git a/packages/interface/src/components/complementary-area/index.js b/packages/interface/src/components/complementary-area/index.js
index 56ad63c601e1..f35fcf9d14bc 100644
--- a/packages/interface/src/components/complementary-area/index.js
+++ b/packages/interface/src/components/complementary-area/index.js
@@ -11,6 +11,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
 import { __ } from '@wordpress/i18n';
 import { check, starEmpty, starFilled } from '@wordpress/icons';
 import { useEffect, useRef } from '@wordpress/element';
+import { store as viewportStore } from '@wordpress/viewport';
 
 /**
  * Internal dependencies
@@ -104,10 +105,8 @@ function ComplementaryArea( {
 				isActive: _activeArea === identifier,
 				isPinned: isItemPinned( scope, identifier ),
 				activeArea: _activeArea,
-				isSmall: select( 'core/viewport' ).isViewportMatch(
-					'< medium'
-				),
-				isLarge: select( 'core/viewport' ).isViewportMatch( 'large' ),
+				isSmall: select( viewportStore ).isViewportMatch( '< medium' ),
+				isLarge: select( viewportStore ).isViewportMatch( 'large' ),
 			};
 		},
 		[ identifier, scope ]

The goal is to make it simpler to maintain dependencies between packages to avoid bugs when consuming from npm. The other benefit is that we will prevent cyclic dependencies in the early stages of development.

Tasks

This is the list of store names that should be replaced with their newly introduced store definitions:

@gziolo gziolo added [Type] Code Quality Issues or PRs that relate to code quality [Package] Data /packages/data [Type] Tracking Issue Tactical breakdown of efforts across the codebase and/or tied to Overview issues. labels Nov 19, 2020
@gziolo gziolo added the Good First Issue An issue that's suitable for someone looking to contribute for the first time label Nov 19, 2020
grzim pushed a commit to grzim/gutenberg that referenced this issue Nov 19, 2020
@rafaelgallani
Copy link
Contributor

Hello! Are the unchecked items (except the one in #27094) available for taking? I would like to replace some of these.
Initially, the core/block-directory one if it's not taken.

@gziolo
Copy link
Member Author

gziolo commented Nov 22, 2020

Yes, those unchecked and without a PR referenced are up for grabs 👍

core/block-directory seems like a good one to start, it might not have an external usage outside of its package though 😃

@rafaelgallani
Copy link
Contributor

Taking core/edit-navigation 😸

@rafaelgallani
Copy link
Contributor

@gziolo I was going to pick some more items, but I just saw that #27226 is proposing a change to the stores names. Should I wait for the PR to be merged or can I continue refactoring?

@gziolo
Copy link
Member Author

gziolo commented Nov 25, 2020

@gziolo I was going to pick some more items, but I just saw that #27226 is proposing a change to the stores names. Should I wait for the PR to be merged or can I continue refactoring?

There is no need to wait. We can always rebase with master branch and update #27226 if we decide to change exported names. It will be a quick operation in IDE to replace store as with an empty string 😄

@rafaelgallani
Copy link
Contributor

rafaelgallani commented Nov 25, 2020

Great! I just asked to make sure I wouldn't be complicating things... 😸
I'll work on it then. 👍🏻

@bosconian-dynamics
Copy link
Contributor

I would love to pick up one of these as my inaugural Gutenberg issue, using the other PRs so far as a model for the work. core/keyboard-shortcuts looks to me like it might be relatively straightforward?

@rafaelgallani
Copy link
Contributor

Can I take core/blocks? It looks a little bit more fun than the previous ones I took since other packages depend on it 😄

@gziolo
Copy link
Member Author

gziolo commented Nov 26, 2020

@rafaelgalani, whatever works for you best. I added your name next to core/blocks on the list.

bosconian-dynamics added a commit to bosconian-dynamics/gutenberg that referenced this issue Nov 29, 2020
definition

Replaces references to the `core/keyboard-shortcuts`
store name string with the store definition object, adding `import`s
where necessary.

Addresses WordPress#27088.
gziolo pushed a commit that referenced this issue Nov 30, 2020
)

definition

Replaces references to the `core/keyboard-shortcuts`
store name string with the store definition object, adding `import`s
where necessary.

Addresses #27088.
@gziolo
Copy link
Member Author

gziolo commented Nov 30, 2020

I opened #27368 that removes bare import statements for packages that use now store definitions rather than hardcoded strings.

@rafaelgallani
Copy link
Contributor

Taking core/edit-post 😸😸😸

@david-szabo97
Copy link
Member

Should we maybe land the ESLint rule first at the warning level to make the process simpler?

Oh, that sounds like a fantastic idea! We can also warn people working on new PRs to avoid using strings.

@gziolo
Copy link
Member Author

gziolo commented Jun 7, 2021

@aristath, what's the current status after your recent work in this area? I see a few more warnings, are there any issues discovered? We should promote the warning to error as the last step.

@aristath
Copy link
Member

aristath commented Jun 7, 2021

There are just a handful of them remaining, these are the ones I couldn't address (*a couple of them were reverted because they were causing an issue with an e2e test)

const { isResolving } = select( 'core/data' );

const { invalidateResolution } = useDispatch( 'core/data' );

const { isResolving } = select( 'core/data' );

const isWelcomeGuideActive = yield controls.select(
'core/edit-post',

const { isResolving } = select( 'core/data' );

const coreEditorSelect = select( 'core/editor' );

Once we address these 6 we should promote this to an error 🎉

@gziolo gziolo removed the Good First Issue An issue that's suitable for someone looking to contribute for the first time label Jun 7, 2021
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Jun 9, 2021
@gziolo
Copy link
Member Author

gziolo commented Jun 9, 2021

I started #32537 which tries to tackle the remaining issues.

@gziolo
Copy link
Member Author

gziolo commented Jun 21, 2021

We did it 🎉

Amazing team effort everyone involved. Major props for the achievement.

There is even an ESLint rule @wordpress/data-no-store-string-literals that will prevent usage of hardcoded store names in the future.

There are still several places where we have hardcoded store names, but they require some architectural changes because the stores used in the code are present conditionally depending on the execution context.

There is a group of items to look at raised for the code used with native mobile apps in #27751.

In the web part of the universe, we still have two places that need more work:

Discovered in #32801:

// FIXME: @wordpress/block-library should not depend on @wordpress/editor.
// Blocks can be loaded into a *non-post* block editor.
// eslint-disable-next-line @wordpress/data-no-store-string-literals
const editorSelectors = select( 'core/editor' );
if ( editorSelectors ) {

Discovered in #32537

// FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.
// It is used by blocks that can be loaded into a *non-post* block editor.
// eslint-disable-next-line @wordpress/data-no-store-string-literals
const coreEditorSelect = select( 'core/editor' );
if ( coreEditorSelect ) {

The related follow-up issue is #32842.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Data /packages/data [Status] In Progress Tracking issues with work in progress [Type] Code Quality Issues or PRs that relate to code quality [Type] Tracking Issue Tactical breakdown of efforts across the codebase and/or tied to Overview issues.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants