anchor-is-valid.js 5.1 KB

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