UNPKG

@jsxtools/eslint-plugin-jsx-a11y

Version:

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

42 lines (39 loc) 1.25 kB
const ariaQuery = require('aria-query'); const jsxAstUtils = require('../util/module/jsx-ast-utils.cjs'); const schemas = require('../util/schemas.cjs'); const getSuggestion = require('../util/getSuggestion.cjs'); const ariaAttributes = [...ariaQuery.aria.keys()]; const errorMessage = (name) => { const suggestions = getSuggestion(name, ariaAttributes); const message = `${name}: This attribute is an invalid ARIA attribute.`; if (suggestions.length > 0) { return `${message} Did you mean to use ${suggestions}?`; } return message; }; const schema = schemas.generateObjSchema(); const ruleOfAriaProps = { meta: { docs: { url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/aria-props.md", description: "Enforce all `aria-*` props are valid." }, schema: [schema] }, create: (context) => ({ JSXAttribute: (attribute) => { const name = jsxAstUtils.propName(attribute); if (name.indexOf("aria-") !== 0) { return; } const isValid = ariaQuery.aria.has(name); if (isValid === false) { context.report({ node: attribute, message: errorMessage(name) }); } } }) }; module.exports = ruleOfAriaProps;