UNPKG

@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
const schemas = require('../util/schemas.cjs'); const getAccessibleChildText = require('../util/getAccessibleChildText.cjs'); const getElementType = require('../util/getElementType.cjs'); const DEFAULT_AMBIGUOUS_WORDS = [ "click here", "here", "link", "a link", "learn more" ]; const schema = schemas.generateObjSchema({ words: schemas.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('", "') } }); } }; } }; module.exports = ruleOfAnchorAmbiguousText;