Skip to content

Commit faf90ef

Browse files
authored
feat: added the consistent-selector-style rule (#925)
1 parent 806d72a commit faf90ef

File tree

70 files changed

+2395
-35
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2395
-35
lines changed

.changeset/early-trainers-know.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': minor
3+
---
4+
5+
feat: added the `consistent-selector-style` rule

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ These rules relate to style guidelines, and are therefore quite subjective:
380380

381381
| Rule ID | Description | |
382382
|:--------|:------------|:---|
383+
| [svelte/consistent-selector-style](https://sveltejs.github.io/eslint-plugin-svelte/rules/consistent-selector-style/) | enforce a consistent style for CSS selectors | |
383384
| [svelte/derived-has-same-inputs-outputs](https://sveltejs.github.io/eslint-plugin-svelte/rules/derived-has-same-inputs-outputs/) | derived store should use same variable names between values and callback | |
384385
| [svelte/first-attribute-linebreak](https://sveltejs.github.io/eslint-plugin-svelte/rules/first-attribute-linebreak/) | enforce the location of first attribute | :wrench: |
385386
| [svelte/html-closing-bracket-new-line](https://sveltejs.github.io/eslint-plugin-svelte/rules/html-closing-bracket-new-line/) | Require or disallow a line break before tag's closing brackets | :wrench: |

docs/rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ These rules relate to style guidelines, and are therefore quite subjective:
7777

7878
| Rule ID | Description | |
7979
| :------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | :------- |
80+
| [svelte/consistent-selector-style](./rules/consistent-selector-style.md) | enforce a consistent style for CSS selectors | |
8081
| [svelte/derived-has-same-inputs-outputs](./rules/derived-has-same-inputs-outputs.md) | derived store should use same variable names between values and callback | |
8182
| [svelte/first-attribute-linebreak](./rules/first-attribute-linebreak.md) | enforce the location of first attribute | :wrench: |
8283
| [svelte/html-closing-bracket-new-line](./rules/html-closing-bracket-new-line.md) | Require or disallow a line break before tag's closing brackets | :wrench: |
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
pageClass: 'rule-details'
3+
sidebarDepth: 0
4+
title: 'svelte/consistent-selector-style'
5+
description: 'enforce a consistent style for CSS selectors'
6+
---
7+
8+
# svelte/consistent-selector-style
9+
10+
> enforce a consistent style for CSS selectors
11+
12+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
13+
14+
## :book: Rule Details
15+
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 are:
17+
18+
- Selecting by element type (i.e. tag name), such as `a {}`
19+
- Selecting by element ID, such as `#link {}`
20+
- Selecting by element class, such as `.link {}`
21+
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.
22+
23+
<!--eslint-skip-->
24+
25+
```svelte
26+
<script>
27+
/* eslint svelte/consistent-selector-style: ["error", { style: ["type", "id", "class"] }] */
28+
</script>
29+
30+
<a class="link" id="firstLink">Click me!</a>
31+
32+
<a class="link cross">Click me too!</a>
33+
34+
<b class="bold cross">Text one</b>
35+
36+
<b>Text two</b>
37+
38+
<i id="italic">Text three</i>
39+
40+
<style>
41+
/* ✓ GOOD */
42+
43+
a {
44+
color: green;
45+
}
46+
47+
#firstLink {
48+
color: green;
49+
}
50+
51+
.cross {
52+
color: green;
53+
}
54+
55+
/* ✗ BAD */
56+
57+
/* Can use a type selector */
58+
.link {
59+
color: red;
60+
}
61+
62+
/* Can use an ID selector (but not a type selector) */
63+
.bold {
64+
color: red;
65+
}
66+
67+
/* Can use a type selector */
68+
#italic {
69+
color: red;
70+
}
71+
</style>
72+
```
73+
74+
## :wrench: Options
75+
76+
```json
77+
{
78+
"svelte/consistent-selector-style": [
79+
"error",
80+
{
81+
"checkGlobal": false,
82+
"style": ["type", "id", "class"]
83+
}
84+
]
85+
}
86+
```
87+
88+
- `checkGlobal` ... Whether to check styles in `:global` blocks as well. Default `false`.
89+
- `style` ... A list of style preferences. Default `["type", "id", "class"]`.
90+
91+
## :books: Further Reading
92+
93+
- [CSS selector documentation](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors)
94+
95+
## :mag: Implementation
96+
97+
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/consistent-selector-style.ts)
98+
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/consistent-selector-style.ts)

packages/eslint-plugin-svelte/src/rule-types.ts

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export interface RuleOptions {
3434
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/comment-directive/
3535
*/
3636
'svelte/comment-directive'?: Linter.RuleEntry<SvelteCommentDirective>
37+
/**
38+
* enforce a consistent style for CSS selectors
39+
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/consistent-selector-style/
40+
*/
41+
'svelte/consistent-selector-style'?: Linter.RuleEntry<SvelteConsistentSelectorStyle>
3742
/**
3843
* derived store should use same variable names between values and callback
3944
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/derived-has-same-inputs-outputs/
@@ -387,6 +392,12 @@ type SvelteButtonHasType = []|[{
387392
type SvelteCommentDirective = []|[{
388393
reportUnusedDisableDirectives?: boolean
389394
}]
395+
// ----- svelte/consistent-selector-style -----
396+
type SvelteConsistentSelectorStyle = []|[{
397+
checkGlobal?: boolean
398+
399+
style?: []|[("class" | "id" | "type")]|[("class" | "id" | "type"), ("class" | "id" | "type")]|[("class" | "id" | "type"), ("class" | "id" | "type"), ("class" | "id" | "type")]
400+
}]
390401
// ----- svelte/first-attribute-linebreak -----
391402
type SvelteFirstAttributeLinebreak = []|[{
392403
multiline?: ("below" | "beside")

0 commit comments

Comments
 (0)