-
-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(biome_css_analyze): noImportantInKeyframe (#2542)
Co-authored-by: ty <[email protected]> Co-authored-by: Emanuele Stoppa <[email protected]>
- Loading branch information
1 parent
edf02c1
commit c601268
Showing
11 changed files
with
209 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
90 changes: 90 additions & 0 deletions
90
crates/biome_css_analyze/src/lint/nursery/no_important_in_keyframe.rs
This file contains 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,90 @@ | ||
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource}; | ||
use biome_console::markup; | ||
use biome_css_syntax::{ | ||
AnyCssDeclarationBlock, AnyCssKeyframesItem, CssDeclarationImportant, CssKeyframesBlock, | ||
}; | ||
use biome_rowan::AstNode; | ||
|
||
declare_rule! { | ||
/// Disallow invalid `!important` within keyframe declarations | ||
/// | ||
/// Using `!important` within keyframes declarations is completely ignored in some browsers. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```css,expect_diagnostic | ||
/// @keyframes foo { | ||
/// from { | ||
/// opacity: 0; | ||
/// } | ||
/// to { | ||
/// opacity: 1 !important; | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```css | ||
/// @keyframes foo { | ||
/// from { | ||
/// opacity: 0; | ||
/// } | ||
/// to { | ||
/// opacity: 1; | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
pub NoImportantInKeyframe { | ||
version: "next", | ||
name: "noImportantInKeyframe", | ||
recommended: true, | ||
sources:&[RuleSource::Stylelint("keyframe-declaration-no-important")], | ||
} | ||
} | ||
|
||
impl Rule for NoImportantInKeyframe { | ||
type Query = Ast<CssKeyframesBlock>; | ||
type State = CssDeclarationImportant; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { | ||
let node = ctx.query(); | ||
for item in node.items() { | ||
let AnyCssKeyframesItem::CssKeyframesItem(keyframe_item) = item else { | ||
return None; | ||
}; | ||
let AnyCssDeclarationBlock::CssDeclarationBlock(block_declaration) = | ||
keyframe_item.block().ok()? | ||
else { | ||
return None; | ||
}; | ||
for colon_declaration in block_declaration.declarations() { | ||
if let Some(important) = colon_declaration.declaration().ok()?.important() { | ||
return Some(important); | ||
} | ||
} | ||
} | ||
None | ||
} | ||
|
||
fn diagnostic(_ctx: &RuleContext<Self>, node: &Self::State) -> Option<RuleDiagnostic> { | ||
let span = node.range(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
span, | ||
markup! { | ||
"Using "<Emphasis>"!important"</Emphasis>" within keyframes declaration is completely ignored in some browsers." | ||
}, | ||
) | ||
.note(markup! { | ||
"Consider removing useless "<Emphasis>"!important"</Emphasis>" declaration." | ||
}), | ||
) | ||
} | ||
} |
This file contains 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
8 changes: 8 additions & 0 deletions
8
crates/biome_css_analyze/tests/specs/nursery/noImportantInKeyframe/invalid.css
This file contains 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,8 @@ | ||
@keyframes foo { | ||
from { | ||
opacity: 0; | ||
} | ||
to { | ||
opacity: 1 !important; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
crates/biome_css_analyze/tests/specs/nursery/noImportantInKeyframe/invalid.css.snap
This file contains 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,33 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: invalid.css | ||
--- | ||
# Input | ||
```css | ||
@keyframes foo { | ||
from { | ||
opacity: 0; | ||
} | ||
to { | ||
opacity: 1 !important; | ||
} | ||
} | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.css:6:16 lint/nursery/noImportantInKeyframe ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Using !important within keyframes declaration is completely ignored in some browsers. | ||
4 │ } | ||
5 │ to { | ||
> 6 │ opacity: 1 !important; | ||
│ ^^^^^^^^^^ | ||
7 │ } | ||
8 │ } | ||
i Consider removing useless !important declaration. | ||
``` |
10 changes: 10 additions & 0 deletions
10
crates/biome_css_analyze/tests/specs/nursery/noImportantInKeyframe/valid.css
This file contains 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,10 @@ | ||
@keyframes foo { | ||
from { | ||
opacity: 0; | ||
} | ||
to { | ||
opacity: 1; | ||
} | ||
} | ||
|
||
a { color: pink !important; } |
17 changes: 17 additions & 0 deletions
17
crates/biome_css_analyze/tests/specs/nursery/noImportantInKeyframe/valid.css.snap
This file contains 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,17 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: valid.css | ||
--- | ||
# Input | ||
```css | ||
@keyframes foo { | ||
from { | ||
opacity: 0; | ||
} | ||
to { | ||
opacity: 1; | ||
} | ||
} | ||
a { color: pink !important; } | ||
``` |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.