-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat: added the consistent-selector-style
rule
#925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ota-meshi
merged 9 commits into
sveltejs:main
from
marekdedic:consistent-selector-style
Jan 15, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ce69f89
chore(consistent-selector-style): added rule scaffolding
marekdedic 7b59f7d
test(consistent-selector-style): added rule tests
marekdedic 65d0fd3
feat(consistent-selector-style): added rule implementation
marekdedic f973859
feat(consistent-selector-style): enabled support for partial style
marekdedic 399fbc6
feat(consistent-selector-style): added support for checking :global s…
marekdedic e2157ea
docs(consistent-selector-style): documented rule
marekdedic b1253f7
docs(consistent-selector-style): Added clearer description of selecto…
marekdedic 9194000
chore(consistent-selector-style): shorter array equality function
marekdedic 70e1add
chore(consistent-selector-style): moved functions into create() and r…
marekdedic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-svelte': minor | ||
--- | ||
|
||
feat: added the `consistent-selector-style` rule |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
--- | ||
pageClass: 'rule-details' | ||
sidebarDepth: 0 | ||
title: 'svelte/consistent-selector-style' | ||
description: 'enforce a consistent style for CSS selectors' | ||
--- | ||
|
||
# svelte/consistent-selector-style | ||
|
||
> enforce a consistent style for CSS selectors | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
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: | ||
|
||
- Selecting by element type (i.e. tag name), such as `a {}` | ||
- Selecting by element ID, such as `#link {}` | ||
- Selecting by element class, such as `.link {}` | ||
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. | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/consistent-selector-style: ["error", { style: ["type", "id", "class"] }] */ | ||
</script> | ||
|
||
<a class="link" id="firstLink">Click me!</a> | ||
|
||
<a class="link cross">Click me too!</a> | ||
|
||
<b class="bold cross">Text one</b> | ||
|
||
<b>Text two</b> | ||
|
||
<i id="italic">Text three</i> | ||
|
||
<style> | ||
/* ✓ GOOD */ | ||
|
||
a { | ||
color: green; | ||
} | ||
|
||
#firstLink { | ||
color: green; | ||
} | ||
|
||
.cross { | ||
color: green; | ||
} | ||
|
||
/* ✗ BAD */ | ||
|
||
/* Can use a type selector */ | ||
.link { | ||
color: red; | ||
} | ||
|
||
/* Can use an ID selector (but not a type selector) */ | ||
.bold { | ||
color: red; | ||
} | ||
|
||
/* Can use a type selector */ | ||
#italic { | ||
color: red; | ||
} | ||
</style> | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"svelte/consistent-selector-style": [ | ||
"error", | ||
{ | ||
"checkGlobal": false, | ||
"style": ["type", "id", "class"] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
- `checkGlobal` ... Whether to check styles in `:global` blocks as well. Default `false`. | ||
- `style` ... A list of style preferences. Default `["type", "id", "class"]`. | ||
|
||
## :books: Further Reading | ||
|
||
- [CSS selector documentation](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/consistent-selector-style.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/consistent-selector-style.ts) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.