Repository navigation
ESLint: Add use-recommended-components rule
#76222
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
Changes from all commits
24e1b79
dde5da6
39877a9
47b5de8
90fe6d0
7568ac1
b4757ae
6587be5
87051f1
247652b
9422b65
72c5dc8
e5becfa
a7958f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Encourage the use of recommended components (use-recommended-components) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
|
|
||
| Encourages the use of recommended UI components in a WordPress environment by flagging imports that have a better alternative. The component lists are maintained directly in the rule and represent an evolving, up-to-date set of guidance. | ||
|
|
||
| ## Rule details | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| // @wordpress/ui — this component isn't recommended yet. | ||
| import { SomeComponent } from '@wordpress/ui'; | ||
|
|
||
| // @wordpress/components — a newer alternative is available. | ||
| import { TextControl } from '@wordpress/components'; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| // Packages not covered by the rule are unaffected. | ||
| import { Button } from '@wordpress/components'; | ||
|
|
||
| // Default and namespace imports are not checked. | ||
| import UI from '@wordpress/ui'; | ||
| ``` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we somehow test the denylist too? Either by adding a "test" entry, or by mocking it for the purpose of the tests?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* eslint-env jest */ | ||
|
|
||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { RuleTester } from 'eslint'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import rule, { ALLOWLIST, DENYLIST } from '../use-recommended-components'; | ||
|
|
||
| const ruleTester = new RuleTester( { | ||
| parserOptions: { | ||
| sourceType: 'module', | ||
| ecmaVersion: 6, | ||
| }, | ||
| } ); | ||
|
|
||
| ruleTester.run( 'use-recommended-components', rule, { | ||
| valid: [ | ||
| // Unrelated packages are not affected. | ||
| "import { Anything } from 'other-package';", | ||
| "import { Button } from '@wordpress/components';", | ||
|
|
||
| // Default and namespace imports are not affected. | ||
| "import UI from '@wordpress/ui';", | ||
| "import * as UI from '@wordpress/ui';", | ||
|
|
||
| // Allowed @wordpress/ui components. | ||
| "import { Badge } from '@wordpress/ui';", | ||
| "import { Stack } from '@wordpress/ui';", | ||
| "import { Badge, Stack } from '@wordpress/ui';", | ||
| ], | ||
|
|
||
| invalid: [ | ||
| // Allowlist: non-allowed @wordpress/ui imports are flagged. | ||
| { | ||
| code: "import { SomeComponent } from '@wordpress/ui';", | ||
| errors: [ | ||
| { | ||
| message: | ||
| '`SomeComponent` from `@wordpress/ui` is not yet recommended for use in a WordPress environment.', | ||
| type: 'ImportSpecifier', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "import { Foo, Bar } from '@wordpress/ui';", | ||
| errors: [ | ||
| { | ||
| message: | ||
| '`Foo` from `@wordpress/ui` is not yet recommended for use in a WordPress environment.', | ||
| type: 'ImportSpecifier', | ||
| }, | ||
| { | ||
| message: | ||
| '`Bar` from `@wordpress/ui` is not yet recommended for use in a WordPress environment.', | ||
| type: 'ImportSpecifier', | ||
| }, | ||
| ], | ||
| }, | ||
| // Denylist: denied components are flagged with their message. | ||
| { | ||
| code: "import { __experimentalZStack } from '@wordpress/components';", | ||
| errors: [ | ||
| { | ||
| message: | ||
| '__experimentalZStack is planned for deprecation. Write your own CSS instead.', | ||
| type: 'ImportSpecifier', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "import { __experimentalZStack as ZStack } from '@wordpress/components';", | ||
| errors: [ | ||
| { | ||
| message: | ||
| '__experimentalZStack is planned for deprecation. Write your own CSS instead.', | ||
| type: 'ImportSpecifier', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| } ); | ||
|
|
||
| describe( 'ALLOWLIST and DENYLIST', () => { | ||
| it( 'should not have overlapping package keys', () => { | ||
| const allowlistPackages = Object.keys( ALLOWLIST ); | ||
| const denylistPackages = Object.keys( DENYLIST ); | ||
| const overlap = allowlistPackages.filter( ( pkg ) => | ||
| denylistPackages.includes( pkg ) | ||
| ); | ||
| expect( overlap ).toEqual( [] ); | ||
| } ); | ||
| } ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /** | ||
| * Allowlist: only the listed components are permitted from these packages. | ||
| * Any other named import will be flagged with the package's message. | ||
| * | ||
| * `message` supports `{{ name }}` and `{{ source }}` placeholders. | ||
| * | ||
| * @type {Record<string, { allowed: string[], message?: string }>} | ||
| */ | ||
| 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. | ||
| * | ||
| * Messages support `{{ name }}` and `{{ source }}` placeholders. | ||
| * | ||
| * @type {Record<string, Record<string, string>>} | ||
| */ | ||
| const DENYLIST = { | ||
| '@wordpress/components': { | ||
| __experimentalZStack: | ||
| '{{ name }} is planned for deprecation. Write your own CSS instead.', | ||
| }, | ||
| }; | ||
|
Comment on lines
+9
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added tests in 90fe6d0 |
||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| const rule = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: | ||
| 'Encourage the use of recommended UI components in a WordPress environment.', | ||
| url: 'https://github.com/WordPress/gutenberg/blob/HEAD/packages/eslint-plugin/docs/rules/use-recommended-components.md', | ||
| }, | ||
| schema: [], | ||
| }, | ||
| create( context ) { | ||
| return { | ||
| /** @param {import('estree').ImportDeclaration} node */ | ||
| ImportDeclaration( node ) { | ||
| if ( typeof node.source.value !== 'string' ) { | ||
| return; | ||
| } | ||
|
|
||
| const source = node.source.value; | ||
|
|
||
| const allowlistEntry = ALLOWLIST[ source ]; | ||
| const denylistEntry = DENYLIST[ source ]; | ||
|
|
||
| if ( ! allowlistEntry && ! denylistEntry ) { | ||
| return; | ||
| } | ||
|
|
||
| node.specifiers.forEach( ( specifier ) => { | ||
| if ( specifier.type !== 'ImportSpecifier' ) { | ||
| return; | ||
| } | ||
|
|
||
| const name = specifier.imported.name; | ||
|
|
||
| if ( | ||
| allowlistEntry && | ||
| ! allowlistEntry.allowed.includes( name ) | ||
| ) { | ||
| context.report( { | ||
| node: specifier, | ||
| message: resolveMessage( | ||
| allowlistEntry.message, | ||
| name, | ||
| source | ||
| ), | ||
| } ); | ||
| } | ||
|
|
||
| if ( denylistEntry?.hasOwnProperty( name ) ) { | ||
| context.report( { | ||
| node: specifier, | ||
| message: resolveMessage( | ||
| denylistEntry[ name ], | ||
| name, | ||
| source | ||
| ), | ||
| } ); | ||
| } | ||
| } ); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * @param {string|undefined} template | ||
| * @param {string} name | ||
| * @param {string} source | ||
| * @return {string} Resolved message string. | ||
| */ | ||
| function resolveMessage( template, name, source ) { | ||
| if ( ! template ) { | ||
| return `\`${ name }\` from \`${ source }\` is not recommended.`; | ||
| } | ||
| return template | ||
| .replace( /\{\{\s*name\s*\}\}/g, name ) | ||
| .replace( /\{\{\s*source\s*\}\}/g, source ); | ||
| } | ||
|
|
||
| module.exports = rule; | ||
| module.exports.ALLOWLIST = ALLOWLIST; | ||
| module.exports.DENYLIST = DENYLIST; | ||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ import { | |
| __experimentalVStack as VStack, | ||
| } from '@wordpress/components'; | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| // TODO: Revert to the `Notice` in `@wordpress/components` for now. | ||
| // eslint-disable-next-line @wordpress/use-recommended-components | ||
|
Comment on lines
+16
to
+17
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Discussed with the team in charge) |
||
| import { Notice } from '@wordpress/ui'; | ||
| import { createElement, useMemo, useState } from '@wordpress/element'; | ||
| import { useDispatch, useSelect } from '@wordpress/data'; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also update the table in
packages/eslint-plugin/README.mdThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 7568ac1