feat(jsx-a11y/no-autofocus): implement fixer support (#4171)

Let oxlint automatically remove the autoFocus attribute when `--fix` is
passed.
This commit is contained in:
Jelle van der Waa 2024-07-17 22:31:19 +02:00 committed by GitHub
parent 2213f9393b
commit 9df7b5675f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -96,14 +96,18 @@ impl Rule for NoAutofocus {
if self.ignore_non_dom {
if HTML_TAG.contains(&element_type) {
if let oxc_ast::ast::JSXAttributeItem::Attribute(attr) = autofocus {
ctx.diagnostic(no_autofocus_diagnostic(attr.span));
ctx.diagnostic_with_fix(no_autofocus_diagnostic(attr.span), |fixer| {
fixer.delete(&attr.span)
});
}
}
return;
}
if let oxc_ast::ast::JSXAttributeItem::Attribute(attr) = autofocus {
ctx.diagnostic(no_autofocus_diagnostic(attr.span));
ctx.diagnostic_with_fix(no_autofocus_diagnostic(attr.span), |fixer| {
fixer.delete(&attr.span)
});
}
}
}
@ -154,5 +158,15 @@ fn test() {
("<Button autoFocus />", Some(config()), Some(settings()), None),
];
Tester::new(NoAutofocus::NAME, pass, fail).test_and_snapshot();
let fix = vec![
("<div autoFocus />", "<div />", None),
("<div autoFocus={true} />", "<div />", None),
("<div autoFocus='true' />", "<div />", None),
("<Button autoFocus='true' />", "<Button />", None),
("<input autoFocus />", "<input />", None),
("<div autoFocus>foo</div>", "<div >foo</div>", None),
("<div autoFocus id='lol'>foo</div>", "<div id='lol'>foo</div>", None),
];
Tester::new(NoAutofocus::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}