source: frontend/node_modules/eslint-plugin-jsx-a11y/lib/rules/anchor-is-valid.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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