@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
73 lines (70 loc) • 3.25 kB
JavaScript
import { dom } from 'aria-query';
import { eventHandlersByType, hasProp, getPropValue, getProp } from '../util/module/jsx-ast-utils.js';
import { generateObjSchema, arraySchema } from '../util/schemas.js';
import getElementType from '../util/getElementType.js';
import isAbstractRole from '../util/isAbstractRole.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 isNonLiteralProperty from '../util/isNonLiteralProperty.js';
import isPresentationRole from '../util/isPresentationRole.js';
const errorMessage = "Avoid non-native interactive elements. If using native HTML is not possible, add an appropriate role and support for tabbing, mouse, keyboard, and touch inputs to an interactive content element.";
const defaultInteractiveProps = [].concat(
eventHandlersByType.focus,
eventHandlersByType.keyboard,
eventHandlersByType.mouse
);
const schema = generateObjSchema({
handlers: arraySchema
});
const ruleOfNoStaticElementInteractions = {
meta: {
docs: {
url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-static-element-interactions.md",
description: "Enforce that non-interactive, visible elements (such as `<div>`) that have click handlers use the role attribute."
},
schema: [schema]
},
create: (context) => {
const { options } = context;
const elementType = getElementType(context);
return {
JSXOpeningElement: (node) => {
const { attributes } = node;
const type = elementType(node);
const {
allowExpressionValues,
handlers = defaultInteractiveProps
} = options[0] || {};
const hasInteractiveProps = handlers.some((prop) => hasProp(attributes, prop) && getPropValue(getProp(attributes, prop)) != null);
if (!dom.has(type)) {
return;
}
if (!hasInteractiveProps || isHiddenFromScreenReader(type, attributes) || isPresentationRole(type, attributes)) {
return;
}
if (isInteractiveElement(type, attributes) || isInteractiveRole(type, attributes) || isNonInteractiveElement(type, attributes) || isNonInteractiveRole(type, attributes) || isAbstractRole(type, attributes)) {
return;
}
if (allowExpressionValues === true && isNonLiteralProperty(attributes, "role")) {
const roleProp = getProp(attributes, "role");
if (roleProp && roleProp.type === "JSXAttribute" && roleProp.value.type === "JSXExpressionContainer") {
if (roleProp.value.expression.type === "ConditionalExpression") {
if (roleProp.value.expression.consequent.type === "Literal" && roleProp.value.expression.alternate.type === "Literal") {
return;
}
}
}
return;
}
context.report({
node,
message: errorMessage
});
}
};
}
};
export { ruleOfNoStaticElementInteractions as default };