@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
51 lines (48 loc) • 1.75 kB
JavaScript
import { dom } from 'aria-query';
import { getProp, hasAnyProp } from '../util/module/jsx-ast-utils.js';
import { generateObjSchema } from '../util/schemas.js';
import getElementType from '../util/getElementType.js';
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader.js';
import isInteractiveElement from '../util/isInteractiveElement.js';
import isPresentationRole from '../util/isPresentationRole.js';
const errorMessage = "Visible, non-interactive elements with click handlers must have at least one keyboard listener.";
const schema = generateObjSchema();
const ruleOfClickEventsHaveKeyEvents = {
meta: {
docs: {
url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/click-events-have-key-events.md",
description: "Enforce a clickable non-interactive element has at least one keyboard event listener."
},
schema: [schema]
},
create: (context) => {
const elementType = getElementType(context);
return {
JSXOpeningElement: (node) => {
const props = node.attributes;
if (getProp(props, "onclick") === void 0) {
return;
}
const type = elementType(node);
const requiredProps = ["onkeydown", "onkeyup", "onkeypress"];
if (!dom.has(type)) {
return;
}
if (isHiddenFromScreenReader(type, props) || isPresentationRole(type, props)) {
return;
}
if (isInteractiveElement(type, props)) {
return;
}
if (hasAnyProp(props, requiredProps)) {
return;
}
context.report({
node,
message: errorMessage
});
}
};
}
};
export { ruleOfClickEventsHaveKeyEvents as default };