-
-
Notifications
You must be signed in to change notification settings - Fork 445
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
feat(linter): implement noDuplicateProperties
#4029
Open
togami2864
wants to merge
9
commits into
biomejs:main
Choose a base branch
from
togami2864:2784
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.
+624
−63
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2fba47d
chore: init rule
togami2864 bd35598
chore: tests
togami2864 45d7596
chore: init rule
togami2864 53384c9
chore: tests
togami2864 58fca26
feat: impl rule
togami2864 b2397d3
chore: doc
togami2864 aa9f68a
chore: diagnostics
togami2864 65713bb
chore: just gen-lint
togami2864 b1ea322
fix: perf
togami2864 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
148 changes: 85 additions & 63 deletions
148
crates/biome_configuration/src/analyzer/linter/rules.rs
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
85 changes: 85 additions & 0 deletions
85
crates/biome_css_analyze/src/lint/nursery/no_duplicate_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,85 @@ | ||
use biome_analyze::{context::RuleContext, declare_lint_rule, Rule, RuleDiagnostic, RuleSource}; | ||
use biome_console::markup; | ||
use biome_css_syntax::CssDeclarationOrRuleList; | ||
use biome_rowan::{AstNode, TextRange}; | ||
use rustc_hash::FxHashSet; | ||
|
||
use crate::services::semantic::Semantic; | ||
|
||
declare_lint_rule! { | ||
/// Disallow duplicate properties within declaration blocks. | ||
/// | ||
/// This rule checks the declaration blocks for duplicate properties. It ignores custom properties. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```css,expect_diagnostic | ||
/// a { | ||
/// color: pink; | ||
/// color: orange; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```css | ||
/// a { | ||
/// color: pink; | ||
/// background: orange; | ||
/// } | ||
/// ``` | ||
/// | ||
pub NoDuplicateProperties { | ||
version: "next", | ||
name: "noDuplicateProperties", | ||
language: "css", | ||
recommended: true, | ||
sources: &[RuleSource::Stylelint("declaration-block-no-duplicate-properties")], | ||
} | ||
} | ||
|
||
impl Rule for NoDuplicateProperties { | ||
type Query = Semantic<CssDeclarationOrRuleList>; | ||
type State = TextRange; | ||
type Signals = Vec<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Vec<Self::State> { | ||
let node = ctx.query(); | ||
let model = ctx.model(); | ||
|
||
let rule = model.get_rule_by_range(node.range()).unwrap(); | ||
|
||
let mut duplicates = Vec::new(); | ||
let mut seen = FxHashSet::default(); | ||
|
||
for declaration in rule.declarations.iter() { | ||
let property = &declaration.property; | ||
let prop_name = property.name.to_lowercase(); | ||
let is_custom_propety = prop_name.starts_with("--"); | ||
|
||
if !seen.insert(prop_name) && !is_custom_propety { | ||
dyc3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
duplicates.push(property.range); | ||
} | ||
} | ||
|
||
duplicates | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, span: &Self::State) -> Option<RuleDiagnostic> { | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
span, | ||
markup! { | ||
"Duplicate properties are not allowed." | ||
}, | ||
) | ||
.note(markup! { | ||
"Consider removing the duplicate property." | ||
}), | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
crates/biome_css_analyze/tests/specs/nursery/noDuplicateProperties/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,44 @@ | ||
a { | ||
color: pink; | ||
color: orange; | ||
} | ||
|
||
a { | ||
color: pink; | ||
color: pink; | ||
color: pink; | ||
} | ||
|
||
a { | ||
color: pink; | ||
color: pink; | ||
color: orange; | ||
} | ||
|
||
a { | ||
color: pink; | ||
background: orange; | ||
color: orange; | ||
} | ||
|
||
a { | ||
color: pink; | ||
background: orange; | ||
background: pink; | ||
} | ||
|
||
a { color: pink; { &:hover { color: orange; color: black; } } } | ||
|
||
a { color: pink; @media { color: orange; color: black; } } | ||
|
||
@media { color: orange; .foo { color: black; color: white; } } | ||
|
||
a { color: pink; @media { color: orange; &::before { color: black; color: white; } } } | ||
|
||
a { color: pink; @media { color: orange; .foo { color: black; color: white; } } } | ||
|
||
a { -webkit-border-radius: 12px; -webkit-border-radius: 10px; } | ||
|
||
a { color: red !important; color: blue; } | ||
|
||
a { color: red !important; color: blue !important; } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could add and use an alternative to
to_ascii_lowercase_cow
inbiome_string_case
egto_lowercase_cow