@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
53 lines (50 loc) • 2.22 kB
JavaScript
const ariaQuery = require('aria-query');
const index = require('../index-BnNhLopb.cjs');
const jsxAstUtils = require('../util/module/jsx-ast-utils.cjs');
const schemas = require('../util/schemas.cjs');
const getElementType = require('../util/getElementType.cjs');
const getImplicitRole = require('../util/getImplicitRole.cjs');
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 = schemas.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 = jsxAstUtils.getProp(node.attributes, "role");
const roleValue = role ? jsxAstUtils.getLiteralPropValue(role) : getImplicitRole(type, node.attributes);
const isImplicit = roleValue && role === void 0;
if (typeof roleValue !== "string" || ariaQuery.roles.get(roleValue) === void 0) {
return;
}
const {
props: propKeyValues
} = ariaQuery.roles.get(roleValue);
const invalidAriaPropsForRole = new Set(index.filter(index.iterFrom(ariaQuery.aria.keys()), (attribute) => !(attribute in propKeyValues)));
node.attributes.filter((prop) => jsxAstUtils.getPropValue(prop) != null && prop.type !== "JSXSpreadAttribute").forEach((prop) => {
const name = jsxAstUtils.propName(prop);
if (invalidAriaPropsForRole.has(name)) {
context.report({
node,
message: errorMessage(name, roleValue, type, isImplicit)
});
}
});
}
};
}
};
module.exports = ruleOfRoleSupportsAriaProps;