Skip to content

Commit

Permalink
fix: allow component token names
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Dec 13, 2023
1 parent a5dfee5 commit 3c18e2c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/pretty-vans-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rhds/tokens": patch
---
Stylelint: avoid some false-positives when linting [component token names][wiki].

[wiki]: https://github.com/RedHat-UX/red-hat-design-system/wiki/CSS-Styles#tokens-and-naming-conventions
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

```yaml
rules:
rhds/token-values:
rhds/no-unknown-token-names:
- true
- migrations:
# reds
Expand Down
2 changes: 1 addition & 1 deletion plugins/stylelint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ specified names, it will automatically replace it with the new one.

```yaml
rules:
rhds/token-values:
rhds/no-unknown-token-name:
- true
- migrations:
# instances of gray-90 will be replaced with gray-95
Expand Down
10 changes: 9 additions & 1 deletion plugins/stylelint/rules/no-unknown-token-name.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('node:path');
const { tokens } = require('@rhds/tokens');
const stylelint = require('stylelint');
const declarationValueIndex = require('stylelint/lib/utils/declarationValueIndex');
Expand All @@ -16,6 +17,10 @@ const meta = {
/** @type {import('stylelint').Plugin} */
const ruleFunction = (_, opts, ctx) => {
return (root, result) => {
// here we assume a file structure of */rh-tagname/rh-tagname.css
const tagName = path.dirname(root.source.input.file)
.split(path.sep)
.findLast(x => x.startsWith('rh-'));
const validOptions = stylelint.utils.validateOptions(
result,
ruleName,
Expand All @@ -36,7 +41,10 @@ const ruleFunction = (_, opts, ctx) => {
parsedValue.walk(parsed => {
if (parsed.type === 'function' && parsed.value === 'var') {
const [{ value }] = parsed.nodes ?? [];
if (value.startsWith('--rh') && !tokens.has(value) || migrations.has(value)) {
if (value.startsWith('--rh') &&
!value.startsWith(`--${tagName}`) &&
!tokens.has(value) ||
migrations.has(value)) {
const message = `Expected ${value} to be a known token name`;
const { nodes: [{ sourceIndex, sourceEndIndex }] } = parsed;
const declIndex = declarationValueIndex(node);
Expand Down
4 changes: 2 additions & 2 deletions tokens/color/crayon/green.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ color:
$value: '#87BB62'
50:
$value: '#63993D'
$description:
$description:
60:
$value: '#3D7317'
$description: Alert - Success accent
70:
$value: '#204D00'
$description: Alert - Success title text
$description: Alert - Success title text

0 comments on commit 3c18e2c

Please sign in to comment.