@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
58 lines (55 loc) • 2 kB
JavaScript
import { dom } from 'aria-query';
import { propName, getLiteralPropValue, getProp } from '../util/module/jsx-ast-utils.js';
import getElementType from '../util/getElementType.js';
import isInteractiveElement from '../util/isInteractiveElement.js';
import isNonInteractiveRole from '../util/isNonInteractiveRole.js';
import isPresentationRole from '../util/isPresentationRole.js';
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 = propName(attribute);
if (attributeName !== "role") {
return;
}
const node = attribute.parent;
const { attributes } = node;
const type = elementType(node);
const role = getLiteralPropValue(getProp(node.attributes, "role"));
if (!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
});
}
}
};
}
};
export { ruleOfNoInteractiveElementToNoninteractiveRole as default };