UNPKG

10.9 kBMarkdownView Raw
1# Combining rules
2
3You can combine rules to enforce strict conventions.
4
5## `*-newline/space-before` and `*-newline/space-after` rules
6
7Say you want to enforce no space before and a single space after the colon in every declaration:
8
9<!-- prettier-ignore -->
10```css
11a { color: pink; }
12/** ↑
13 * No space before and a single space after this colon */
14```
15
16You can enforce that with:
17
18```json
19{
20 "declaration-colon-space-after": "always",
21 "declaration-colon-space-before": "never"
22}
23```
24
25Some _things_ (e.g. declaration blocks and value lists) can span more than one line. In these cases, `newline` rules and extra options can be used to provide flexibility.
26
27For example, this is the complete set of `value-list-comma-*` rules and their options:
28
29- `value-list-comma-space-after`: `"always"|"never"|"always-single-line"|"never-single-line"`
30- `value-list-comma-space-before`: `"always"|"never"|"always-single-line"|"never-single-line"`
31- `value-list-comma-newline-after`: `"always"|"always-multi-line|"never-multi-line"`
32- `value-list-comma-newline-before`: `"always"|"always-multi-line"|"never-multi-line"`
33
34Where `*-multi-line` and `*-single-line` are in reference to the value list (the _thing_). For example, given:
35
36<!-- prettier-ignore -->
37```css
38a,
39b {
40 color: red;
41 font-family: sans, serif, monospace; /* single-line value list */
42} ↑ ↑
43/** ↑ ↑
44 * The value list starts here and ends here */
45```
46
47There is only a single-line value list in this example. The selector is multi-line, as is the declaration block and, as such, also the rule. But the value list isn't. The `*-multi-line` and `*-single-line` refer to the value list in the context of this rule.
48
49### Example A
50
51Say you only want to allow single-line value lists. And you want to enforce no space before and a single space after the commas:
52
53<!-- prettier-ignore -->
54```css
55a {
56 font-family: sans, serif, monospace;
57 box-shadow: 1px 1px 1px red, 2px 2px 1px 1px blue inset, 2px 2px 1px 2px blue inset;
58}
59```
60
61You can enforce that with:
62
63```json
64{
65 "value-list-comma-space-after": "always",
66 "value-list-comma-space-before": "never"
67}
68```
69
70### Example B
71
72Say you want to allow both single-line and multi-line value lists. You want there to be a single space after the commas in the single-line lists and no space before the commas in both the single-line and multi-line lists:
73
74<!-- prettier-ignore -->
75```css
76a {
77 font-family: sans, serif, monospace; /* single-line value list with space after, but no space before */
78 box-shadow: 1px 1px 1px red, /* multi-line value list ... */
79 2px 2px 1px 1px blue inset, /* ... with newline after, ... */
80 2px 2px 1px 2px blue inset; /* ... but no space before */
81}
82```
83
84You can enforce that with:
85
86```json
87{
88 "value-list-comma-newline-after": "always-multi-line",
89 "value-list-comma-space-after": "always-single-line",
90 "value-list-comma-space-before": "never"
91}
92```
93
94### Example C
95
96Say you want to allow both single-line and multi-line value lists. You want there to be no space before the commas in the single-line lists and always a space after the commas in both lists:
97
98<!-- prettier-ignore -->
99```css
100a {
101 font-family: sans, serif, monospace;
102 box-shadow: 1px 1px 1px red
103 , 2px 2px 1px 1px blue inset
104 , 2px 2px 1px 2px blue inset;
105}
106```
107
108You can enforce that with:
109
110```json
111{
112 "value-list-comma-newline-before": "always-multi-line",
113 "value-list-comma-space-after": "always",
114 "value-list-comma-space-before": "never-single-line"
115}
116```
117
118### Example D
119
120The rules are flexible enough to enforce entirely different conventions for single-line and multi-line lists. Say you want to allow both single-line and multi-line value lists. You want the single-line lists to have a single space before and after the colons. Whereas you want the multi-line lists to have a single newline before the commas, but no space after:
121
122<!-- prettier-ignore -->
123```css
124a {
125 font-family: sans , serif , monospace; /* single-line list with a single space before and after the comma */
126 box-shadow: 1px 1px 1px red /* multi-line list ... */
127 ,2px 2px 1px 1px blue inset /* ... with newline before, ... */
128 ,2px 2px 1px 2px blue inset; /* ... but no space after the comma */
129}
130```
131
132You can enforce that with:
133
134```json
135{
136 "value-list-comma-newline-after": "never-multi-line",
137 "value-list-comma-newline-before": "always-multi-line",
138 "value-list-comma-space-after": "always-single-line",
139 "value-list-comma-space-before": "always-single-line"
140}
141```
142
143### Example E
144
145Say you want to disable single-line blocks:
146
147<!-- prettier-ignore -->
148```css
149 a { color: red; }
150/** ↑
151 * Declaration blocks like this */
152```
153
154Use the `block-opening-brace-newline-after` and `block-opening-brace-newline-before` rules together. For example, this config:
155
156```json
157{
158 "block-opening-brace-newline-after": ["always"],
159 "block-closing-brace-newline-before": ["always"]
160}
161```
162
163Would allow:
164
165<!-- prettier-ignore -->
166```css
167a {
168 color: red;
169}
170```
171
172But not these patterns:
173
174<!-- prettier-ignore -->
175```css
176a { color: red;
177}
178
179a {
180color: red; }
181
182a { color: red; }
183```
184
185To allow single-line blocks but enforce newlines with multi-line blocks, use the `"always-multi-line"` option for both rules.
186
187## `*-empty-line-before` and `*-max-empty-lines` rules
188
189These rules work together to control where empty lines are allowed.
190
191Each _thing_ is responsible for pushing itself away from the _preceding thing_, rather than pushing the _subsequent thing_ away. This consistency is to avoid conflicts and is why there aren't any `*-empty-line-after` rules in stylelint.
192
193Say you want to enforce the following:
194
195<!-- prettier-ignore -->
196```css
197a {
198 background: green;
199 color: red;
200
201 @media (min-width: 30em) {
202 color: blue;
203 }
204}
205
206b {
207 --custom-property: green;
208
209 background: pink;
210 color: red;
211}
212```
213
214You can do that with:
215
216```json
217{
218 "at-rule-empty-line-before": [
219 "always",
220 {
221 "except": ["first-nested"]
222 }
223 ],
224 "custom-property-empty-line-before": [
225 "always",
226 {
227 "except": ["after-custom-property", "first-nested"]
228 }
229 ],
230 "declaration-empty-line-before": [
231 "always",
232 {
233 "except": ["after-declaration", "first-nested"]
234 }
235 ],
236 "block-closing-brace-empty-line-before": "never",
237 "rule-empty-line-before": ["always-multi-line"]
238}
239```
240
241We recommend that you set your primary option (e.g. `"always"` or `"never"`) to whatever is your most common occurrence and define your exceptions with the `except` optional secondary options. There are many values for the `except` option e.g. `first-nested`, `after-comment` etc.
242
243The `*-empty-line-before` rules control whether there must never be an empty line or whether there must be _one or more_ empty lines before a _thing_. The `*-max-empty-lines` rules complement this by controlling _the number_ of empty lines within _things_. The `max-empty-lines` rule sets a limit across the entire source. A _stricter_ limit can then be set within _things_ using the likes of `function-max-empty-lines`, `selector-max-empty-lines` and `value-list-max-empty-lines`.
244
245For example, say you want to enforce the following:
246
247<!-- prettier-ignore -->
248```css
249a,
250b {
251 box-shadow:
252 inset 0 2px 0 #dcffa6,
253 0 2px 5px #000;
254}
255
256c {
257 transform:
258 translate(
259 1,
260 1
261 );
262}
263```
264
265i.e. a maximum of 1 empty line within the whole source, but no empty lines within functions, selector lists and value lists.
266
267You can do that with:
268
269```json
270{
271 "function-max-empty-lines": 0,
272 "max-empty-lines": 1,
273 "selector-list-max-empty-lines": 0,
274 "value-list-max-empty-lines": 0
275}
276```
277
278## `*-whitelist`, `*-blacklist`, `color-named` and applicable `*-no-*` rules
279
280These rules work together to (dis)allow language features and constructs.
281
282There are `*-whitelist` and `*-blacklist` rules that target the constructs of the CSS language: at-rules, functions, declarations (i.e. property-value pairs), properties and units. These rules (dis)allow any language features that make use of these constructs (e.g. `@media`, `rgb()`). However, there are features not caught by these `*-whitelist` and `*-blacklist` rules (or are, but would require complex regex to configure). There are individual rules, usually a `*-no-*` rule (e.g. `color-no-hex` and `selector-no-id`), to disallow each of these features.
283
284Say you want to disallow the `@debug` language extension. You can do that using either the `at-rule-blacklist` or `at-rule-whitelist` rules because the `@debug` language extension uses the at-rule construct e.g.
285
286```json
287{
288 "at-rule-blacklist": ["debug"]
289}
290```
291
292Say you want to, for whatever reason, disallow the whole at-rule construct. You can do that using:
293
294```json
295{
296 "at-rule-whitelist": []
297}
298```
299
300Say you want to disallow the value `none` for the `border` properties. You can do that using either the `declaration-property-value-blacklist` or `declaration-property-value-whitelist` e.g.
301
302```json
303{
304 "declaration-property-value-blacklist": [
305 {
306 "/^border/": ["none"]
307 }
308 ]
309}
310```
311
312## `color-*` and `function-*` rules
313
314Most `<color>` values are _functions_. As such, they can be (dis)allowed using either the `function-blacklist` or `function-whitelist` rules. Two other color representations aren't functions: named colors and hex colors. There are two specific rules that (dis)allow these: `color-named` and `color-no-hex`, respectively.
315
316Say you want to enforce using a named color _if one exists for your chosen color_ and use `hwb` color if one does not, e.g.:
317
318<!-- prettier-ignore -->
319```css
320a {
321 background: hwb(235, 0%, 0%); /* there is no named color equivalent for this color */
322 color: black;
323}
324```
325
326If you're taking a whitelisting approach, you can do that with:
327
328```json
329{
330 "color-named": "always-where-possible",
331 "color-no-hex": true,
332 "function-whitelist": ["hwb"]
333}
334```
335
336Or, if you're taking a blacklisting approach:
337
338```json
339{
340 "color-named": "always-where-possible",
341 "color-no-hex": true,
342 "function-blacklist": ["/^rgb/", "/^hsl/", "gray"]
343}
344```
345
346This approach scales to when language extensions (that use the two built-in extendable syntactic constructs of at-rules and functions) are used. For example, say you want to disallow all standard color presentations in favour of using a custom color representation function, e.g. `my-color(red with a dash of green / 5%)`. You can do that with:
347
348```json
349{
350 "color-named": "never",
351 "color-no-hex": true,
352 "function-whitelist": ["my-color"]
353}
354```
355
356## Manage conflicts
357
358Each rule stands alone, so sometimes it's possible to configure rules such that they conflict with one another. For example, you could turn on two conflicting blacklist and whitelist rules, e.g. `unit-blacklist` and `unit-whitelist`.
359
360It's your responsibility as the configuration author to resolve these conflicts.