Skip to content

Commit

Permalink
feat(linter): add fixer for jsx-a11y/aria-props
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Jul 10, 2024
1 parent ca0b4fa commit ade9630
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 56 deletions.
86 changes: 43 additions & 43 deletions crates/oxc_linter/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,54 @@ pub const VALID_ARIA_ROLES: phf::Set<&'static str> = phf_set! {
"deletion",
"dialog",
"directory",
"doc-abstract",
"doc-acknowledgments",
"doc-afterword",
"doc-appendix",
"doc-backlink",
"doc-biblioentry",
"doc-bibliography",
"doc-biblioref",
"doc-chapter",
"doc-colophon",
"doc-conclusion",
"doc-cover",
"doc-credit",
"doc-credits",
"doc-dedication",
"doc-endnote",
"doc-endnotes",
"doc-epigraph",
"doc-epilogue",
"doc-errata",
"doc-example",
"doc-footnote",
"doc-foreword",
"doc-glossary",
"doc-glossref",
"doc-index",
"doc-introduction",
"doc-noteref",
"doc-notice",
"doc-pagebreak",
"doc-pagelist",
"doc-part",
"doc-preface",
"doc-prologue",
"doc-pullquote",
"doc-qna",
"doc-subtitle",
"doc-tip",
"doc-toc",
"document",
"emphasis",
"feed",
"figure",
"form",
"generic",
"graphics-document",
"graphics-object",
"graphics-symbol",
"grid",
"gridcell",
"group",
Expand Down Expand Up @@ -154,49 +196,7 @@ pub const VALID_ARIA_ROLES: phf::Set<&'static str> = phf_set! {
"tooltip",
"tree",
"treegrid",
"treeitem",
"doc-abstract",
"doc-acknowledgments",
"doc-afterword",
"doc-appendix",
"doc-backlink",
"doc-biblioentry",
"doc-bibliography",
"doc-biblioref",
"doc-chapter",
"doc-colophon",
"doc-conclusion",
"doc-cover",
"doc-credit",
"doc-credits",
"doc-dedication",
"doc-endnote",
"doc-endnotes",
"doc-epigraph",
"doc-epilogue",
"doc-errata",
"doc-example",
"doc-footnote",
"doc-foreword",
"doc-glossary",
"doc-glossref",
"doc-index",
"doc-introduction",
"doc-noteref",
"doc-notice",
"doc-pagebreak",
"doc-pagelist",
"doc-part",
"doc-preface",
"doc-prologue",
"doc-pullquote",
"doc-qna",
"doc-subtitle",
"doc-tip",
"doc-toc",
"graphics-document",
"graphics-object",
"graphics-symbol"
"treeitem"
};

pub const HTML_TAG: phf::Set<&'static str> = phf_set! {
Expand Down
42 changes: 35 additions & 7 deletions crates/oxc_linter/src/rules/jsx_a11y/aria_props.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use oxc_ast::{ast::JSXAttributeItem, AstKind};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_span::{GetSpan, Span};

use crate::{
context::LintContext, globals::VALID_ARIA_PROPS, rule::Rule, utils::get_jsx_attribute_name,
AstNode,
};

fn aria_props_diagnostic(span0: Span, x1: &str) -> OxcDiagnostic {
OxcDiagnostic::warn("eslint-plugin-jsx-a11y(aria-props): Invalid ARIA prop.")
.with_help(format!("`{x1}` is an invalid ARIA attribute."))
.with_label(span0)
fn aria_props_diagnostic(span: Span, prop_name: &str, suggestion: Option<&str>) -> OxcDiagnostic {
let mut err = OxcDiagnostic::warn(format!(
"eslint-plugin-jsx-a11y(aria-props): '{prop_name}' is not a valid ARIA attribute."
));

if let Some(suggestion) = suggestion {
err = err.with_help(format!("Did you mean '{suggestion}'?"));
}

err.with_label(span)
}

#[derive(Debug, Default, Clone)]
Expand All @@ -25,6 +31,8 @@ declare_oxc_lint!(
/// Using invalid ARIA attributes can mislead screen readers and other assistive technologies.
/// It may cause the accessibility features of the website to fail, making it difficult
/// for users with disabilities to use the site effectively.
///
/// This rule includes fixes for some common typos.
///
/// ### Example
/// ```javascript
Expand All @@ -37,17 +45,35 @@ declare_oxc_lint!(
AriaProps,
correctness
);

impl Rule for AriaProps {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::JSXAttributeItem(JSXAttributeItem::Attribute(attr)) = node.kind() {
let name = get_jsx_attribute_name(&attr.name).to_lowercase();
if name.starts_with("aria-") && !VALID_ARIA_PROPS.contains(&name) {
ctx.diagnostic(aria_props_diagnostic(attr.span, &name));
let suggestion = COMMON_TYPOS.get(&name).copied();
let diagnostic = aria_props_diagnostic(attr.span, &name, suggestion);

if let Some(suggestion) = suggestion {
ctx.diagnostic_with_fix(diagnostic, |fixer| {
fixer.replace(attr.name.span(), suggestion)
})
} else {
ctx.diagnostic(diagnostic);
}
}
}
}
}

const COMMON_TYPOS: phf::Map<&'static str, &'static str> = phf::phf_map! {
"aria-labeledby" => "aria-labelledby",
"aria-role" => "role",
"aria-sorted" => "aria-sort",
"aria-lable" => "aria-label",

Check warning on line 73 in crates/oxc_linter/src/rules/jsx_a11y/aria_props.rs

View workflow job for this annotation

GitHub Actions / Spell Check

"lable" should be "label".
"aria-value" => "aria-valuenow",
};

#[test]
fn test() {
use crate::tester::Tester;
Expand All @@ -68,6 +94,8 @@ fn test() {
r#"<div aria-labeledby="foobar" />"#,
r#"<div aria-skldjfaria-klajsd="foobar" />"#,
];
let fix =
vec![(r#"<div aria-labeledby="foobar" />"#, r#"<div aria-labelledby="foobar" />"#, None)];

Tester::new(AriaProps::NAME, pass, fail).test_and_snapshot();
Tester::new(AriaProps::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}
10 changes: 4 additions & 6 deletions crates/oxc_linter/src/snapshots/aria_props.snap
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
---
source: crates/oxc_linter/src/tester.rs
---
eslint-plugin-jsx-a11y(aria-props): Invalid ARIA prop.
eslint-plugin-jsx-a11y(aria-props): 'aria-' is not a valid ARIA attribute.
╭─[aria_props.tsx:1:6]
1<div aria-="foobar" />
· ──────────────
╰────
help: `aria-` is an invalid ARIA attribute.

eslint-plugin-jsx-a11y(aria-props): Invalid ARIA prop.
eslint-plugin-jsx-a11y(aria-props): 'aria-labeledby' is not a valid ARIA attribute.
╭─[aria_props.tsx:1:6]
1<div aria-labeledby="foobar" />
· ───────────────────────
╰────
help: `aria-labeledby` is an invalid ARIA attribute.
help: Did you mean 'aria-labelledby'?

eslint-plugin-jsx-a11y(aria-props): Invalid ARIA prop.
eslint-plugin-jsx-a11y(aria-props): 'aria-skldjfaria-klajsd' is not a valid ARIA attribute.
╭─[aria_props.tsx:1:6]
1<div aria-skldjfaria-klajsd="foobar" />
· ───────────────────────────────
╰────
help: `aria-skldjfaria-klajsd` is an invalid ARIA attribute.

0 comments on commit ade9630

Please sign in to comment.