Skip to content

Commit 36d5a96

Browse files
dustin-jwarnest00
authored andcommitted
Bug 1988862 - Add a Stylelint rule to enforce using space tokens r=frontend-codestyle-reviewers,hjones
- Add tests for margin shorthand - Add tests for padding shorthand - Add tests for inset shorthand - Add tests for gap shorthand - Add tests for margin/padding block/inline shorthand - Add tests for margin/padding longhand properties - Remove tokens-table values from auto-fixable list - Add tests for inset block/inline, plus top, left, right, and bottom - Add tests for column-gap and row-gap - Add tests for keywords - Add files to be ignored for rollout - Add tests for variable fallbacks Differential Revision: https://phabricator.services.mozilla.com/D267857
1 parent 25553ff commit 36d5a96

File tree

6 files changed

+1653
-0
lines changed

6 files changed

+1653
-0
lines changed

.stylelintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ module.exports = {
274274
"stylelint-plugin-mozilla/use-border-color-tokens": true,
275275
"stylelint-plugin-mozilla/use-font-size-tokens": true,
276276
"stylelint-plugin-mozilla/use-font-weight-tokens": true,
277+
"stylelint-plugin-mozilla/use-space-tokens": true,
277278
},
278279

279280
overrides: [
@@ -418,6 +419,7 @@ module.exports = {
418419
"stylelint-plugin-mozilla/use-border-color-tokens": false,
419420
"stylelint-plugin-mozilla/use-font-size-tokens": false,
420421
"stylelint-plugin-mozilla/use-font-weight-tokens": false,
422+
"stylelint-plugin-mozilla/use-space-tokens": false,
421423
},
422424
},
423425
{
@@ -429,6 +431,7 @@ module.exports = {
429431
rules: {
430432
"stylelint-plugin-mozilla/use-border-radius-tokens": true,
431433
"stylelint-plugin-mozilla/use-border-color-tokens": false,
434+
"stylelint-plugin-mozilla/use-space-tokens": false,
432435
},
433436
},
434437
],
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
================
2+
use-space-tokens
3+
================
4+
5+
This rule checks that CSS declarations use space design token variables
6+
instead of hardcoded values. This ensures consistent spacing (e.g. margins,
7+
padding, gaps, etc.) across the application and makes it easier to maintain
8+
design system consistency.
9+
10+
Examples of incorrect code for this rule:
11+
-----------------------------------------
12+
13+
.. code-block:: css
14+
15+
.custom-button {
16+
padding: 0.5rem;
17+
}
18+
19+
.. code-block:: css
20+
21+
.card {
22+
margin-inline: 8px;
23+
}
24+
25+
.. code-block:: css
26+
27+
.overlay {
28+
inset: 1rem;
29+
}
30+
31+
.. code-block:: css
32+
33+
.grid {
34+
gap: 4px 12px;
35+
}
36+
37+
Examples of correct token usage for this rule:
38+
----------------------------------------------
39+
40+
.. code-block:: css
41+
42+
.custom-button {
43+
padding-block: var(--space-small);
44+
}
45+
46+
.. code-block:: css
47+
48+
.custom-button {
49+
padding-inline: var(--space-medium);
50+
}
51+
52+
.. code-block:: css
53+
54+
.custom-button {
55+
column-gap: var(--space-xxsmall);
56+
}
57+
58+
.. code-block:: css
59+
60+
.custom-button {
61+
margin-block-start: var(--space-large);
62+
}
63+
64+
.. code-block:: css
65+
66+
/* Local CSS variables that reference valid space tokens are allowed */
67+
:root {
68+
--custom-space: var(--space-xsmall);
69+
}
70+
71+
.custom-button {
72+
padding: var(--custom-space);
73+
}
74+
75+
.. code-block:: css
76+
77+
.custom-button {
78+
margin-inline-end: var(--custom-space, --space-xlarge);
79+
}
80+
81+
82+
The rule also allows these values to be non-token values:
83+
84+
.. code-block:: css
85+
86+
.inherited-inset {
87+
inset: inherit;
88+
}
89+
90+
.. code-block:: css
91+
92+
.unset-padding {
93+
padding: unset;
94+
}
95+
96+
.. code-block:: css
97+
98+
.initial-row-gap {
99+
row-gap: initial;
100+
}

0 commit comments

Comments
 (0)