-
-
Notifications
You must be signed in to change notification settings - Fork 963
feat(lint/css): add noHexColors
#8860
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
Merged
Merged
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added the nursery rule [`noHexColors`](https://biomejs.dev/linter/rules/no-hex-colors/), which flags the use of hexadecimal color codes in CSS and suggests using named colors or RGB/RGBA/HSL/HSLA formats instead. | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
85 changes: 85 additions & 0 deletions
85
crates/biome_css_analyze/src/lint/nursery/no_hex_colors.rs
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,85 @@ | ||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_css_syntax::CssColor; | ||
| use biome_rowan::AstNode; | ||
| use biome_rule_options::no_hex_colors::NoHexColorsOptions; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Disallow hex colors. | ||
| /// | ||
| /// While hex colors are widely supported and compact, they can be less readable | ||
| /// and have limitations in terms of color representation compared to color models | ||
| /// like HSL or OKLCH. This rule encourages the use of more expressive color formats. | ||
| /// | ||
| /// This rule is inspired by the Stylelint rule | ||
| /// [`color-no-hex`](https://stylelint.io/user-guide/rules/color-no-hex/). | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```css,expect_diagnostic | ||
| /// a { color: #000; } | ||
| /// ``` | ||
| /// | ||
| /// ```css,expect_diagnostic | ||
| /// a { color: #fff1aa; } | ||
| /// ``` | ||
| /// | ||
| /// ```css,expect_diagnostic | ||
| /// a { color: #123456aa; } | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```css | ||
| /// a { color: black; } | ||
| /// ``` | ||
| /// | ||
| /// ```css | ||
| /// a { color: rgb(0, 0, 0); } | ||
| /// ``` | ||
| /// | ||
| /// ### References | ||
| /// | ||
| /// - [MDN Web Docs on CSS color values](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value) | ||
| /// | ||
| pub NoHexColors { | ||
| version: "next", | ||
| name: "noHexColors", | ||
| language: "css", | ||
| sources: &[RuleSource::Stylelint("color-no-hex").same()], | ||
| recommended: false, | ||
| } | ||
| } | ||
|
|
||
| impl Rule for NoHexColors { | ||
| type Query = Ast<CssColor>; | ||
| type State = (); | ||
| type Signals = Option<Self::State>; | ||
| type Options = NoHexColorsOptions; | ||
|
|
||
| fn run(_ctx: &RuleContext<Self>) -> Self::Signals { | ||
| Some(()) | ||
| } | ||
|
|
||
| fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| ctx.query().range(), | ||
| markup! { | ||
| "Unexpected hex color." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "Hex colors are less readable and have limitations compared to other color models." | ||
| }) | ||
| .note(markup! { | ||
| "Consider using a named color or a color function like rgb(), hsl() or oklch(). See "<Hyperlink href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value">"MDN Web Docs on CSS color values"</Hyperlink>" for more information." | ||
| }), | ||
| ) | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
crates/biome_css_analyze/tests/specs/nursery/noHexColors/invalid.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /* should generate diagnostics */ | ||
| a { color: #000; } | ||
| a { color: #fff1aa; } | ||
| a { color: #123456aa; } | ||
| a { color: #0000000000000000; } |
98 changes: 98 additions & 0 deletions
98
crates/biome_css_analyze/tests/specs/nursery/noHexColors/invalid.css.snap
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,98 @@ | ||
| --- | ||
| source: crates/biome_css_analyze/tests/spec_tests.rs | ||
| expression: invalid.css | ||
| --- | ||
| # Input | ||
| ```css | ||
| /* should generate diagnostics */ | ||
| a { color: #000; } | ||
| a { color: #fff1aa; } | ||
| a { color: #123456aa; } | ||
| a { color: #0000000000000000; } | ||
|
|
||
| ``` | ||
|
|
||
| _Note: The parser emitted 1 diagnostics which are not shown here._ | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid.css:2:12 lint/nursery/noHexColors ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Unexpected hex color. | ||
|
|
||
| 1 │ /* should generate diagnostics */ | ||
| > 2 │ a { color: #000; } | ||
| │ ^^^^ | ||
| 3 │ a { color: #fff1aa; } | ||
| 4 │ a { color: #123456aa; } | ||
|
|
||
| i Hex colors are less readable and have limitations compared to other color models. | ||
|
|
||
| i Consider using a named color or a color function like rgb(), hsl() or oklch(). See MDN Web Docs on CSS color values for more information. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.css:3:12 lint/nursery/noHexColors ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Unexpected hex color. | ||
|
|
||
| 1 │ /* should generate diagnostics */ | ||
| 2 │ a { color: #000; } | ||
| > 3 │ a { color: #fff1aa; } | ||
| │ ^^^^^^^ | ||
| 4 │ a { color: #123456aa; } | ||
| 5 │ a { color: #0000000000000000; } | ||
|
|
||
| i Hex colors are less readable and have limitations compared to other color models. | ||
|
|
||
| i Consider using a named color or a color function like rgb(), hsl() or oklch(). See MDN Web Docs on CSS color values for more information. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.css:4:12 lint/nursery/noHexColors ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Unexpected hex color. | ||
|
|
||
| 2 │ a { color: #000; } | ||
| 3 │ a { color: #fff1aa; } | ||
| > 4 │ a { color: #123456aa; } | ||
| │ ^^^^^^^^^ | ||
| 5 │ a { color: #0000000000000000; } | ||
| 6 │ | ||
|
|
||
| i Hex colors are less readable and have limitations compared to other color models. | ||
|
|
||
| i Consider using a named color or a color function like rgb(), hsl() or oklch(). See MDN Web Docs on CSS color values for more information. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.css:5:12 lint/nursery/noHexColors ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Unexpected hex color. | ||
|
|
||
| 3 │ a { color: #fff1aa; } | ||
| 4 │ a { color: #123456aa; } | ||
| > 5 │ a { color: #0000000000000000; } | ||
| │ ^^^^^^^^^^^^^^^^^ | ||
| 6 │ | ||
|
|
||
| i Hex colors are less readable and have limitations compared to other color models. | ||
|
|
||
| i Consider using a named color or a color function like rgb(), hsl() or oklch(). See MDN Web Docs on CSS color values for more information. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` |
7 changes: 7 additions & 0 deletions
7
crates/biome_css_analyze/tests/specs/nursery/noHexColors/valid.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /* should not generate diagnostics */ | ||
| p { | ||
| color: red; | ||
| } | ||
| a { color: black; } | ||
| a { color: rgb(0, 0, 0); } | ||
| a { color: rgba(0, 0, 0, 1); } |
16 changes: 16 additions & 0 deletions
16
crates/biome_css_analyze/tests/specs/nursery/noHexColors/valid.css.snap
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,16 @@ | ||
| --- | ||
| source: crates/biome_css_analyze/tests/spec_tests.rs | ||
| assertion_line: 111 | ||
| expression: valid.css | ||
| --- | ||
| # Input | ||
| ```css | ||
| /* should not generate diagnostics */ | ||
| p { | ||
| color: red; | ||
| } | ||
| a { color: black; } | ||
| a { color: rgb(0, 0, 0); } | ||
| a { color: rgba(0, 0, 0, 1); } | ||
|
|
||
| ``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,6 @@ | ||
| use biome_deserialize_macros::{Deserializable, Merge}; | ||
| use serde::{Deserialize, Serialize}; | ||
| #[derive(Default, Clone, Debug, Deserialize, Deserializable, Merge, Eq, PartialEq, Serialize)] | ||
| #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] | ||
| #[serde(rename_all = "camelCase", deny_unknown_fields, default)] | ||
| pub struct NoHexColorsOptions {} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Please add an invalid example to the changeset.
A short inline snippet is required for new lint rules in changesets. Based on learnings, please include one.
✍️ Suggested tweak
📝 Committable suggestion
🤖 Prompt for AI Agents