Skip to content

Commit 933d71c

Browse files
authored
feat: allow relative-size and global values in relative-font-units (eslint#214)
* feat: allow relative-size and global values in relative-font-units * fix example * make disallowed font size keywords case-insensitive
1 parent 0aa7b97 commit 933d71c

File tree

3 files changed

+379
-127
lines changed

3 files changed

+379
-127
lines changed

docs/rules/relative-font-units.md

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ Generally, relative units such as `rem` or `em` are preferred over absolute ones
1919

2020
## Rule Details
2121

22-
This rule enforces the use of relative units for font size.
22+
This rule enforces the use of relative units for font size. Relative-size keywords (`smaller`, `larger`) and global values (`inherit`, `initial`, `revert`, `revert-layer`, `unset`) are always allowed. Conversely, absolute-size keywords (`xx-small`, `x-small`, `small`, `medium`, `large`, `x-large`, `xx-large`, `xxx-large`) along with the `math` keyword and any absolute length units (such as `px` or `pt`), are never allowed.
2323

2424
## Options
2525

2626
This rule accepts an option which is an object with the following property:
2727

2828
- `allowUnits` (default: `["rem"]`) - Specify an array of relative units that are allowed to be used. You can use the following units:
29-
- **%**: Represents the "percentage" of the parent elements font size, allowing the text to scale relative to its container.
29+
- **%**: Represents the "percentage" of the parent element's font size, allowing the text to scale relative to its container.
3030
- **cap**: Represents the "cap height" (nominal height of capital letters) of the element's font.
3131
- **ch**: Represents the width or advance measure of the "0" glyph in the element's font.
3232
- **em**: Represents the calculated font-size of the element.
@@ -45,40 +45,60 @@ Example of **incorrect** code for default `{ allowUnits: ["rem"] }` option:
4545
```css
4646
/* eslint css/relative-font-units: ["error", { allowUnits: ["rem"] }] */
4747

48-
a {
48+
.a {
4949
font-size: 10px;
5050
}
5151

52-
b {
52+
.b {
5353
font-size: 2em;
5454
}
5555

56-
c {
56+
.c {
5757
font-size: small;
5858
}
59+
60+
.d {
61+
font-size: math;
62+
}
5963
```
6064

6165
Example of **correct** code for default `{ allowUnits: ["rem"] }` option:
6266

6367
```css
6468
/* eslint css/relative-font-units: ["error", { allowUnits: ["rem"] }] */
6569

66-
a {
70+
.a {
6771
font-size: 2rem;
6872
}
6973

70-
b {
74+
.b {
7175
font-size: 1rem;
7276
width: 20px;
7377
}
7478

75-
c {
79+
.c {
7680
font-size: var(--foo);
7781
}
7882

79-
d {
83+
.d {
8084
font-size: calc(2rem + 2px);
8185
}
86+
87+
.e {
88+
font-size: smaller;
89+
}
90+
91+
.f {
92+
font-size: larger;
93+
}
94+
95+
.g {
96+
font-size: inherit;
97+
}
98+
99+
.h {
100+
font-size: unset;
101+
}
82102
```
83103

84104
Font size can also be specified in `font` property:
@@ -88,18 +108,30 @@ Example of **correct** code for `{ allowUnits: ["em", "%"] }` option:
88108
```css
89109
/* eslint css/relative-font-units: ["error", { allowUnits: ["em", "%"] }] */
90110

91-
a {
111+
.a {
92112
font-size: 2em;
93113
}
94114

95-
b {
115+
.b {
96116
font:
97117
20% Arial,
98118
sans-serif;
99119
}
100120

101-
c {
102-
font: Arial var(--foo);
121+
.c {
122+
font: var(--foo) Arial;
123+
}
124+
125+
.d {
126+
font:
127+
smaller Arial,
128+
sans-serif;
129+
}
130+
131+
.e {
132+
font:
133+
inherit Arial,
134+
sans-serif;
103135
}
104136
```
105137

src/rules/relative-font-units.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const relativeFontUnits = [
3434
"rlh",
3535
];
3636

37-
const fontSizeIdentifiers = new Set([
37+
const disallowedFontSizeKeywords = new Set([
3838
"xx-small",
3939
"x-small",
4040
"small",
@@ -43,14 +43,7 @@ const fontSizeIdentifiers = new Set([
4343
"x-large",
4444
"xx-large",
4545
"xxx-large",
46-
"smaller",
47-
"larger",
4846
"math",
49-
"inherit",
50-
"initial",
51-
"revert",
52-
"revert-layer",
53-
"unset",
5447
]);
5548

5649
//-----------------------------------------------------------------------------
@@ -111,7 +104,10 @@ export default {
111104
if (
112105
(value.type === "Dimension" &&
113106
!allowedFontUnits.includes(value.unit)) ||
114-
value.type === "Identifier" ||
107+
(value.type === "Identifier" &&
108+
disallowedFontSizeKeywords.has(
109+
value.name.toLowerCase(),
110+
)) ||
115111
(value.type === "Percentage" &&
116112
!allowedFontUnits.includes("%"))
117113
) {
@@ -138,9 +134,7 @@ export default {
138134
child => child.type === "Dimension",
139135
);
140136
const identifierNode = value.children.find(
141-
child =>
142-
child.type === "Identifier" &&
143-
fontSizeIdentifiers.has(child.name),
137+
child => child.type === "Identifier",
144138
);
145139
const percentageNode = value.children.find(
146140
child => child.type === "Percentage",
@@ -156,7 +150,11 @@ export default {
156150
loc: percentageNode?.loc,
157151
},
158152
{
159-
check: identifierNode,
153+
check:
154+
identifierNode &&
155+
disallowedFontSizeKeywords.has(
156+
identifierNode.name.toLowerCase(),
157+
),
160158
loc: identifierNode?.loc,
161159
},
162160
{

0 commit comments

Comments
 (0)