UNPKG

@jsxtools/eslint-plugin-jsx-a11y

Version:

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

57 lines (54 loc) 1.97 kB
import getElementType from '../util/getElementType.js'; import getExplicitRole from '../util/getExplicitRole.js'; import getImplicitRole from '../util/getImplicitRole.js'; const errorMessage = (element, implicitRole) => `The element ${element} has an implicit role of ${implicitRole}. Defining this explicitly is redundant and should be avoided.`; const DEFAULT_ROLE_EXCEPTIONS = { nav: ["navigation"] }; const ruleOfNoRedundantRoles = { meta: { docs: { url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-redundant-roles.md", description: "Enforce explicit role property is not the same as implicit/default role property on element." }, schema: [{ type: "object", additionalProperties: { type: "array", items: { type: "string" }, uniqueItems: true } }] }, create: (context) => { const { options } = context; const elementType = getElementType(context); return { JSXOpeningElement: (node) => { const type = elementType(node); const implicitRole = getImplicitRole(type, node.attributes); const explicitRole = getExplicitRole(type, node.attributes); if (!implicitRole || !explicitRole) { return; } if (implicitRole === explicitRole) { const allowedRedundantRoles = options[0] || {}; let redundantRolesForElement; if (Object.hasOwn(allowedRedundantRoles, type)) { redundantRolesForElement = allowedRedundantRoles[type]; } else { redundantRolesForElement = DEFAULT_ROLE_EXCEPTIONS[type] || []; } if (redundantRolesForElement.includes(implicitRole)) { return; } context.report({ node, message: errorMessage(type, implicitRole.toLowerCase()) }); } } }; } }; export { ruleOfNoRedundantRoles as default };