@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
65 lines (62 loc) • 3.05 kB
JavaScript
const ariaQuery = require('aria-query');
const jsxAstUtils = require('../util/module/jsx-ast-utils.cjs');
const schemas = require('../util/schemas.cjs');
const getElementType = require('../util/getElementType.cjs');
const isDisabledElement = require('../util/isDisabledElement.cjs');
const isHiddenFromScreenReader = require('../util/isHiddenFromScreenReader.cjs');
const isInteractiveElement = require('../util/isInteractiveElement.cjs');
const isInteractiveRole = require('../util/isInteractiveRole.cjs');
const isNonInteractiveElement = require('../util/isNonInteractiveElement.cjs');
const isNonInteractiveRole = require('../util/isNonInteractiveRole.cjs');
const isPresentationRole = require('../util/isPresentationRole.cjs');
const getTabIndex = require('../util/getTabIndex.cjs');
const schema = schemas.generateObjSchema({
// TODO: convert to use iterFilter and iterFrom
tabbable: schemas.enumArraySchema([...ariaQuery.roles.keys()].filter((name) => !ariaQuery.roles.get(name).abstract && ariaQuery.roles.get(name).superClass.some((klasses) => klasses.includes("widget"))))
});
const interactiveProps = [].concat(
jsxAstUtils.eventHandlersByType.mouse,
jsxAstUtils.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 = jsxAstUtils.hasAnyProp(attributes, interactiveProps);
const hasTabindex = getTabIndex(jsxAstUtils.getProp(attributes, "tabIndex")) !== void 0;
if (!ariaQuery.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 = jsxAstUtils.getLiteralPropValue(jsxAstUtils.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.`
});
}
}
}
};
}
};
module.exports = ruleOfInteractiveSupportsFocus;