Skip to content

Commit ffef936

Browse files
committed
docs(consistent-selector-style): documented rule
1 parent 608a102 commit ffef936

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

docs/rules/consistent-selector-style.md

+53-11
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,79 @@ description: 'enforce a consistent style for CSS selectors'
1313

1414
## :book: Rule Details
1515

16-
This rule reports ???.
17-
18-
<ESLintCodeBlock>
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.
1917

2018
<!--eslint-skip-->
2119

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

3469
## :wrench: Options
3570

3671
```json
3772
{
38-
"svelte/consistent-selector-style": ["error", {}]
73+
"svelte/consistent-selector-style": [
74+
"error",
75+
{
76+
"checkGlobal": false,
77+
"style": ["type", "id", "class"]
78+
}
79+
]
3980
}
4081
```
4182

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

4486
## :books: Further Reading
4587

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

4890
## :mag: Implementation
4991

0 commit comments

Comments
 (0)