source: frontend/node_modules/eslint-plugin-jsx-a11y/lib/rules/label-has-for.js

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

Fix frontend appearance

  • Property mode set to 100644
File size: 5.3 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 _schemas = require("../util/schemas");
9var _getElementType = _interopRequireDefault(require("../util/getElementType"));
10var _hasAccessibleChild = _interopRequireDefault(require("../util/hasAccessibleChild"));
11function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
12/**
13 * @fileoverview Enforce label tags have htmlFor attribute.
14 * @author Ethan Cohen
15 */
16
17// ----------------------------------------------------------------------------
18// Rule Definition
19// ----------------------------------------------------------------------------
20
21var enumValues = ['nesting', 'id'];
22var schema = {
23 type: 'object',
24 properties: {
25 components: _schemas.arraySchema,
26 required: {
27 oneOf: [{
28 type: 'string',
29 "enum": enumValues
30 }, (0, _schemas.generateObjSchema)({
31 some: (0, _schemas.enumArraySchema)(enumValues)
32 }, ['some']), (0, _schemas.generateObjSchema)({
33 every: (0, _schemas.enumArraySchema)(enumValues)
34 }, ['every'])]
35 },
36 allowChildren: {
37 type: 'boolean'
38 }
39 }
40};
41// Breadth-first search, assuming that HTML for forms is shallow.
42function validateNesting(node) {
43 var queue = node.parent.children.slice();
44 var child;
45 var opener;
46 while (queue.length) {
47 child = queue.shift();
48 opener = child.openingElement;
49 if (child.type === 'JSXElement' && opener && (opener.name.name === 'input' || opener.name.name === 'textarea' || opener.name.name === 'select')) {
50 return true;
51 }
52 if (child.children) {
53 queue = queue.concat(child.children);
54 }
55 }
56 return false;
57}
58function validateID(_ref, context) {
59 var _settings$jsxA11y$at, _settings$jsxA11y, _settings$jsxA11y$att;
60 var attributes = _ref.attributes;
61 var settings = context.settings;
62 var htmlForAttributes = (_settings$jsxA11y$at = (_settings$jsxA11y = settings['jsx-a11y']) === null || _settings$jsxA11y === void 0 ? void 0 : (_settings$jsxA11y$att = _settings$jsxA11y.attributes) === null || _settings$jsxA11y$att === void 0 ? void 0 : _settings$jsxA11y$att["for"]) !== null && _settings$jsxA11y$at !== void 0 ? _settings$jsxA11y$at : ['htmlFor'];
63 for (var i = 0; i < htmlForAttributes.length; i += 1) {
64 var attribute = htmlForAttributes[i];
65 if ((0, _jsxAstUtils.hasProp)(attributes, attribute)) {
66 var htmlForAttr = (0, _jsxAstUtils.getProp)(attributes, attribute);
67 var htmlForValue = (0, _jsxAstUtils.getPropValue)(htmlForAttr);
68 return htmlForAttr !== false && !!htmlForValue;
69 }
70 }
71 return false;
72}
73function validate(node, required, allowChildren, elementType, context) {
74 if (allowChildren === true) {
75 return (0, _hasAccessibleChild["default"])(node.parent, elementType);
76 }
77 if (required === 'nesting') {
78 return validateNesting(node);
79 }
80 return validateID(node, context);
81}
82function getValidityStatus(node, required, allowChildren, elementType, context) {
83 if (Array.isArray(required.some)) {
84 var _isValid = required.some.some(function (rule) {
85 return validate(node, rule, allowChildren, elementType, context);
86 });
87 var _message = !_isValid ? "Form label must have ANY of the following types of associated control: ".concat(required.some.join(', ')) : null;
88 return {
89 isValid: _isValid,
90 message: _message
91 };
92 }
93 if (Array.isArray(required.every)) {
94 var _isValid2 = required.every.every(function (rule) {
95 return validate(node, rule, allowChildren, elementType, context);
96 });
97 var _message2 = !_isValid2 ? "Form label must have ALL of the following types of associated control: ".concat(required.every.join(', ')) : null;
98 return {
99 isValid: _isValid2,
100 message: _message2
101 };
102 }
103 var isValid = validate(node, required, allowChildren, elementType, context);
104 var message = !isValid ? "Form label must have the following type of associated control: ".concat(required) : null;
105 return {
106 isValid,
107 message
108 };
109}
110var _default = exports["default"] = {
111 meta: {
112 deprecated: true,
113 replacedBy: ['label-has-associated-control'],
114 docs: {
115 description: 'Enforce that `<label>` elements have the `htmlFor` prop.',
116 url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/label-has-for.md'
117 },
118 schema: [schema]
119 },
120 create: function create(context) {
121 var elementType = (0, _getElementType["default"])(context);
122 return {
123 JSXOpeningElement(node) {
124 var options = context.options[0] || {};
125 var componentOptions = options.components || [];
126 var typesToValidate = ['label'].concat(componentOptions);
127 var nodeType = elementType(node);
128
129 // Only check 'label' elements and custom types.
130 if (typesToValidate.indexOf(nodeType) === -1) {
131 return;
132 }
133 var required = options.required || {
134 every: ['nesting', 'id']
135 };
136 var allowChildren = options.allowChildren || false;
137 var _getValidityStatus = getValidityStatus(node, required, allowChildren, elementType, context),
138 isValid = _getValidityStatus.isValid,
139 message = _getValidityStatus.message;
140 if (!isValid) {
141 context.report({
142 node,
143 message
144 });
145 }
146 }
147 };
148 }
149};
150module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.