-
-
Notifications
You must be signed in to change notification settings - Fork 475
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(lint): useSemanticElements #2867
Merged
ematipico
merged 9 commits into
biomejs:main
from
fujiyamaorange:lint-prefer-tag-over-role-3
May 20, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0f26b11
feat: init
fujiyamaorange f6a3e04
chore: add test cases
fujiyamaorange a69471e
feat(lint): add temporary linter
fujiyamaorange 690102b
feat: add feature
fujiyamaorange df3a915
fix: fix rule
fujiyamaorange 1f09a57
fix: fix lint & test cases
fujiyamaorange 9e31e13
fix: fix lint
fujiyamaorange a3ab25b
fix: add test cases & refactor code
fujiyamaorange efa396b
fix: update footer_list
fujiyamaorange 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
10 changes: 10 additions & 0 deletions
10
crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs
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 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.
86 changes: 86 additions & 0 deletions
86
crates/biome_js_analyze/src/lint/nursery/use_semantic_elements.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,86 @@ | ||
use biome_analyze::{context::RuleContext, declare_rule, Rule, RuleDiagnostic, RuleSource}; | ||
use biome_aria::AriaRoles; | ||
use biome_console::markup; | ||
use biome_js_syntax::{AnyJsxAttribute, JsxOpeningElement}; | ||
use biome_rowan::AstNode; | ||
|
||
use crate::services::aria::Aria; | ||
|
||
declare_rule! { | ||
/// It detects the use of `role` attributes in JSX elements and suggests using semantic elements instead. | ||
/// | ||
/// The `role` attribute is used to define the purpose of an element, but it should be used as a last resort. Using semantic elements like `<button>`, `<input>`, `<textarea>`, `<a>`, `<img>`, `<table>`, `<article>`, `<section>`, `<nav>`, `<aside>`, `<header>`, `<footer>`, `<main>`, `<figure>`, `<figcaption>`, `<details>`, `<summary>`, `<dialog>`, `<menu>`, `<menuitem>`, `<fieldset>`, `<legend>`, `<caption>`, `<colgroup>`, `<col>`, `<optgroup>`, `<option>`, `<select>`, `<datalist>`, `<output>`, `<progress>`, `<meter>`, `<time>`, `<audio>`, `<video>`, `<track>`, `<source>`, `<embed>`, `<object>`, `<param>`, `<iframe>`, `<canvas>`, `<map>`, `<area>`, `<svg>`, `<math>` are more accessible and provide better semantics. | ||
/// | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <div role="checkbox"> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <div role="img"> | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```jsx | ||
/// <div>...</div> | ||
/// <header>...</header> | ||
/// <img alt="" src="image.jpg" /> | ||
/// ``` | ||
/// | ||
pub UseSemanticElements { | ||
version: "next", | ||
name: "useSemanticElements", | ||
language: "jsx", | ||
sources: &[RuleSource::EslintJsxA11y("prefer-tag-over-role")], | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for UseSemanticElements { | ||
type Query = Aria<JsxOpeningElement>; | ||
type State = AnyJsxAttribute; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let role_attributes = node.find_attribute_by_name("role").unwrap(); | ||
|
||
if let Some(attr) = role_attributes { | ||
let extract_attributes = ctx.extract_attributes(&node.attributes()); | ||
|
||
let element = node.name().ok()?.as_jsx_name()?.value_token().ok()?; | ||
let element_name = element.text_trimmed(); | ||
let is_not_interative = | ||
AriaRoles.is_not_interactive_element(element_name, extract_attributes); | ||
if is_not_interative { | ||
return Some(AnyJsxAttribute::from(attr)); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
state.range(), | ||
markup! { | ||
"The element with this role can be changed to a DOM element that already this role." | ||
}, | ||
) | ||
.footer_list( | ||
markup! { | ||
"For examples and more information, see" <Hyperlink href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles">"WAI-ARIA Roles"</Hyperlink> | ||
}, | ||
&["<button>", "<input>", "<textarea>", "<a>", "<img>", "<table>", "<article>", "<section>", "<nav>", "<aside>", "<header>", "<footer>", "<main>", "<figure>", "<figcaption>", "<details>", "<summary>", "<dialog>", "<menu>", "<menuitem>", "<fieldset>", "<legend>", "<caption>", "<colgroup>", "<col>", "<optgroup>", "<option>", "<select>", "<datalist>", "<output>", "<progress>", "<meter>", "<time>", "<audio>", "<video>", "<track>", "<source>", "<embed>", "<object>", "<param>", "<iframe>", "<canvas>", "<map>", "<area>", "<svg>", "<math>"] | ||
), | ||
) | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
crates/biome_js_analyze/tests/specs/nursery/useSemanticElements/invalid.jsx
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,48 @@ | ||
<> | ||
<div role="checkbox" ></div> | ||
<div role="radio" ></div> | ||
<div role="option" ></div> | ||
<div role="combobox" ></div> | ||
<div role="heading" ></div> | ||
<div role="separator" ></div> | ||
<div role="button" ></div> | ||
<div role="article" ></div> | ||
<div role="dialog" ></div> | ||
<div role="alert" ></div> | ||
<div role="alertdialog" ></div> | ||
<div role="cell" ></div> | ||
<div role="columnheader" ></div> | ||
<div role="definition" ></div> | ||
<div role="figure" ></div> | ||
<div role="form" ></div> | ||
<div role="graphics-document" ></div> | ||
<div role="graphics-object" ></div> | ||
<div role="grid" ></div> | ||
<div role="gridcell" ></div> | ||
<div role="group" ></div> | ||
<div role="img" ></div> | ||
<div role="link" ></div> | ||
<div role="list" ></div> | ||
<div role="listbox" ></div> | ||
<div role="listitem" ></div> | ||
<div role="navigation" ></div> | ||
<div role="row" ></div> | ||
<div role="rowgroup" ></div> | ||
<div role="rowheader" ></div> | ||
<div role="search" ></div> | ||
<div role="searchbox" ></div> | ||
<div role="table" ></div> | ||
<div role="term" ></div> | ||
<div role="textbox" ></div> | ||
<div role="generic" ></div> | ||
<div role="caption" ></div> | ||
<div role="main" ></div> | ||
<div role="time" ></div> | ||
<div role="p" ></div> | ||
<div role="aside" ></div> | ||
<div role="blockquote" ></div> | ||
<div role="associationlist" ></div> | ||
<div role="status" ></div> | ||
<div role="contentinfo" ></div> | ||
<div role="region" ></div> | ||
</> |
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.
@fujiyamaorange why do we need to check that the element/role is not interactive? I don't get this part.
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.
As far as I remember, there are some proper cases with the
role
attribute.Regarding,
role="checkbox"
, it's allowed to use this attribute when it also has anaria-checked
attribute in an element.https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/checkbox_role