Skip to content

Commit

Permalink
feat(linter): Added fixer for jsx_a11y/no_access_key rule
Browse files Browse the repository at this point in the history
  • Loading branch information
tapanprakasht committed Oct 22, 2024
1 parent b711ee1 commit 477b0fc
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 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,
fix
);

impl Rule for NoAccessKey {
Expand All @@ -50,12 +51,12 @@ impl Rule for NoAccessKey {
{
match attr.value.as_ref() {
Some(JSXAttributeValue::StringLiteral(_)) => {
ctx.diagnostic(no_access_key_diagnostic(attr.span));
ctx.diagnostic_with_fix(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_fix(no_access_key_diagnostic(attr.span), |fixer| fixer.delete(&attr.span));
}
}
_ => {}
Expand Down Expand Up @@ -84,5 +85,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 477b0fc

Please sign in to comment.