Skip to content

Commit e2829bf

Browse files
committed
docs(consistent-selector-style): documented rule
1 parent 7143dca commit e2829bf

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

docs/rules/consistent-selector-style.md

+53-7
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,59 @@ description: 'enforce a consistent style for CSS selectors'
1313

1414
## :book: Rule Details
1515

16-
This rule reports ???.
16+
This rule allows you to set a preferred style for your CSS (& other style language) selectors. In CSS, there is a wide list of options for selecting elements, however, the three most basic types select by element type (i.e. tag name), ID or class. This rule allows you to set a preference for some of these three styles over others. Not all selectors can be used in all situations, however. While class selectors can be used in any situation, ID selectors can only be used to select a single element and type selectors are only applicable when the list of selected elements is the list of all elements of the particular type. To help with this, the rule accepts a list of selector style preferences and reports situations when the given selector can be rewritten using a more preferred style.
1717

1818
<ESLintCodeBlock>
1919

2020
<!--eslint-skip-->
2121

2222
```svelte
2323
<script>
24-
/* eslint svelte/consistent-selector-style: "error" */
24+
/* eslint svelte/consistent-selector-style: ["error", { style: ["type", "id", "class"] }] */
2525
</script>
2626
27-
<!-- ✓ GOOD -->
27+
<a class="link" id="firstLink">Click me!</a>
2828
29-
<!-- ✗ BAD -->
29+
<a class="link cross">Click me too!</a>
30+
31+
<b class="bold cross">Text one</b>
32+
33+
<b>Text two</b>
34+
35+
<i id="italic">Text three</i>
36+
37+
<style>
38+
/* ✓ GOOD */
39+
40+
a {
41+
color: green;
42+
}
43+
44+
#firstLink {
45+
color: green;
46+
}
47+
48+
.cross {
49+
color: green;
50+
}
51+
52+
/* ✗ BAD */
53+
54+
/* Can use a type selector */
55+
.link {
56+
color: red;
57+
}
58+
59+
/* Can use an ID selector (but not a type selector) */
60+
.bold {
61+
color: red;
62+
}
63+
64+
/* Can use a type selector */
65+
#italic {
66+
color: red;
67+
}
68+
</style>
3069
```
3170

3271
</ESLintCodeBlock>
@@ -35,15 +74,22 @@ This rule reports ???.
3574

3675
```json
3776
{
38-
"svelte/consistent-selector-style": ["error", {}]
77+
"svelte/consistent-selector-style": [
78+
"error",
79+
{
80+
"checkGlobal": false,
81+
"style": ["type", "id", "class"]
82+
}
83+
]
3984
}
4085
```
4186

42-
-
87+
- `checkGlobal` ... Whether to check styles in `:global` blocks as well. Default `false`.
88+
- `style` ... A list of style preferences. Default `["type", "id", "class"]`.
4389

4490
## :books: Further Reading
4591

46-
-
92+
- [CSS selector documentation](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors)
4793

4894
## :mag: Implementation
4995

0 commit comments

Comments
 (0)