UNPKG

@jsxtools/eslint-plugin-jsx-a11y

Version:

Static AST checker for accessibility rules on JSX elements for flat ESLint Config.

51 lines (48 loc) 1.55 kB
import { generateObjSchema, arraySchema } from '../util/schemas.js'; import getElementType from '../util/getElementType.js'; import hasAccessibleChild from '../util/hasAccessibleChild.js'; import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader.js'; const errorMessage = "Headings must have content and the content must be accessible by a screen reader."; const headings = [ "h1", "h2", "h3", "h4", "h5", "h6" ]; const schema = generateObjSchema({ components: arraySchema }); const ruleOfHeadingHasContent = { meta: { docs: { url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/heading-has-content.md", description: "Enforce heading (`h1`, `h2`, etc) elements contain accessible content." }, schema: [schema] }, create: (context) => { const elementType = getElementType(context); return { JSXOpeningElement: (node) => { const options = context.options[0] || {}; const componentOptions = options.components || []; const typeCheck = headings.concat(componentOptions); const nodeType = elementType(node); if (typeCheck.indexOf(nodeType) === -1) { return; } if (hasAccessibleChild(node.parent, elementType)) { return; } if (isHiddenFromScreenReader(nodeType, node.attributes)) { return; } context.report({ node, message: errorMessage }); } }; } }; export { ruleOfHeadingHasContent as default };