Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions crates/oxc_linter/src/rules/jsx_a11y/no_redundant_roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,34 @@ pub struct NoRedundantRoles;
declare_oxc_lint!(
/// ### What it does
///
/// Enforces that the explicit `role` property is not the same as
/// implicit/default role property on element.
/// Enforces that code does not include a redundant `role` property, in the
/// case that it's identical to the implicit `role` property of the
/// element type.
///
/// ### Why is this bad?
///
/// Redundant roles can lead to confusion and verbosity in the codebase.
///
/// ### Examples
///
/// This rule applies for the following elements and their implicit roles:
///
/// - `<nav>`: `navigation`
/// - `<button>`: `button`
/// - `<body>`: `document`
///
/// Examples of **incorrect** code for this rule:
/// ```jsx
/// <nav role="navigation" />
/// <nav role="navigation"></nav>
/// <button role="button"></button>
/// <body role="document"></body>
/// ```
///
/// Examples of **correct** code for this rule:
/// ```jsx
/// <nav />
/// <nav></nav>
/// <button></button>
/// <body></body>
/// ```
NoRedundantRoles,
jsx_a11y,
Expand Down Expand Up @@ -101,6 +112,12 @@ fn test() {

let pass = vec![
("<div />", None, None),
("<button />", None, None),
("<button></button>", None, None),
("<button>Foo</button>", None, None),
("<button>role</button>", None, None),
("<nav />", None, None),
("<body />", None, None),
("<button role='main' />", None, None),
("<MyComponent role='button' />", None, None),
("<button role={`${foo}button`} />", None, None),
Expand All @@ -109,13 +126,37 @@ fn test() {

let fail = vec![
("<button role='button' />", None, None),
("<button role='button' data-foo='bar' />", None, None),
("<button role='button' data-role='bar' />", None, None),
("<button data-role='bar' role='button' />", None, None),
("<button role='button'></button>", None, None),
("<button role='button'>Foo</button>", None, None),
("<button role='button'><p>Test</p></button>", None, None),
("<button role='button' title='button'></button>", None, None),
("<body role='document' />", None, None),
("<nav role='navigation' />", None, None),
("<Button role='button' />", None, Some(settings())),
];

let fix = vec![
("<button role='button' />", "<button />"),
("<button role='button'>Foo</button>", "<button >Foo</button>"),
("<button role='button' data-role='bar' />", "<button data-role='bar' />"),
("<button data-role='bar' role='button' />", "<button data-role='bar' />"),
(
"<button role='button'>
Foo
</button>",
"<button >
Foo
</button>",
),
("<nav role='navigation' />", "<nav />"),
("<body role='document' />", "<body />"),
(
"<body role='document'><p>Foobarbaz!! document body role</p></body>",
"<body ><p>Foobarbaz!! document body role</p></body>",
),
];

Tester::new(NoRedundantRoles::NAME, NoRedundantRoles::PLUGIN, pass, fail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,69 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: Remove the redundant role `button` from the element `button`.

⚠ eslint-plugin-jsx-a11y(no-redundant-roles): The `button` element has an implicit role of `button`. Defining this explicitly is redundant and should be avoided.
╭─[no_redundant_roles.tsx:1:9]
1 │ <button role='button' data-foo='bar' />
· ─────────────
╰────
help: Remove the redundant role `button` from the element `button`.

⚠ eslint-plugin-jsx-a11y(no-redundant-roles): The `button` element has an implicit role of `button`. Defining this explicitly is redundant and should be avoided.
╭─[no_redundant_roles.tsx:1:9]
1 │ <button role='button' data-role='bar' />
· ─────────────
╰────
help: Remove the redundant role `button` from the element `button`.

⚠ eslint-plugin-jsx-a11y(no-redundant-roles): The `button` element has an implicit role of `button`. Defining this explicitly is redundant and should be avoided.
╭─[no_redundant_roles.tsx:1:25]
1 │ <button data-role='bar' role='button' />
· ─────────────
╰────
help: Remove the redundant role `button` from the element `button`.

⚠ eslint-plugin-jsx-a11y(no-redundant-roles): The `button` element has an implicit role of `button`. Defining this explicitly is redundant and should be avoided.
╭─[no_redundant_roles.tsx:1:9]
1 │ <button role='button'></button>
· ─────────────
╰────
help: Remove the redundant role `button` from the element `button`.

⚠ eslint-plugin-jsx-a11y(no-redundant-roles): The `button` element has an implicit role of `button`. Defining this explicitly is redundant and should be avoided.
╭─[no_redundant_roles.tsx:1:9]
1 │ <button role='button'>Foo</button>
· ─────────────
╰────
help: Remove the redundant role `button` from the element `button`.

⚠ eslint-plugin-jsx-a11y(no-redundant-roles): The `button` element has an implicit role of `button`. Defining this explicitly is redundant and should be avoided.
╭─[no_redundant_roles.tsx:1:9]
1 │ <button role='button'><p>Test</p></button>
· ─────────────
╰────
help: Remove the redundant role `button` from the element `button`.

⚠ eslint-plugin-jsx-a11y(no-redundant-roles): The `button` element has an implicit role of `button`. Defining this explicitly is redundant and should be avoided.
╭─[no_redundant_roles.tsx:1:9]
1 │ <button role='button' title='button'></button>
· ─────────────
╰────
help: Remove the redundant role `button` from the element `button`.

⚠ eslint-plugin-jsx-a11y(no-redundant-roles): The `body` element has an implicit role of `document`. Defining this explicitly is redundant and should be avoided.
╭─[no_redundant_roles.tsx:1:7]
1 │ <body role='document' />
· ───────────────
╰────
help: Remove the redundant role `document` from the element `body`.

⚠ eslint-plugin-jsx-a11y(no-redundant-roles): The `nav` element has an implicit role of `navigation`. Defining this explicitly is redundant and should be avoided.
╭─[no_redundant_roles.tsx:1:6]
1 │ <nav role='navigation' />
· ─────────────────
╰────
help: Remove the redundant role `navigation` from the element `nav`.

⚠ eslint-plugin-jsx-a11y(no-redundant-roles): The `button` element has an implicit role of `button`. Defining this explicitly is redundant and should be avoided.
╭─[no_redundant_roles.tsx:1:9]
1 │ <Button role='button' />
Expand Down
Loading