UNPKG

@jsxtools/eslint-plugin-jsx-a11y

Version:

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

58 lines (55 loc) 1.91 kB
import { dom } from 'aria-query'; import { propName } from '../util/module/jsx-ast-utils.js'; import getElementType from '../util/getElementType.js'; import getExplicitRole from '../util/getExplicitRole.js'; import isNonInteractiveElement from '../util/isNonInteractiveElement.js'; import isInteractiveRole from '../util/isInteractiveRole.js'; const errorMessage = "Non-interactive elements should not be assigned interactive roles."; const ruleOfNoNoninteractiveElementToInteractiveRole = { meta: { docs: { url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-noninteractive-element-to-interactive-role.md", description: "Non-interactive elements should not be assigned interactive roles." }, schema: [{ type: "object", additionalProperties: { type: "array", items: { type: "string" }, uniqueItems: true } }] }, create: (context) => { const { options } = context; const elementType = getElementType(context); return { JSXAttribute: (attribute) => { const attributeName = propName(attribute); if (attributeName !== "role") { return; } const node = attribute.parent; const { attributes } = node; const type = elementType(node); const role = getExplicitRole(type, node.attributes); if (!dom.has(type)) { return; } const allowedRoles = options[0] || {}; if (Object.hasOwn(allowedRoles, type) && allowedRoles[type].includes(role)) { return; } if (isNonInteractiveElement(type, attributes) && isInteractiveRole(type, attributes)) { context.report({ node: attribute, message: errorMessage }); } } }; } }; export { ruleOfNoNoninteractiveElementToInteractiveRole as default };