anchor-ambiguous-text.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = void 0;
  7. var _schemas = require("../util/schemas");
  8. var _getAccessibleChildText = _interopRequireDefault(require("../util/getAccessibleChildText"));
  9. var _getElementType = _interopRequireDefault(require("../util/getElementType"));
  10. /**
  11. * @fileoverview Enforce anchor text to not exactly match 'click here', 'here', 'link', 'learn more', and user-specified words.
  12. * @author Matt Wang
  13. *
  14. */
  15. // ----------------------------------------------------------------------------
  16. // Rule Definition
  17. // ----------------------------------------------------------------------------
  18. var DEFAULT_AMBIGUOUS_WORDS = ['click here', 'here', 'link', 'a link', 'learn more'];
  19. var schema = (0, _schemas.generateObjSchema)({
  20. words: _schemas.arraySchema
  21. });
  22. var _default = exports["default"] = {
  23. meta: {
  24. docs: {
  25. url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/anchor-ambiguous-text.md',
  26. description: 'Enforce `<a>` text to not exactly match "click here", "here", "link", or "a link".'
  27. },
  28. schema: [schema]
  29. },
  30. create: function create(context) {
  31. var elementType = (0, _getElementType["default"])(context);
  32. var typesToValidate = ['a'];
  33. var options = context.options[0] || {};
  34. var _options$words = options.words,
  35. words = _options$words === void 0 ? DEFAULT_AMBIGUOUS_WORDS : _options$words;
  36. var ambiguousWords = new Set(words);
  37. return {
  38. JSXOpeningElement: function JSXOpeningElement(node) {
  39. var nodeType = elementType(node);
  40. // Only check anchor elements and custom types.
  41. if (typesToValidate.indexOf(nodeType) === -1) {
  42. return;
  43. }
  44. var nodeText = (0, _getAccessibleChildText["default"])(node.parent, elementType);
  45. if (!ambiguousWords.has(nodeText)) {
  46. // check the value
  47. return;
  48. }
  49. context.report({
  50. node,
  51. message: 'Ambiguous text within anchor. Screenreader users rely on link text for context; the words "{{wordsList}}" are ambiguous and do not provide enough context.',
  52. data: {
  53. wordsList: words.join('", "')
  54. }
  55. });
  56. }
  57. };
  58. }
  59. };
  60. module.exports = exports.default;