Skip to content

Commit

Permalink
refactor(linter): improve jsx-a11y/heading-has-content (#9089)
Browse files Browse the repository at this point in the history
- Enable a repaired test case
- Simplify logical code
  • Loading branch information
shulaoda authored Feb 15, 2025
1 parent 47c1649 commit 9ca22f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
15 changes: 5 additions & 10 deletions crates/oxc_linter/src/rules/jsx_a11y/heading_has_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ declare_oxc_lint!(
/// this could either confuse users or even prevent them
/// from accessing information on the page's structure.
///
/// ### Example
/// ### Examples
///
/// Examples of **incorrect** code for this rule:
/// ```jsx
Expand Down Expand Up @@ -85,14 +85,10 @@ impl Rule for HeadingHasContent {
return;
};

// let JSXElementName::Identifier(iden) = &jsx_el.name else {
// return;
// };
let name = get_element_type(ctx, jsx_el);
let name = name.as_ref();

// let name = iden.name.as_str();
let name = &get_element_type(ctx, jsx_el);

if !DEFAULT_COMPONENTS.iter().any(|&comp| comp == name)
if DEFAULT_COMPONENTS.binary_search(&name).is_err()
&& !self
.components
.as_ref()
Expand Down Expand Up @@ -178,8 +174,7 @@ fn test() {
(r"<Heading>{undefined}</Heading>", Some(components()), None),
// CUSTOM ELEMENT TESTS FOR COMPONENTS SETTINGS
(r"<Heading />", None, Some(settings())),
// TODO: This should be failed but pass for now
// (r#"<h1><CustomInput type="hidden" /></h1>"#, None, Some(settings())),
(r#"<h1><CustomInput type="hidden" /></h1>"#, None, Some(settings())),
];

Tester::new(HeadingHasContent::NAME, HeadingHasContent::PLUGIN, pass, fail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ source: crates/oxc_linter/src/tester.rs
· ───────────
╰────
help: Provide screen reader accessible content when using heading elements.

eslint-plugin-jsx-a11y(heading-has-content): Headings must have content and the content must be accessible by a screen reader.
╭─[heading_has_content.tsx:1:1]
1 │ <h1><CustomInput type="hidden" /></h1>
· ────
╰────
help: Provide screen reader accessible content when using heading elements.

0 comments on commit 9ca22f4

Please sign in to comment.