@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
53 lines (50 loc) • 2.18 kB
JavaScript
import { roles, aria } from 'aria-query';
import { f as filter, i as iterFrom } from '../index-d79WKvnw.js';
import { getProp, getLiteralPropValue, getPropValue, propName } from '../util/module/jsx-ast-utils.js';
import { generateObjSchema } from '../util/schemas.js';
import getElementType from '../util/getElementType.js';
import getImplicitRole from '../util/getImplicitRole.js';
const errorMessage = (attr, role, tag, isImplicit) => {
if (isImplicit) {
return `The attribute ${attr} is not supported by the role ${role}. This role is implicit on the element ${tag}.`;
}
return `The attribute ${attr} is not supported by the role ${role}.`;
};
const schema = generateObjSchema();
const ruleOfRoleSupportsAriaProps = {
meta: {
docs: {
url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/role-supports-aria-props.md",
description: "Enforce that elements with explicit or implicit roles defined contain only `aria-*` properties supported by that `role`."
},
schema: [schema]
},
create(context) {
const elementType = getElementType(context);
return {
JSXOpeningElement(node) {
const type = elementType(node);
const role = getProp(node.attributes, "role");
const roleValue = role ? getLiteralPropValue(role) : getImplicitRole(type, node.attributes);
const isImplicit = roleValue && role === void 0;
if (typeof roleValue !== "string" || roles.get(roleValue) === void 0) {
return;
}
const {
props: propKeyValues
} = roles.get(roleValue);
const invalidAriaPropsForRole = new Set(filter(iterFrom(aria.keys()), (attribute) => !(attribute in propKeyValues)));
node.attributes.filter((prop) => getPropValue(prop) != null && prop.type !== "JSXSpreadAttribute").forEach((prop) => {
const name = propName(prop);
if (invalidAriaPropsForRole.has(name)) {
context.report({
node,
message: errorMessage(name, roleValue, type, isImplicit)
});
}
});
}
};
}
};
export { ruleOfRoleSupportsAriaProps as default };