@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
73 lines (70 loc) • 3.36 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 isAbstractRole = require('../util/isAbstractRole.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 isNonLiteralProperty = require('../util/isNonLiteralProperty.cjs');
const isPresentationRole = require('../util/isPresentationRole.cjs');
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(
jsxAstUtils.eventHandlersByType.focus,
jsxAstUtils.eventHandlersByType.keyboard,
jsxAstUtils.eventHandlersByType.mouse
);
const schema = schemas.generateObjSchema({
handlers: schemas.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) => jsxAstUtils.hasProp(attributes, prop) && jsxAstUtils.getPropValue(jsxAstUtils.getProp(attributes, prop)) != null);
if (!ariaQuery.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 = jsxAstUtils.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
});
}
};
}
};
module.exports = ruleOfNoStaticElementInteractions;