@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
66 lines (63 loc) • 2.06 kB
JavaScript
import { roles, dom } from 'aria-query';
import { f as filter, i as iterFrom } from '../index-d79WKvnw.js';
import { propName, getLiteralPropValue } from '../util/module/jsx-ast-utils.js';
import getElementType from '../util/getElementType.js';
import { generateObjSchema } from '../util/schemas.js';
const errorMessage = "Elements with ARIA roles must use a valid, non-abstract ARIA role.";
const schema = generateObjSchema({
allowedInvalidRoles: {
items: {
type: "string"
},
type: "array",
uniqueItems: true
},
ignoreNonDOM: {
type: "boolean",
default: false
}
});
const validRoles = new Set(filter(iterFrom(roles.keys()), (role) => roles.get(role).abstract === false));
const ruleOfAriaRole = {
meta: {
docs: {
url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/aria-role.md",
description: "Enforce that elements with ARIA roles must use a valid, non-abstract ARIA role."
},
schema: [schema]
},
create: (context) => {
const options = context.options[0] || {};
const ignoreNonDOM = !!options.ignoreNonDOM;
const allowedInvalidRoles = new Set(options.allowedInvalidRoles || []);
const elementType = getElementType(context);
return {
JSXAttribute: (attribute) => {
if (ignoreNonDOM) {
const type = elementType(attribute.parent);
if (!dom.get(type)) {
return;
}
}
const name = propName(attribute).toUpperCase();
if (name !== "ROLE") {
return;
}
const value = getLiteralPropValue(attribute);
if (value === void 0 || value === null) {
return;
}
const values = String(value).split(" ");
const isValid = values.every((val) => allowedInvalidRoles.has(val) || validRoles.has(val));
if (isValid === true) {
return;
}
context.report({
node: attribute,
message: errorMessage
});
}
};
}
};
export { ruleOfAriaRole as default };