feat(linter): add fixer for jsx_ally/no_aria_hidden_on_focusable (#4772)

part of #4179
This commit is contained in:
heygsc 2024-08-09 13:28:28 +08:00 committed by GitHub
parent c509a21a1f
commit abd83fa998
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -38,7 +38,8 @@ declare_oxc_lint!(
/// <div aria-hidden="true" />
/// ```
NoAriaHiddenOnFocusable,
correctness
correctness,
fix
);
impl Rule for NoAriaHiddenOnFocusable {
@ -49,7 +50,10 @@ impl Rule for NoAriaHiddenOnFocusable {
if let Some(aria_hidden_prop) = has_jsx_prop_ignore_case(jsx_el, "aria-hidden") {
if is_aria_hidden_true(aria_hidden_prop) && is_focusable(ctx, jsx_el) {
if let JSXAttributeItem::Attribute(boxed_attr) = aria_hidden_prop {
ctx.diagnostic(no_aria_hidden_on_focusable_diagnostic(boxed_attr.span));
ctx.diagnostic_with_fix(
no_aria_hidden_on_focusable_diagnostic(boxed_attr.span),
|fixer| fixer.delete(&boxed_attr.span),
);
}
}
}
@ -127,5 +131,14 @@ fn test() {
r#"<p tabIndex="0" aria-hidden="true">text</p>;"#,
];
Tester::new(NoAriaHiddenOnFocusable::NAME, pass, fail).test_and_snapshot();
let fix = vec![
(r#"<div aria-hidden="true" tabIndex="0" />;"#, r#"<div tabIndex="0" />;"#),
(r#"<input aria-hidden="true" />;"#, "<input />;"),
(r#"<a href="/" aria-hidden="true" />"#, r#"<a href="/" />"#),
(r#"<button aria-hidden="true" />"#, "<button />"),
(r#"<textarea aria-hidden="true" />"#, "<textarea />"),
(r#"<p tabIndex="0" aria-hidden="true">text</p>;"#, r#"<p tabIndex="0" >text</p>;"#),
];
Tester::new(NoAriaHiddenOnFocusable::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}