Skip to content
← Back to rules

jsx-a11y/no-redundant-roles Correctness

🛠️ An auto-fix is available for this rule.

它的作用

强制代码不要包含多余的 role 属性,前提是它与元素类型的隐式 role 属性相同。

为什么这不好?

多余的角色会导致代码库中的混淆和冗余。

示例

此规则适用于以下元素及其隐式角色:

  • <nav>: navigation
  • <button>: button
  • <main>: main

以下是此规则的错误代码示例:

jsx
<nav role="navigation"></nav>
<button role="button"></button>
<main role="main"></main>

以下是此规则的正确代码示例:

jsx
<nav></nav>
<button></button>
<main></main>

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["jsx-a11y"],
  "rules": {
    "jsx-a11y/no-redundant-roles": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["jsx-a11y"],
  rules: {
    "jsx-a11y/no-redundant-roles": "error",
  },
});
bash
oxlint --deny jsx-a11y/no-redundant-roles --jsx-a11y-plugin

版本

此规则已在 v0.2.1 中添加。

参考资料