anchor-has-content.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. /**
  7. * @fileoverview Enforce anchor elements to contain accessible content.
  8. * @author Lisa Ring & Niklas Holmberg
  9. */
  10. // ----------------------------------------------------------------------------
  11. // Rule Definition
  12. // ----------------------------------------------------------------------------
  13. var errorMessage = 'Anchors must have content and the content must be accessible by a screen reader.';
  14. var schema = (0, _schemas.generateObjSchema)({
  15. components: _schemas.arraySchema
  16. });
  17. module.exports = {
  18. meta: {
  19. docs: {
  20. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/anchor-has-content.md'
  21. },
  22. schema: [schema]
  23. },
  24. create: function create(context) {
  25. return {
  26. JSXOpeningElement: function JSXOpeningElement(node) {
  27. var options = context.options[0] || {};
  28. var componentOptions = options.components || [];
  29. var typeCheck = ['a'].concat(componentOptions);
  30. var nodeType = (0, _jsxAstUtils.elementType)(node); // Only check anchor elements and custom types.
  31. if (typeCheck.indexOf(nodeType) === -1) {
  32. return;
  33. }
  34. if ((0, _hasAccessibleChild["default"])(node.parent)) {
  35. return;
  36. }
  37. context.report({
  38. node,
  39. message: errorMessage
  40. });
  41. }
  42. };
  43. }
  44. };