UNPKG

@jsxtools/eslint-plugin-jsx-a11y

Version:

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

47 lines (44 loc) 1.63 kB
import { dom, aria } from 'aria-query'; import { propName } from '../util/module/jsx-ast-utils.js'; import { generateObjSchema } from '../util/schemas.js'; import getElementType from '../util/getElementType.js'; const errorMessage = (invalidProp) => `This element does not support ARIA roles, states and properties. Try removing the prop '${invalidProp}'.`; const schema = generateObjSchema(); const ruleOfAriaUnsupportedElements = { meta: { docs: { url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/aria-unsupported-elements.md", description: "Enforce that elements that do not support ARIA roles, states, and properties do not have those attributes." }, schema: [schema] }, create: (context) => { const elementType = getElementType(context); return { JSXOpeningElement: (node) => { const nodeType = elementType(node); const nodeAttrs = dom.get(nodeType) || {}; const { reserved: isReservedNodeType = false } = nodeAttrs; if (isReservedNodeType === false) { return; } const invalidAttributes = /* @__PURE__ */ new Set([...aria.keys(), "role"]); node.attributes.forEach((prop) => { if (prop.type === "JSXSpreadAttribute") { return; } const name = propName(prop).toLowerCase(); if (invalidAttributes.has(name)) { context.report({ node, message: errorMessage(name) }); } }); } }; } }; export { ruleOfAriaUnsupportedElements as default };