-
-
Notifications
You must be signed in to change notification settings - Fork 842
feat(analyze/html): add noDuplicateAttributes #8653
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 new nursery rule [`noDuplicateAttributes`](https://biomejs.dev/linter/rules/no-duplicate-attributes/) to forbid duplicate attributes in HTML elements. |
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
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.
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
173 changes: 173 additions & 0 deletions
173
crates/biome_html_analyze/src/lint/nursery/no_duplicate_attributes.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,173 @@ | ||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_html_syntax::{AnyHtmlAttribute, AnyVueDirective, HtmlAttributeList}; | ||
| use biome_rowan::{AstNode, AstNodeList, TextRange, TokenText}; | ||
| use biome_rule_options::no_duplicate_attributes::NoDuplicateAttributesOptions; | ||
| use std::collections::HashSet; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Disallow duplication of attributes. | ||
| /// | ||
| /// According to the HTML specification, each attribute name must be unique within a single element. | ||
| /// Duplicate attributes are invalid and can lead to unexpected behavior in browsers. | ||
| /// | ||
| /// ## Vue templates | ||
| /// | ||
| /// For Vue templates (`.vue` files), this rule also considers the following directives as | ||
| /// aliases of their arguments: | ||
| /// | ||
| /// - `v-bind:foo` and `:foo` are handled as the attribute `foo`. | ||
| /// | ||
| /// Vue class/style bindings are ignored. For example, `class` and `:class` may co-exist. | ||
| /// | ||
| /// Event handlers are ignored. For example, `@click` and `v-on:click` are not considered | ||
| /// attributes by this rule. | ||
| /// | ||
| /// Dynamic arguments such as `:[foo]` or `v-bind:[foo]` are ignored. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```html,expect_diagnostic | ||
| /// <div foo="a" foo="b"></div> | ||
| /// ``` | ||
| /// | ||
| /// ```vue,expect_diagnostic | ||
| /// <template> | ||
| /// <div foo :foo="bar" /> | ||
| /// </template> | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```html | ||
| /// <div foo="a" bar="b"></div> | ||
| /// ``` | ||
| /// | ||
| pub NoDuplicateAttributes { | ||
| version: "next", | ||
| name: "noDuplicateAttributes", | ||
| language: "html", | ||
| recommended: true, | ||
| sources: &[ | ||
| RuleSource::HtmlEslint("no-duplicate-attrs").same(), | ||
| RuleSource::EslintVueJs("no-duplicate-attributes").same() | ||
| ], | ||
| } | ||
| } | ||
|
|
||
| pub struct State { | ||
| range: TextRange, | ||
| name: TokenText, | ||
| /// Range of the first occurrence of the attribute. | ||
| original_range: TextRange, | ||
| } | ||
|
|
||
| impl Rule for NoDuplicateAttributes { | ||
| type Query = Ast<HtmlAttributeList>; | ||
| type State = State; | ||
| type Signals = Box<[Self::State]>; | ||
| type Options = NoDuplicateAttributesOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let node = ctx.query(); | ||
| let mut seen = HashSet::<(TokenText, TextRange)>::new(); | ||
| let mut violations = Vec::new(); | ||
|
|
||
| for attribute in node.iter() { | ||
| let Some(key) = attribute_key(&attribute) else { | ||
| continue; | ||
| }; | ||
|
|
||
| if let Some((_, original_range)) = seen.iter().find(|(tt, _)| tt == &key.0) { | ||
| violations.push(State { | ||
| range: attribute.range(), | ||
| name: key.0.clone(), | ||
| original_range: *original_range, | ||
| }); | ||
| } else { | ||
| seen.insert(key); | ||
| } | ||
| } | ||
|
|
||
| violations.into_boxed_slice() | ||
| } | ||
|
|
||
| fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
| let name = state.name.text(); | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| state.range, | ||
| markup! { | ||
| "Duplicate attribute '"<Emphasis>{name}</Emphasis>"'." | ||
| }, | ||
| ) | ||
| .detail(state.original_range, "This is the first occurrence of the attribute.") | ||
| .note("Each attribute name must be unique within a single element. Duplicate attributes are invalid and can lead to unexpected browser behavior.").note( | ||
| markup! { | ||
| "Consider removing or renaming the duplicate '"<Emphasis>{name}</Emphasis>"' attribute." | ||
| }, | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| fn attribute_key(attribute: &AnyHtmlAttribute) -> Option<(TokenText, TextRange)> { | ||
| // Plain HTML attribute (eg. `foo`) | ||
| if let Some(html_attr) = attribute.as_html_attribute() | ||
| && let Ok(name) = html_attr.name() | ||
| && let Ok(token) = name.value_token() | ||
| { | ||
| return Some((token.token_text_trimmed(), token.text_trimmed_range())); | ||
| } | ||
|
|
||
| // Vue directives (`.vue` files only) | ||
| let vue = attribute.as_any_vue_directive()?; | ||
|
|
||
| match vue { | ||
| // Longhand directive: v-bind:foo | ||
| AnyVueDirective::VueDirective(directive) => { | ||
| let name_token = directive.name_token().ok()?; | ||
| let name = name_token.text_trimmed(); | ||
| if name != "v-bind" { | ||
| return None; | ||
| } | ||
|
|
||
| let argument = directive.arg()?; | ||
| let argument = argument.arg().ok()?; | ||
| let static_argument = argument.as_vue_static_argument()?; | ||
| let name_token = static_argument.name_token().ok()?; | ||
|
|
||
| let key = name_token.token_text_trimmed(); | ||
| if key.text() == "class" || key.text() == "style" { | ||
| return None; | ||
| } | ||
|
|
||
| Some((key, name_token.text_trimmed_range())) | ||
| } | ||
|
|
||
| // Shorthand bind: :foo | ||
| AnyVueDirective::VueVBindShorthandDirective(directive) => { | ||
| let argument = directive.arg().ok()?; | ||
| let argument = argument.arg().ok()?; | ||
| let static_argument = argument.as_vue_static_argument()?; | ||
| let name_token = static_argument.name_token().ok()?; | ||
|
|
||
| let key = name_token.token_text_trimmed(); | ||
| if key.text() == "class" || key.text() == "style" { | ||
| return None; | ||
| } | ||
|
|
||
| Some((key, name_token.text_trimmed_range())) | ||
| } | ||
|
|
||
| // Ignore all v-on and shorthand @event handlers. | ||
| AnyVueDirective::VueVOnShorthandDirective(_) => None, | ||
|
|
||
| _ => None, | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
crates/biome_html_analyze/tests/specs/nursery/noDuplicateAttributes/invalid.html
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,12 @@ | ||
| <!-- should generate diagnostics --> | ||
|
|
||
| <div foo="a" foo="b"></div> | ||
|
|
||
| <div Foo Foo></div> | ||
|
|
||
| <!-- case-sensitive: these should NOT be considered duplicates --> | ||
| <div foo Foo></div> | ||
|
|
||
| <div class class></div> | ||
| <div style style></div> | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
EslintHtmlinstead perhaps? I think as all of them start with EslintThere 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.
That's how the project is named though. I think its because
eslint-plugin-htmlis a different thing that only allows linting js in html, and does not implement any html rules itself. It's a bit weird.