anchor-is-valid.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var _jsxAstUtils = require("jsx-ast-utils");
  7. var _safeRegexTest = _interopRequireDefault(require("safe-regex-test"));
  8. var _schemas = require("../util/schemas");
  9. var _getElementType = _interopRequireDefault(require("../util/getElementType"));
  10. function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
  11. /**
  12. * @fileoverview Performs validity check on anchor hrefs. Warns when anchors are used as buttons.
  13. * @author Almero Steyn
  14. *
  15. */
  16. // ----------------------------------------------------------------------------
  17. // Rule Definition
  18. // ----------------------------------------------------------------------------
  19. var allAspects = ['noHref', 'invalidHref', 'preferButton'];
  20. var preferButtonErrorMessage = 'Anchor used as a button. Anchors are primarily expected to navigate. Use the button element instead. Learn more: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/HEAD/docs/rules/anchor-is-valid.md';
  21. var noHrefErrorMessage = 'The href attribute is required for an anchor to be keyboard accessible. Provide a valid, navigable address as the href value. If you cannot provide an href, but still need the element to resemble a link, use a button and change it with appropriate styles. Learn more: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/HEAD/docs/rules/anchor-is-valid.md';
  22. var invalidHrefErrorMessage = 'The href attribute requires a valid value to be accessible. Provide a valid, navigable address as the href value. If you cannot provide a valid href, but still need the element to resemble a link, use a button and change it with appropriate styles. Learn more: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/HEAD/docs/rules/anchor-is-valid.md';
  23. var schema = (0, _schemas.generateObjSchema)({
  24. components: _schemas.arraySchema,
  25. specialLink: _schemas.arraySchema,
  26. aspects: (0, _schemas.enumArraySchema)(allAspects, 1)
  27. });
  28. var _default = exports["default"] = {
  29. meta: {
  30. docs: {
  31. url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/anchor-is-valid.md',
  32. description: 'Enforce all anchors are valid, navigable elements.'
  33. },
  34. schema: [schema]
  35. },
  36. create: function create(context) {
  37. var elementType = (0, _getElementType["default"])(context);
  38. var testJShref = (0, _safeRegexTest["default"])(/^\W*?javascript:/);
  39. return {
  40. JSXOpeningElement: function JSXOpeningElement(node) {
  41. var attributes = node.attributes;
  42. var options = context.options[0] || {};
  43. var componentOptions = options.components || [];
  44. var typeCheck = ['a'].concat(componentOptions);
  45. var nodeType = elementType(node);
  46. // Only check anchor elements and custom types.
  47. if (typeCheck.indexOf(nodeType) === -1) {
  48. return;
  49. }
  50. // Set up the rule aspects to check.
  51. var aspects = options.aspects || allAspects;
  52. // Create active aspect flag object. Failing checks will only report
  53. // if the related flag is set to true.
  54. var activeAspects = {};
  55. allAspects.forEach(function (aspect) {
  56. activeAspects[aspect] = aspects.indexOf(aspect) !== -1;
  57. });
  58. var propOptions = options.specialLink || [];
  59. var propsToValidate = ['href'].concat(propOptions);
  60. var values = propsToValidate.map(function (prop) {
  61. return (0, _jsxAstUtils.getPropValue)((0, _jsxAstUtils.getProp)(node.attributes, prop));
  62. });
  63. // Checks if any actual or custom href prop is provided.
  64. var hasAnyHref = values.some(function (value) {
  65. return value != null;
  66. });
  67. // Need to check for spread operator as props can be spread onto the element
  68. // leading to an incorrect validation error.
  69. var hasSpreadOperator = attributes.some(function (prop) {
  70. return prop.type === 'JSXSpreadAttribute';
  71. });
  72. var onClick = (0, _jsxAstUtils.getProp)(attributes, 'onClick');
  73. // When there is no href at all, specific scenarios apply:
  74. if (!hasAnyHref) {
  75. // If no spread operator is found and no onClick event is present
  76. // it is a link without href.
  77. if (!hasSpreadOperator && activeAspects.noHref && (!onClick || onClick && !activeAspects.preferButton)) {
  78. context.report({
  79. node,
  80. message: noHrefErrorMessage
  81. });
  82. }
  83. // If no spread operator is found but an onClick is preset it should be a button.
  84. if (!hasSpreadOperator && onClick && activeAspects.preferButton) {
  85. context.report({
  86. node,
  87. message: preferButtonErrorMessage
  88. });
  89. }
  90. return;
  91. }
  92. // Hrefs have been found, now check for validity.
  93. var invalidHrefValues = values.filter(function (value) {
  94. return value != null && typeof value === 'string' && (!value.length || value === '#' || testJShref(value));
  95. });
  96. if (invalidHrefValues.length !== 0) {
  97. // If an onClick is found it should be a button, otherwise it is an invalid link.
  98. if (onClick && activeAspects.preferButton) {
  99. context.report({
  100. node,
  101. message: preferButtonErrorMessage
  102. });
  103. } else if (activeAspects.invalidHref) {
  104. context.report({
  105. node,
  106. message: invalidHrefErrorMessage
  107. });
  108. }
  109. }
  110. }
  111. };
  112. }
  113. };
  114. module.exports = exports.default;