Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
refactor: update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nissy-dev committed May 8, 2023
1 parent b205fad commit 33e8927
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions crates/rome_js_analyze/src/analyzers/a11y/use_alt_text.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_console::{fmt::Display, fmt::Formatter, markup};
use rome_js_syntax::{jsx_ext::AnyJsxElement, TextRange};
use rome_rowan::AstNode;

Expand Down Expand Up @@ -57,6 +57,15 @@ pub enum ValidatedElement {
Input,
}

impl Display for ValidatedElement {
fn fmt(&self, fmt: &mut Formatter) -> std::io::Result<()> {
match self {
ValidatedElement::Object => fmt.write_markup(markup!(<Emphasis>"title"</Emphasis>)),
_ => fmt.write_markup(markup!(<Emphasis>"alt"</Emphasis>)),
}
}
}

impl Rule for UseAltText {
type Query = Ast<AnyJsxElement>;
type State = (ValidatedElement, TextRange);
Expand Down Expand Up @@ -120,14 +129,9 @@ impl Rule for UseAltText {

fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> {
let (validate_element, range) = state;
let message = match validate_element {
ValidatedElement::Object => markup!(
"Provide a text alternative through the "<Emphasis>"title"</Emphasis>", "<Emphasis>"aria-label"</Emphasis>" or "<Emphasis>"aria-labelledby"</Emphasis>" attribute"
).to_owned(),
_ => markup!(
"Provide a text alternative through the "<Emphasis>"alt"</Emphasis>", "<Emphasis>"aria-label"</Emphasis>" or "<Emphasis>"aria-labelledby"</Emphasis>" attribute"
).to_owned(),
};
let message = markup!(
"Provide a text alternative through the "{{validate_element}}", "<Emphasis>"aria-label"</Emphasis>" or "<Emphasis>"aria-labelledby"</Emphasis>" attribute"
).to_owned();
Some(
RuleDiagnostic::new(rule_category!(), range, message).note(markup! {
"Meaningful alternative text on elements helps users relying on screen readers to understand content's purpose within a page."
Expand Down

0 comments on commit 33e8927

Please sign in to comment.