@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
58 lines (55 loc) • 2.05 kB
JavaScript
const ariaQuery = require('aria-query');
const jsxAstUtils = require('../util/module/jsx-ast-utils.cjs');
const getElementType = require('../util/getElementType.cjs');
const isInteractiveElement = require('../util/isInteractiveElement.cjs');
const isNonInteractiveRole = require('../util/isNonInteractiveRole.cjs');
const isPresentationRole = require('../util/isPresentationRole.cjs');
const errorMessage = "Interactive elements should not be assigned non-interactive roles.";
const ruleOfNoInteractiveElementToNoninteractiveRole = {
meta: {
docs: {
url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-interactive-element-to-noninteractive-role.md",
description: "Interactive elements should not be assigned non-interactive roles."
},
schema: [{
type: "object",
additionalProperties: {
type: "array",
items: {
type: "string"
},
uniqueItems: true
}
}]
},
create: (context) => {
const { options } = context;
const elementType = getElementType(context);
return {
JSXAttribute: (attribute) => {
const attributeName = jsxAstUtils.propName(attribute);
if (attributeName !== "role") {
return;
}
const node = attribute.parent;
const { attributes } = node;
const type = elementType(node);
const role = jsxAstUtils.getLiteralPropValue(jsxAstUtils.getProp(node.attributes, "role"));
if (!ariaQuery.dom.has(type)) {
return;
}
const allowedRoles = options[0] || {};
if (Object.hasOwn(allowedRoles, type) && allowedRoles[type].includes(role)) {
return;
}
if (isInteractiveElement(type, attributes) && (isNonInteractiveRole(type, attributes) || isPresentationRole(type, attributes))) {
context.report({
node: attribute,
message: errorMessage
});
}
}
};
}
};
module.exports = ruleOfNoInteractiveElementToNoninteractiveRole;