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.56 kB
const schemas = require('../util/schemas.cjs'); const getElementType = require('../util/getElementType.cjs'); const hasAccessibleChild = require('../util/hasAccessibleChild.cjs'); const isHiddenFromScreenReader = require('../util/isHiddenFromScreenReader.cjs'); 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 = schemas.generateObjSchema({ components: schemas.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 }); } }; } }; module.exports = ruleOfHeadingHasContent;