Repository navigation
ESLint: Add no-ds-tokens rule
#75872
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7cdb012
Add no-ds-tokens ESLint rule
mirka 5621ddc
Enable no-ds-tokens rule for packages/components
mirka 23babe1
Fix regex to also match tokens at start of string
mirka 60fd2d2
Add changelog
mirka 1fdb6ce
Add comment to eslintrc
mirka 971c9dd
Add word-boundary valid test case
mirka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Disallow usage of Design System tokens (no-ds-tokens) | ||
|
|
||
| Disallows any usage of Design System token CSS custom properties (beginning with `--wpds-`) in string literals and template literals. | ||
|
|
||
| This is useful in contexts where Design System tokens should not be referenced directly. It is not included in any preset and must be explicitly enabled. | ||
|
ciampo marked this conversation as resolved.
Resolved
|
||
|
|
||
| ## Rule details | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| const style = 'color: var(--wpds-color-fg-content-neutral)'; | ||
| ``` | ||
|
|
||
| ```js | ||
| const style = `border: 1px solid var(--wpds-border-color)`; | ||
| ``` | ||
|
|
||
| ```jsx | ||
| <div style={ { color: 'var(--wpds-color-fg-content-neutral)' } } /> | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| const style = 'color: var(--my-custom-prop)'; | ||
| ``` | ||
|
|
||
| ```js | ||
| const style = `border: 1px solid var(--other-prefix-token)`; | ||
| ``` | ||
|
|
||
| ```jsx | ||
| <div style={ { color: 'blue' } } /> | ||
| ``` | ||
|
ciampo marked this conversation as resolved.
Resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { RuleTester } from 'eslint'; | ||
| import rule from '../no-ds-tokens'; | ||
|
|
||
| const ruleTester = new RuleTester( { | ||
| parserOptions: { | ||
| ecmaVersion: 6, | ||
| ecmaFeatures: { | ||
| jsx: true, | ||
| }, | ||
| }, | ||
| } ); | ||
|
|
||
| ruleTester.run( 'no-ds-tokens', rule, { | ||
| valid: [ | ||
| { | ||
| code: `const style = 'color: var(--my-custom-prop)';`, | ||
| }, | ||
| { | ||
| code: `const style = 'color: blue';`, | ||
| }, | ||
| { | ||
| code: 'const style = `border: 1px solid var(--other-prefix-token)`;', | ||
| }, | ||
| { | ||
| code: `<div style={ { color: 'var(--my-custom-prop)' } } />`, | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: `const style = 'color: var(--wpds-color-fg-content-neutral)';`, | ||
| errors: [ | ||
| { | ||
| messageId: 'disallowed', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'const style = `color: var(--wpds-color-fg-content-neutral)`;', | ||
| errors: [ | ||
| { | ||
| messageId: 'disallowed', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: `<div style={ { color: 'var(--wpds-color-fg-content-neutral)' } } />`, | ||
| errors: [ | ||
| { | ||
| messageId: 'disallowed', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'const style = `border: 1px solid var(--wpds-border-color, var(--wpds-border-fallback))`;', | ||
| errors: [ | ||
|
ciampo marked this conversation as resolved.
Resolved
|
||
| { | ||
| messageId: 'disallowed', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| } ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| const DS_TOKEN_PREFIX = 'wpds-'; | ||
|
|
||
| const wpdsTokensRegex = new RegExp( `[^\\w]--${ DS_TOKEN_PREFIX }`, 'i' ); | ||
|
ciampo marked this conversation as resolved.
Resolved
|
||
|
|
||
| module.exports = /** @type {import('eslint').Rule.RuleModule} */ ( { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'Disallow any usage of --wpds-* CSS custom properties', | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| disallowed: | ||
| 'Design System tokens (--wpds-*) should not be used in this context.', | ||
| }, | ||
| }, | ||
| create( context ) { | ||
| const selector = `:matches(Literal[value=${ wpdsTokensRegex }], TemplateLiteral TemplateElement[value.raw=${ wpdsTokensRegex }])`; | ||
| return { | ||
| /** @param {import('estree').Literal | import('estree').TemplateElement} node */ | ||
| [ selector ]( node ) { | ||
| context.report( { | ||
| node, | ||
| messageId: 'disallowed', | ||
| } ); | ||
|
ciampo marked this conversation as resolved.
Resolved
|
||
| }, | ||
| }; | ||
| }, | ||
| } ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.