Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 11 commits into from
Oct 25, 2024
27 changes: 23 additions & 4 deletions crates/oxc_linter/src/rules/jsx_a11y/no_access_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ declare_oxc_lint!(
/// <div />
/// ```
NoAccessKey,
correctness
correctness,
suggestion,
);

impl Rule for NoAccessKey {
Expand All @@ -50,12 +51,16 @@ 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)
});
}
}
_ => {}
Expand Down Expand Up @@ -84,5 +89,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();
}
Loading