ESLint: Add use-recommended-components rule#76222
Conversation
use-recommended-components rule`use-recommended-components rule
|
Currently figuring out what to do with the two violations in |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: 0 B Total Size: 8.75 MB ℹ️ View Unchanged
|
|
Flaky tests detected in a7958f5. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/23055311398
|
6c63109 to
39877a9
Compare
There was a problem hiding this comment.
The lint rule better covers what is contained in this AGENTS file.
There was a problem hiding this comment.
Should we somehow test the denylist too? Either by adding a "test" entry, or by mocking it for the purpose of the tests?
There was a problem hiding this comment.
Let's also update the table in packages/eslint-plugin/README.md
| const ALLOWLIST = { | ||
| '@wordpress/ui': { | ||
| allowed: [ 'Badge', 'Stack' ], | ||
| message: | ||
| '`{{ name }}` from `{{ source }}` is not yet recommended for use in a WordPress environment.', | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Denylist: the listed components are flagged with a message pointing | ||
| * to a recommended alternative. | ||
| * | ||
| * @type {Record<string, Record<string, string>>} | ||
| */ | ||
| const DENYLIST = { | ||
| // Example: | ||
| // '@wordpress/components': { | ||
| // TextControl: 'Use `InputControl` from `@wordpress/ui` instead.', | ||
| // }, | ||
| }; |
There was a problem hiding this comment.
I know it's an unlikely edge case, but should we handle the situation in which a component is both in the allow and deny list?
| // TODO: Revert to the `Notice` in `@wordpress/components` for now. | ||
| // eslint-disable-next-line @wordpress/use-recommended-components |
aduth
left a comment
There was a problem hiding this comment.
Couple minor comments, but LGTM overall 👍
| @@ -0,0 +1,25 @@ | |||
| # Encourage the use of recommended components (use-recommended-components) | |||
There was a problem hiding this comment.
It might be worth a future refactor to generalize a rule for "don't use an named import from this package, use it from another" and not specifically to components. I see the value in pre-packaging the configuration for components usage, but there's some underlying usefulness to the general approach of redirecting package imports.
There was a problem hiding this comment.
Yes, possibly! Though, in the general case, these things have been motivated by deprecations, which can already be signaled by TS and deprecation messages. So in those cases a lint rule is convenient but also slightly redundant.
What?
Part of #76135
Adds a new
use-recommended-componentsESLint rule to@wordpress/eslint-pluginand enables it in the repo.Why?
As
@wordpress/uicomponents become ready for use, we need a way to communicate which ones are recommended and which aren't yet. On the other side, as components in older packages like@wordpress/componentsgain newer alternatives, we want to nudge consumers toward them.This rule provides an evolving, up-to-date set of that guidance directly as lint feedback.
I also wanted to make clear that this is guidance for a WordPress environment, where there are specific concerns (infrastructure, coherence with legacy componentry) that may not apply to consumers in a non-WordPress environment.
How?
The rule uses two strategies, both with component lists hardcoded in the rule file:
@wordpress/uiwhere only a subset of components are ready for general use. Named imports not on the list are flagged.The rule is enabled as
errorin this repo's.eslintrc.jsonly, not in the sharedrecommendedconfig, so third-party consumers of@wordpress/eslint-plugincan opt in if they want. At this early stage, it is mostly intended for the Gutenberg repo.Testing Instructions
Run the rule's unit tests: