-
-
Notifications
You must be signed in to change notification settings - Fork 511
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_analyzer):
noDuplicateCustomProperties
- Loading branch information
Showing
11 changed files
with
247 additions
and
56 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
94 changes: 94 additions & 0 deletions
94
crates/biome_css_analyze/src/lint/nursery/no_duplicate_custom_properties.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,94 @@ | ||
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic}; | ||
use biome_console::markup; | ||
use biome_css_syntax::{CssDeclarationOrRuleList, CssLanguage}; | ||
use biome_rowan::{SyntaxToken, TextRange}; | ||
|
||
declare_rule! { | ||
/// Disallow duplicate custom properties within declaration blocks. | ||
/// | ||
/// This rule checks the declaration blocks for duplicate custom properties. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```css,expect_diagnostic | ||
/// a { --custom-property: pink; --custom-property: orange; } | ||
/// ``` | ||
/// | ||
/// ```css,expect_diagnostic | ||
/// a { --custom-property: pink; background: orange; --custom-property: orange } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```css | ||
/// a { --custom-property: pink; } | ||
/// ``` | ||
/// | ||
/// ```css | ||
/// a { --custom-property: pink; --cUstOm-prOpErtY: orange; } | ||
/// ``` | ||
/// | ||
pub NoDuplicateCustomProperties { | ||
version: "next", | ||
name: "noDuplicateCustomProperties", | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoDuplicateCustomProperties { | ||
type Query = Ast<CssDeclarationOrRuleList>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { | ||
let node = ctx.query(); | ||
let mut custom_properties: Vec<SyntaxToken<CssLanguage>> = vec![]; | ||
for item in node { | ||
if let Some(css_declaration) = item.as_css_declaration_with_semicolon() { | ||
if let Ok(property) = css_declaration.declaration().ok()?.property() { | ||
if let Some(property) = property.as_css_generic_property() { | ||
if let Some(ident) = property.name().ok()?.as_css_identifier() { | ||
let value_token = ident.value_token().ok()?; | ||
custom_properties.push(value_token); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if let Some(duplicate) = check_duplicate_custom_properties(custom_properties) { | ||
return Some(duplicate.text_trimmed_range()); | ||
} | ||
None | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> { | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
range, | ||
markup! { | ||
"Duplicate custom properties are not allowed." | ||
}, | ||
) | ||
.note(markup! { | ||
"Consider removing the duplicate custom property." | ||
}), | ||
) | ||
} | ||
} | ||
|
||
fn check_duplicate_custom_properties( | ||
custom_properties: Vec<SyntaxToken<CssLanguage>>, | ||
) -> Option<SyntaxToken<CssLanguage>> { | ||
let mut seen = std::collections::HashSet::<&str>::new(); | ||
for value in custom_properties.iter() { | ||
let trimmed_text = value.text_trimmed(); | ||
if !seen.insert(trimmed_text) { | ||
return Some(value.clone()); | ||
} | ||
} | ||
None | ||
} |
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
3 changes: 3 additions & 0 deletions
3
crates/biome_css_analyze/tests/specs/nursery/noDuplicateCustomProperties/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,3 @@ | ||
a { --custom-property: pink; --custom-property: orange; } | ||
|
||
a { --custom-property: pink; background: orange; --custom-property: orange } |
43 changes: 43 additions & 0 deletions
43
crates/biome_css_analyze/tests/specs/nursery/noDuplicateCustomProperties/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,43 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: invalid.css | ||
--- | ||
# Input | ||
```css | ||
a { --custom-property: pink; --custom-property: orange; } | ||
a { --custom-property: pink; background: orange; --custom-property: orange } | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.css:1:30 lint/nursery/noDuplicateCustomProperties ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Duplicate custom properties are not allowed. | ||
> 1 │ a { --custom-property: pink; --custom-property: orange; } | ||
│ ^^^^^^^^^^^^^^^^^ | ||
2 │ | ||
3 │ a { --custom-property: pink; background: orange; --custom-property: orange } | ||
i Consider removing the duplicate custom property. | ||
``` | ||
|
||
``` | ||
invalid.css:3:50 lint/nursery/noDuplicateCustomProperties ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Duplicate custom properties are not allowed. | ||
1 │ a { --custom-property: pink; --custom-property: orange; } | ||
2 │ | ||
> 3 │ a { --custom-property: pink; background: orange; --custom-property: orange } | ||
│ ^^^^^^^^^^^^^^^^^ | ||
4 │ | ||
i Consider removing the duplicate custom property. | ||
``` |
4 changes: 4 additions & 0 deletions
4
crates/biome_css_analyze/tests/specs/nursery/noDuplicateCustomProperties/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,4 @@ | ||
/* should not generate diagnostics */ | ||
a { --custom-property: pink; } | ||
|
||
a { --custom-property: pink; --cUstOm-prOpErtY: orange; } |
12 changes: 12 additions & 0 deletions
12
crates/biome_css_analyze/tests/specs/nursery/noDuplicateCustomProperties/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,12 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: valid.css | ||
--- | ||
# Input | ||
```css | ||
/* should not generate diagnostics */ | ||
a { --custom-property: pink; } | ||
a { --custom-property: pink; --cUstOm-prOpErtY: orange; } | ||
``` |
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.