@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
47 lines (44 loc) • 1.67 kB
JavaScript
const ariaQuery = require('aria-query');
const jsxAstUtils = require('../util/module/jsx-ast-utils.cjs');
const schemas = require('../util/schemas.cjs');
const getElementType = require('../util/getElementType.cjs');
const errorMessage = (invalidProp) => `This element does not support ARIA roles, states and properties. Try removing the prop '${invalidProp}'.`;
const schema = schemas.generateObjSchema();
const ruleOfAriaUnsupportedElements = {
meta: {
docs: {
url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/aria-unsupported-elements.md",
description: "Enforce that elements that do not support ARIA roles, states, and properties do not have those attributes."
},
schema: [schema]
},
create: (context) => {
const elementType = getElementType(context);
return {
JSXOpeningElement: (node) => {
const nodeType = elementType(node);
const nodeAttrs = ariaQuery.dom.get(nodeType) || {};
const {
reserved: isReservedNodeType = false
} = nodeAttrs;
if (isReservedNodeType === false) {
return;
}
const invalidAttributes = /* @__PURE__ */ new Set([...ariaQuery.aria.keys(), "role"]);
node.attributes.forEach((prop) => {
if (prop.type === "JSXSpreadAttribute") {
return;
}
const name = jsxAstUtils.propName(prop).toLowerCase();
if (invalidAttributes.has(name)) {
context.report({
node,
message: errorMessage(name)
});
}
});
}
};
}
};
module.exports = ruleOfAriaUnsupportedElements;