aria-unsupported-elements.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  4. var _ariaQuery = require("aria-query");
  5. var _jsxAstUtils = require("jsx-ast-utils");
  6. var _schemas = require("../util/schemas");
  7. /**
  8. * @fileoverview Enforce that elements that do not support ARIA roles,
  9. * states and properties do not have those attributes.
  10. * @author Ethan Cohen
  11. */
  12. // ----------------------------------------------------------------------------
  13. // Rule Definition
  14. // ----------------------------------------------------------------------------
  15. var errorMessage = function errorMessage(invalidProp) {
  16. return "This element does not support ARIA roles, states and properties. Try removing the prop '".concat(invalidProp, "'.");
  17. };
  18. var schema = (0, _schemas.generateObjSchema)();
  19. module.exports = {
  20. meta: {
  21. docs: {
  22. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/aria-unsupported-elements.md'
  23. },
  24. schema: [schema]
  25. },
  26. create: function create(context) {
  27. return {
  28. JSXOpeningElement: function JSXOpeningElement(node) {
  29. var nodeType = (0, _jsxAstUtils.elementType)(node);
  30. var nodeAttrs = _ariaQuery.dom.get(nodeType) || {};
  31. var _nodeAttrs$reserved = nodeAttrs.reserved,
  32. isReservedNodeType = _nodeAttrs$reserved === void 0 ? false : _nodeAttrs$reserved; // If it's not reserved, then it can have aria-* roles, states, and properties
  33. if (isReservedNodeType === false) {
  34. return;
  35. }
  36. var invalidAttributes = (0, _toConsumableArray2["default"])(_ariaQuery.aria.keys()).concat('role');
  37. node.attributes.forEach(function (prop) {
  38. if (prop.type === 'JSXSpreadAttribute') {
  39. return;
  40. }
  41. var name = (0, _jsxAstUtils.propName)(prop).toLowerCase();
  42. if (invalidAttributes.indexOf(name) > -1) {
  43. context.report({
  44. node,
  45. message: errorMessage(name)
  46. });
  47. }
  48. });
  49. }
  50. };
  51. }
  52. };