@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
52 lines (49 loc) • 1.62 kB
JavaScript
import { generateObjSchema, arraySchema } from '../util/schemas.js';
import getAccessibleChildText from '../util/getAccessibleChildText.js';
import getElementType from '../util/getElementType.js';
const DEFAULT_AMBIGUOUS_WORDS = [
"click here",
"here",
"link",
"a link",
"learn more"
];
const schema = generateObjSchema({
words: arraySchema
});
const ruleOfAnchorAmbiguousText = {
meta: {
docs: {
url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/anchor-ambiguous-text.md",
description: 'Enforce `<a>` text to not exactly match "click here", "here", "link", or "a link".'
},
schema: [schema]
},
create: (context) => {
const elementType = getElementType(context);
const typesToValidate = ["a"];
const options = context.options[0] || {};
const { words = DEFAULT_AMBIGUOUS_WORDS } = options;
const ambiguousWords = new Set(words);
return {
JSXOpeningElement: (node) => {
const nodeType = elementType(node);
if (typesToValidate.indexOf(nodeType) === -1) {
return;
}
const nodeText = getAccessibleChildText(node.parent, elementType);
if (!ambiguousWords.has(nodeText)) {
return;
}
context.report({
node,
message: 'Ambiguous text within anchor. Screenreader users rely on link text for context; the words "{{wordsList}}" are ambiguous and do not provide enough context.',
data: {
wordsList: words.join('", "')
}
});
}
};
}
};
export { ruleOfAnchorAmbiguousText as default };