-
-
Notifications
You must be signed in to change notification settings - Fork 22
feat: add new rule no-duplicate-keyframe-selectors
#143
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
Open
snitin315
wants to merge
9
commits into
main
Choose a base branch
from
no-dup-keyframe-selectors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
57d184f
feat: add new rule `no-duplicate-keyframe-selectors`
snitin315 470e3a9
docs: update
snitin315 ae83968
chore: refactor
snitin315 53d4a68
chore: refactor
snitin315 9ac7fbc
Merge branch 'main' into no-dup-keyframe-selectors
snitin315 aee3b73
Merge remote-tracking branch 'origin/main' into no-dup-keyframe-selec…
snitin315 864aeb2
chore: update eslint config for css files
snitin315 6db3bf4
refactor: rule logic
snitin315 831fac0
refactor: improve traversing logic
snitin315 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 |
---|---|---|
|
@@ -57,4 +57,6 @@ pnpm-lock.yaml | |
|
||
# Build | ||
dist/ | ||
|
||
src/build | ||
test.css |
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,95 @@ | ||
# no-duplicate-keyframe-selectors | ||
|
||
Disallow duplicate selectors within keyframe blocks. | ||
|
||
## Background | ||
|
||
The [`@keyframes` at-rule](https://developer.mozilla.org/en-US/docs/Web/CSS/@keyframes) in CSS defines intermediate steps in an animation sequence. Each keyframe selector (like `0%`, `50%`, `100%`, `from`, or `to`) represents a point in the animation timeline and contains styles to apply at that point. | ||
|
||
```css | ||
@keyframes test { | ||
0% { | ||
opacity: 0; | ||
} | ||
|
||
100% { | ||
opacity: 1; | ||
} | ||
} | ||
``` | ||
|
||
If a selector is repeated within the same @keyframes block, the last declaration wins, potentially causing unintentional overrides or confusion. | ||
|
||
## Rule Details | ||
|
||
This rule warns when it finds a keyframe block that contains duplicate selectors. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```css | ||
/* eslint css/no-duplicate-keyframe-selectors: "error" */ | ||
|
||
@keyframes test { | ||
0% { | ||
opacity: 0; | ||
} | ||
|
||
0% { | ||
opacity: 1; | ||
} | ||
} | ||
|
||
@keyframes test { | ||
from { | ||
opacity: 0; | ||
} | ||
|
||
from { | ||
opacity: 1; | ||
} | ||
} | ||
|
||
@keyframes test { | ||
from { | ||
opacity: 0; | ||
} | ||
|
||
from { | ||
opacity: 1; | ||
} | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```css | ||
/* eslint css/no-duplicate-keyframe-selectors: "error" */ | ||
|
||
@keyframes test { | ||
0% { | ||
opacity: 0; | ||
} | ||
|
||
100% { | ||
opacity: 1; | ||
} | ||
} | ||
|
||
@keyframes test { | ||
from { | ||
opacity: 0; | ||
} | ||
|
||
to { | ||
opacity: 1; | ||
} | ||
} | ||
``` | ||
|
||
## When Not to Use It | ||
|
||
If you aren't concerned with duplicate selectors within keyframe blocks, you can safely disable this rule. | ||
|
||
## Prior Art | ||
|
||
- [`keyframe-block-no-duplicate-selectors`](https://stylelint.io/user-guide/rules/keyframe-block-no-duplicate-selectors/) |
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,81 @@ | ||
/** | ||
* @fileoverview Rule to disallow duplicate selectors within keyframe blocks. | ||
* @author Nitin Kumar | ||
*/ | ||
|
||
//----------------------------------------------------------------------------- | ||
// Type Definitions | ||
//----------------------------------------------------------------------------- | ||
|
||
/** | ||
* @import { CSSRuleDefinition } from "../types.js" | ||
* @typedef {"duplicateKeyframeSelector"} DuplicateKeyframeSelectorMessageIds | ||
* @typedef {CSSRuleDefinition<{ RuleOptions: [], MessageIds: DuplicateKeyframeSelectorMessageIds }>} DuplicateKeyframeSelectorRuleDefinition | ||
*/ | ||
|
||
//----------------------------------------------------------------------------- | ||
// Rule Definition | ||
//----------------------------------------------------------------------------- | ||
|
||
/** @type {DuplicateKeyframeSelectorRuleDefinition} */ | ||
export default { | ||
meta: { | ||
type: "problem", | ||
|
||
docs: { | ||
description: "Disallow duplicate selectors within keyframe blocks", | ||
recommended: true, | ||
url: "https://github.com/eslint/css/blob/main/docs/rules/no-duplicate-keyframe-selectors.md", | ||
}, | ||
|
||
messages: { | ||
duplicateKeyframeSelector: | ||
"Unexpected duplicate selector '{{selector}}' found within keyframe block.", | ||
}, | ||
}, | ||
|
||
create(context) { | ||
let insideKeyframes = false; | ||
const seen = new Map(); | ||
|
||
return { | ||
"Atrule[name=keyframes]"() { | ||
insideKeyframes = true; | ||
seen.clear(); | ||
}, | ||
|
||
"Atrule[name=keyframes]:exit"() { | ||
insideKeyframes = false; | ||
}, | ||
|
||
Rule(node) { | ||
if (!insideKeyframes) { | ||
return; | ||
} | ||
|
||
// @ts-ignore - children is a valid property for prelude | ||
const selector = node.prelude.children[0].children[0]; | ||
let value; | ||
if (selector.type === "Percentage") { | ||
value = `${selector.value}%`; | ||
} else if (selector.type === "TypeSelector") { | ||
value = selector.name.toLowerCase(); | ||
} else { | ||
value = selector.value; | ||
} | ||
|
||
if (seen.has(value)) { | ||
context.report({ | ||
loc: selector.loc, | ||
messageId: "duplicateKeyframeSelector", | ||
data: { | ||
selector: value, | ||
}, | ||
}); | ||
} else { | ||
seen.set(value, true); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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.