feat(linter): Add fixer for jsx-a11y/no-access-key rule (#6781)

This commit is contained in:
Tapan Prakash 2024-10-25 12:18:12 +05:30 committed by GitHub
parent 442975408b
commit a73c5af0f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,7 +37,8 @@ declare_oxc_lint!(
/// <div />
/// ```
NoAccessKey,
correctness
correctness,
suggestion,
);
impl Rule for NoAccessKey {
@ -50,12 +51,17 @@ impl Rule for NoAccessKey {
{
match attr.value.as_ref() {
Some(JSXAttributeValue::StringLiteral(_)) => {
ctx.diagnostic(no_access_key_diagnostic(attr.span));
ctx.diagnostic_with_suggestion(no_access_key_diagnostic(attr.span), |fixer| {
fixer.delete(&attr.span)
});
}
Some(JSXAttributeValue::ExpressionContainer(container)) => {
if container.expression.is_expression() && !container.expression.is_undefined()
{
ctx.diagnostic(no_access_key_diagnostic(attr.span));
ctx.diagnostic_with_suggestion(
no_access_key_diagnostic(attr.span),
|fixer| fixer.delete(&attr.span),
);
}
}
_ => {}
@ -84,5 +90,19 @@ fn test() {
r"<div accessKey={`${undefined}${undefined}`} />",
];
Tester::new(NoAccessKey::NAME, pass, fail).test_and_snapshot();
let fix = vec![
(r#"<div accesskey="h" />"#, r"<div />"),
(r#"<div accessKey="h" />"#, r"<div />"),
(r#"<div accessKey="h" {...props} />"#, r"<div {...props} />"),
(r#"<div acCesSKeY="y" />"#, r"<div />"),
(r#"<div accessKey={"y"} />"#, r"<div />"),
(r"<div accessKey={`${y}`} />", r"<div />"),
(r"<div accessKey={`${undefined}y${undefined}`} />", r"<div />"),
(r"<div accessKey={`This is ${bad}`} />", r"<div />"),
(r"<div accessKey={accessKey} />", r"<div />"),
(r"<div accessKey={`${undefined}`} />", r"<div />"),
(r"<div accessKey={`${undefined}${undefined}`} />", r"<div />"),
];
Tester::new(NoAccessKey::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}