Skip to content

Commit

Permalink
feat(linter): Add fixer for jsx-a11y/no-access-key rule (#6781)
Browse files Browse the repository at this point in the history
  • Loading branch information
tapanprakasht authored Oct 25, 2024
1 parent 4429754 commit a73c5af
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 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,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),
);
}
}
_ => {}
Expand Down Expand Up @@ -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();
}

0 comments on commit a73c5af

Please sign in to comment.