You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

heading-has-content.js 2.0 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. var _jsxAstUtils = require('jsx-ast-utils');
  3. var _schemas = require('../util/schemas');
  4. var _hasAccessibleChild = require('../util/hasAccessibleChild');
  5. var _hasAccessibleChild2 = _interopRequireDefault(_hasAccessibleChild);
  6. var _isHiddenFromScreenReader = require('../util/isHiddenFromScreenReader');
  7. var _isHiddenFromScreenReader2 = _interopRequireDefault(_isHiddenFromScreenReader);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @fileoverview Enforce heading (h1, h2, etc) elements contain accessible content.
  11. * @author Ethan Cohen
  12. */
  13. // ----------------------------------------------------------------------------
  14. // Rule Definition
  15. // ----------------------------------------------------------------------------
  16. var errorMessage = 'Headings must have content and the content must be accessible by a screen reader.';
  17. var headings = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
  18. var schema = (0, _schemas.generateObjSchema)({ components: _schemas.arraySchema });
  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);
  33. // Only check 'h*' elements and custom types.
  34. if (typeCheck.indexOf(nodeType) === -1) {
  35. return;
  36. }
  37. if ((0, _hasAccessibleChild2.default)(node.parent)) {
  38. return;
  39. }
  40. if ((0, _isHiddenFromScreenReader2.default)(nodeType, node.attributes)) {
  41. return;
  42. }
  43. context.report({
  44. node: node,
  45. message: errorMessage
  46. });
  47. }
  48. };
  49. }
  50. };