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.96 kB
const ariaQuery = require('aria-query'); const jsxAstUtils = require('../util/module/jsx-ast-utils.cjs'); const getElementType = require('../util/getElementType.cjs'); const getExplicitRole = require('../util/getExplicitRole.cjs'); const isNonInteractiveElement = require('../util/isNonInteractiveElement.cjs'); const isInteractiveRole = require('../util/isInteractiveRole.cjs'); 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 = jsxAstUtils.propName(attribute); if (attributeName !== "role") { return; } const node = attribute.parent; const { attributes } = node; const type = elementType(node); const role = getExplicitRole(type, node.attributes); if (!ariaQuery.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 }); } } }; } }; module.exports = ruleOfNoNoninteractiveElementToInteractiveRole;