@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
52 lines (49 loc) • 1.51 kB
JavaScript
const axobjectQuery = require('axobject-query');
const jsxAstUtils = require('./module/jsx-ast-utils.cjs');
const isSemanticRoleElement = (elementType, attributes) => {
const roleAttr = jsxAstUtils.getProp(attributes, "role");
let res = false;
const roleAttrValue = jsxAstUtils.getLiteralPropValue(roleAttr);
axobjectQuery.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 === jsxAstUtils.propName(attr);
let valuesMatch;
if (cAttr.value !== void 0) {
valuesMatch = cAttr.value === jsxAstUtils.getLiteralPropValue(attr);
}
if (!namesMatch) {
return false;
}
return namesMatch && valuesMatch !== void 0 ? valuesMatch : true;
}
)
)) {
axObjects.forEach((name) => {
if (res) {
return;
}
const roles = axobjectQuery.AXObjectRoles.get(name);
if (roles) {
roles.forEach((role) => {
if (res === true) {
return;
}
if (role.name === roleAttrValue) {
res = true;
}
});
}
});
}
});
return res;
};
module.exports = isSemanticRoleElement;