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.8 kB
const ariaQuery = require('aria-query'); const jsxAstUtils = require('../util/module/jsx-ast-utils.cjs'); const schemas = require('../util/schemas.cjs'); const getElementType = require('../util/getElementType.cjs'); const isHiddenFromScreenReader = require('../util/isHiddenFromScreenReader.cjs'); const isInteractiveElement = require('../util/isInteractiveElement.cjs'); const isPresentationRole = require('../util/isPresentationRole.cjs'); const errorMessage = "Visible, non-interactive elements with click handlers must have at least one keyboard listener."; const schema = schemas.generateObjSchema(); const ruleOfClickEventsHaveKeyEvents = { meta: { docs: { url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/click-events-have-key-events.md", description: "Enforce a clickable non-interactive element has at least one keyboard event listener." }, schema: [schema] }, create: (context) => { const elementType = getElementType(context); return { JSXOpeningElement: (node) => { const props = node.attributes; if (jsxAstUtils.getProp(props, "onclick") === void 0) { return; } const type = elementType(node); const requiredProps = ["onkeydown", "onkeyup", "onkeypress"]; if (!ariaQuery.dom.has(type)) { return; } if (isHiddenFromScreenReader(type, props) || isPresentationRole(type, props)) { return; } if (isInteractiveElement(type, props)) { return; } if (jsxAstUtils.hasAnyProp(props, requiredProps)) { return; } context.report({ node, message: errorMessage }); } }; } }; module.exports = ruleOfClickEventsHaveKeyEvents;