@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
65 lines (62 loc) • 2.95 kB
JavaScript
import { roles, dom } from 'aria-query';
import { eventHandlersByType, hasAnyProp, getProp, getLiteralPropValue } from '../util/module/jsx-ast-utils.js';
import { generateObjSchema, enumArraySchema } from '../util/schemas.js';
import getElementType from '../util/getElementType.js';
import isDisabledElement from '../util/isDisabledElement.js';
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader.js';
import isInteractiveElement from '../util/isInteractiveElement.js';
import isInteractiveRole from '../util/isInteractiveRole.js';
import isNonInteractiveElement from '../util/isNonInteractiveElement.js';
import isNonInteractiveRole from '../util/isNonInteractiveRole.js';
import isPresentationRole from '../util/isPresentationRole.js';
import getTabIndex from '../util/getTabIndex.js';
const schema = generateObjSchema({
// TODO: convert to use iterFilter and iterFrom
tabbable: enumArraySchema([...roles.keys()].filter((name) => !roles.get(name).abstract && roles.get(name).superClass.some((klasses) => klasses.includes("widget"))))
});
const interactiveProps = [].concat(
eventHandlersByType.mouse,
eventHandlersByType.keyboard
);
const ruleOfInteractiveSupportsFocus = {
meta: {
docs: {
url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/interactive-supports-focus.md",
description: "Enforce that elements with interactive handlers like `onClick` must be focusable."
},
schema: [schema]
},
create: (context) => {
const elementType = getElementType(context);
return {
JSXOpeningElement: (node) => {
const tabbable = context.options && context.options[0] && context.options[0].tabbable || [];
const { attributes } = node;
const type = elementType(node);
const hasInteractiveProps = hasAnyProp(attributes, interactiveProps);
const hasTabindex = getTabIndex(getProp(attributes, "tabIndex")) !== void 0;
if (!dom.has(type)) {
return;
}
if (!hasInteractiveProps || isDisabledElement(attributes) || isHiddenFromScreenReader(type, attributes) || isPresentationRole(type, attributes)) {
return;
}
if (hasInteractiveProps && isInteractiveRole(type, attributes) && !isInteractiveElement(type, attributes) && !isNonInteractiveElement(type, attributes) && !isNonInteractiveRole(type, attributes) && !hasTabindex) {
const role = getLiteralPropValue(getProp(attributes, "role"));
if (tabbable.includes(role)) {
context.report({
node,
message: `Elements with the '${role}' interactive role must be tabbable.`
});
} else {
context.report({
node,
message: `Elements with the '${role}' interactive role must be focusable.`
});
}
}
}
};
}
};
export { ruleOfInteractiveSupportsFocus as default };