Skip to content
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
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 85 additions & 63 deletions crates/biome_configuration/src/analyzer/linter/rules.rs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions crates/biome_css_analyze/src/lint/nursery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use biome_analyze::declare_lint_group;

pub mod no_duplicate_custom_properties;
pub mod no_duplicate_properties;
pub mod no_irregular_whitespace;
pub mod no_missing_var_function;
pub mod no_unknown_pseudo_class;
Expand All @@ -14,6 +15,7 @@ declare_lint_group! {
name : "nursery" ,
rules : [
self :: no_duplicate_custom_properties :: NoDuplicateCustomProperties ,
self :: no_duplicate_properties :: NoDuplicateProperties ,
self :: no_irregular_whitespace :: NoIrregularWhitespace ,
self :: no_missing_var_function :: NoMissingVarFunction ,
self :: no_unknown_pseudo_class :: NoUnknownPseudoClass ,
Expand Down
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();
Copy link
Contributor

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 in biome_string_case eg to_lowercase_cow

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."
}),
)
}
}
2 changes: 2 additions & 0 deletions crates/biome_css_analyze/src/options.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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; }
Loading
Loading