heading-has-content.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _jsxAstUtils = require("jsx-ast-utils");
  4. var _schemas = require("../util/schemas");
  5. var _hasAccessibleChild = _interopRequireDefault(require("../util/hasAccessibleChild"));
  6. var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
  7. /**
  8. * @fileoverview Enforce heading (h1, h2, etc) elements contain accessible content.
  9. * @author Ethan Cohen
  10. */
  11. // ----------------------------------------------------------------------------
  12. // Rule Definition
  13. // ----------------------------------------------------------------------------
  14. var errorMessage = 'Headings must have content and the content must be accessible by a screen reader.';
  15. var headings = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
  16. var schema = (0, _schemas.generateObjSchema)({
  17. components: _schemas.arraySchema
  18. });
  19. module.exports = {
  20. meta: {
  21. docs: {
  22. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/heading-has-content.md'
  23. },
  24. schema: [schema]
  25. },
  26. create: function create(context) {
  27. return {
  28. JSXOpeningElement: function JSXOpeningElement(node) {
  29. var options = context.options[0] || {};
  30. var componentOptions = options.components || [];
  31. var typeCheck = headings.concat(componentOptions);
  32. var nodeType = (0, _jsxAstUtils.elementType)(node); // Only check 'h*' elements and custom types.
  33. if (typeCheck.indexOf(nodeType) === -1) {
  34. return;
  35. }
  36. if ((0, _hasAccessibleChild["default"])(node.parent)) {
  37. return;
  38. }
  39. if ((0, _isHiddenFromScreenReader["default"])(nodeType, node.attributes)) {
  40. return;
  41. }
  42. context.report({
  43. node,
  44. message: errorMessage
  45. });
  46. }
  47. };
  48. }
  49. };