|
| 1 | +function hasAttr(node, name) { |
| 2 | + return node.attributes?.some((a) => a.name === name); |
| 3 | +} |
| 4 | + |
| 5 | +function isString(value) { |
| 6 | + return typeof value === 'string'; |
| 7 | +} |
| 8 | + |
| 9 | +function isRegExp(value) { |
| 10 | + return value instanceof RegExp; |
| 11 | +} |
| 12 | + |
| 13 | +function allowedFormat(value) { |
| 14 | + return isString(value) || isRegExp(value); |
| 15 | +} |
| 16 | + |
| 17 | +function parseConfig(config) { |
| 18 | + if (config === false) { |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
| 22 | + if (config === true || config === undefined) { |
| 23 | + return { labelTags: ['label'] }; |
| 24 | + } |
| 25 | + |
| 26 | + if (config && typeof config === 'object' && Array.isArray(config.labelTags)) { |
| 27 | + return { |
| 28 | + labelTags: ['label', ...config.labelTags.filter(allowedFormat)], |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + return { labelTags: ['label'] }; |
| 33 | +} |
| 34 | + |
| 35 | +function matchesLabelTag(tag, configuredTag) { |
| 36 | + return isRegExp(configuredTag) ? configuredTag.test(tag) : configuredTag === tag; |
| 37 | +} |
| 38 | + |
| 39 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 40 | +module.exports = { |
| 41 | + meta: { |
| 42 | + type: 'suggestion', |
| 43 | + docs: { |
| 44 | + description: 'require label for form input elements', |
| 45 | + category: 'Accessibility', |
| 46 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-input-label.md', |
| 47 | + templateMode: 'both', |
| 48 | + }, |
| 49 | + schema: [ |
| 50 | + { |
| 51 | + anyOf: [ |
| 52 | + { type: 'boolean' }, |
| 53 | + { |
| 54 | + type: 'object', |
| 55 | + properties: { |
| 56 | + labelTags: { |
| 57 | + type: 'array', |
| 58 | + }, |
| 59 | + }, |
| 60 | + additionalProperties: false, |
| 61 | + }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + ], |
| 65 | + messages: { |
| 66 | + requireLabel: 'form elements require a valid associated label.', |
| 67 | + multipleLabels: 'form elements should not have multiple labels.', |
| 68 | + }, |
| 69 | + originallyFrom: { |
| 70 | + name: 'ember-template-lint', |
| 71 | + rule: 'lib/rules/require-input-label.js', |
| 72 | + docs: 'docs/rule/require-input-label.md', |
| 73 | + tests: 'test/unit/rules/require-input-label-test.js', |
| 74 | + }, |
| 75 | + }, |
| 76 | + |
| 77 | + create(context) { |
| 78 | + const config = parseConfig(context.options[0]); |
| 79 | + if (config === false) { |
| 80 | + return {}; |
| 81 | + } |
| 82 | + |
| 83 | + const filename = context.getFilename(); |
| 84 | + const isStrictMode = filename.endsWith('.gjs') || filename.endsWith('.gts'); |
| 85 | + const elementStack = []; |
| 86 | + |
| 87 | + function hasValidLabelParent() { |
| 88 | + for (let i = elementStack.length - 1; i >= 0; i--) { |
| 89 | + const entry = elementStack[i]; |
| 90 | + const hasMatchingLabelTag = config.labelTags.some((configuredTag) => |
| 91 | + matchesLabelTag(entry.tag, configuredTag) |
| 92 | + ); |
| 93 | + |
| 94 | + if (hasMatchingLabelTag) { |
| 95 | + if (entry.tag !== 'label') { |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + const children = entry.node.children || []; |
| 100 | + return children.length > 1; |
| 101 | + } |
| 102 | + } |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + return { |
| 107 | + GlimmerElementNode(node) { |
| 108 | + elementStack.push({ tag: node.tag, node }); |
| 109 | + |
| 110 | + if (isStrictMode && (node.tag === 'Input' || node.tag === 'Textarea')) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + const tagName = node.tag?.toLowerCase(); |
| 115 | + if (tagName !== 'input' && tagName !== 'textarea' && tagName !== 'select') { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + // Skip if input has type="hidden" |
| 120 | + const typeAttr = node.attributes?.find((a) => a.name === 'type'); |
| 121 | + if (typeAttr?.value?.type === 'GlimmerTextNode' && typeAttr.value.chars === 'hidden') { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + // Skip if has ...attributes (can't determine labelling) |
| 126 | + if (hasAttr(node, '...attributes')) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + let labelCount = 0; |
| 131 | + const validLabel = hasValidLabelParent(); |
| 132 | + if (validLabel) { |
| 133 | + labelCount++; |
| 134 | + } |
| 135 | + |
| 136 | + const hasId = hasAttr(node, 'id'); |
| 137 | + const hasAriaLabel = hasAttr(node, 'aria-label'); |
| 138 | + const hasAriaLabelledBy = hasAttr(node, 'aria-labelledby'); |
| 139 | + if (hasId) { |
| 140 | + labelCount++; |
| 141 | + } |
| 142 | + if (hasAriaLabel) { |
| 143 | + labelCount++; |
| 144 | + } |
| 145 | + if (hasAriaLabelledBy) { |
| 146 | + labelCount++; |
| 147 | + } |
| 148 | + |
| 149 | + if (labelCount === 1) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + if (validLabel && hasId) { |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + context.report({ |
| 158 | + node, |
| 159 | + messageId: labelCount === 0 ? 'requireLabel' : 'multipleLabels', |
| 160 | + }); |
| 161 | + }, |
| 162 | + 'GlimmerElementNode:exit'() { |
| 163 | + elementStack.pop(); |
| 164 | + }, |
| 165 | + |
| 166 | + GlimmerMustacheStatement(node) { |
| 167 | + const name = node.path?.original; |
| 168 | + if (name !== 'input' && name !== 'textarea') { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + const pairs = node.hash?.pairs || []; |
| 173 | + |
| 174 | + function hasPair(key) { |
| 175 | + return pairs.some((p) => p.key === key); |
| 176 | + } |
| 177 | + |
| 178 | + // Skip if type="hidden" (literal string only) |
| 179 | + const typePair = pairs.find((p) => p.key === 'type'); |
| 180 | + if (typePair?.value?.type === 'GlimmerStringLiteral' && typePair.value.value === 'hidden') { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + // If in a valid label, it's valid |
| 185 | + if (hasValidLabelParent()) { |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + // If has id, it's valid |
| 190 | + if (hasPair('id')) { |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + context.report({ |
| 195 | + node, |
| 196 | + messageId: 'requireLabel', |
| 197 | + }); |
| 198 | + }, |
| 199 | + }; |
| 200 | + }, |
| 201 | +}; |
0 commit comments