@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
52 lines (49 loc) • 1.48 kB
JavaScript
import { elementAXObjects, AXObjectRoles } from 'axobject-query';
import { getProp, getLiteralPropValue, propName } from './module/jsx-ast-utils.js';
const isSemanticRoleElement = (elementType, attributes) => {
const roleAttr = getProp(attributes, "role");
let res = false;
const roleAttrValue = getLiteralPropValue(roleAttr);
elementAXObjects.forEach((axObjects, concept) => {
if (res) {
return;
}
if (concept.name === elementType && (concept.attributes || []).every(
(cAttr) => attributes.some(
(attr) => {
if (!attr.type || attr.type !== "JSXAttribute") {
return false;
}
const namesMatch = cAttr.name === propName(attr);
let valuesMatch;
if (cAttr.value !== void 0) {
valuesMatch = cAttr.value === getLiteralPropValue(attr);
}
if (!namesMatch) {
return false;
}
return namesMatch && valuesMatch !== void 0 ? valuesMatch : true;
}
)
)) {
axObjects.forEach((name) => {
if (res) {
return;
}
const roles = AXObjectRoles.get(name);
if (roles) {
roles.forEach((role) => {
if (res === true) {
return;
}
if (role.name === roleAttrValue) {
res = true;
}
});
}
});
}
});
return res;
};
export { isSemanticRoleElement as default };