Skip to content
← Back to rules

jsx-a11y/anchor-ambiguous-text Restriction

它的作用

检查锚点链接文本中是否使用了含糊不清的词语。

此规则会检查锚点元素的 aria-label 文本(如果可用)。 如果锚点没有 aria-label,则会组合其子元素中的以下文本:

  • aria-label(如果可用)
  • 如果子元素是图片,则使用其 alt 文本
  • HTML 元素的文本内容

这为什么不好?

屏幕阅读器用户依赖链接文本来获取上下文,而像“click here”这样的含糊词语并不能提供足够的上下文。

示例

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

jsx
<a>link</a>
<a>click here</a>

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

jsx
<a>read this tutorial</a>
<a aria-label="oxc linter documentation">click here</a>

配置

此规则接受一个包含以下属性的配置对象:

words

type: string[]

default: ["click here", "here", "link", "a link", "learn more"]

需要在锚点文本中标记出来的含糊词语或短语列表。

如何使用

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

json
{
  "plugins": ["jsx-a11y"],
  "rules": {
    "jsx-a11y/anchor-ambiguous-text": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则于 v0.13.2 中添加。

参考资料