|
| 1 | +function hasAttr(node, name) { |
| 2 | + return node.attributes?.some((a) => a.name === name); |
| 3 | +} |
| 4 | + |
| 5 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 6 | +module.exports = { |
| 7 | + meta: { |
| 8 | + type: 'suggestion', |
| 9 | + docs: { |
| 10 | + description: 'require label for form input elements', |
| 11 | + category: 'Accessibility', |
| 12 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-input-label.md', |
| 13 | + templateMode: 'both', |
| 14 | + }, |
| 15 | + schema: [ |
| 16 | + { |
| 17 | + type: 'object', |
| 18 | + properties: { |
| 19 | + labelTags: { |
| 20 | + type: 'array', |
| 21 | + items: { type: 'string' }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + additionalProperties: false, |
| 25 | + }, |
| 26 | + ], |
| 27 | + messages: { |
| 28 | + requireLabel: 'Input elements should have an associated label.', |
| 29 | + multipleLabels: 'Input element has multiple labelling mechanisms.', |
| 30 | + }, |
| 31 | + originallyFrom: { |
| 32 | + name: 'ember-template-lint', |
| 33 | + rule: 'lib/rules/require-input-label.js', |
| 34 | + docs: 'docs/rule/require-input-label.md', |
| 35 | + tests: 'test/unit/rules/require-input-label-test.js', |
| 36 | + }, |
| 37 | + }, |
| 38 | + |
| 39 | + create(context) { |
| 40 | + const options = context.options[0] || {}; |
| 41 | + const customLabelTags = options.labelTags || []; |
| 42 | + const labelTags = new Set(['label', ...customLabelTags]); |
| 43 | + const elementStack = []; |
| 44 | + |
| 45 | + function hasValidLabelParent() { |
| 46 | + for (let i = elementStack.length - 1; i >= 0; i--) { |
| 47 | + const entry = elementStack[i]; |
| 48 | + if (labelTags.has(entry.tag)) { |
| 49 | + // Custom label tags (not 'label') are always considered valid |
| 50 | + if (entry.tag !== 'label') { |
| 51 | + return true; |
| 52 | + } |
| 53 | + // For 'label' tag, valid only if it has more than one child (text content + input) |
| 54 | + const children = entry.node.children || []; |
| 55 | + return children.length > 1; |
| 56 | + } |
| 57 | + } |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + return { |
| 62 | + GlimmerElementNode(node) { |
| 63 | + elementStack.push({ tag: node.tag, node }); |
| 64 | + |
| 65 | + const tagName = node.tag?.toLowerCase(); |
| 66 | + if (tagName !== 'input' && tagName !== 'textarea' && tagName !== 'select') { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Skip if input has type="hidden" |
| 71 | + const typeAttr = node.attributes?.find((a) => a.name === 'type'); |
| 72 | + if (typeAttr?.value?.type === 'GlimmerTextNode' && typeAttr.value.chars === 'hidden') { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // Skip if has ...attributes (can't determine labelling) |
| 77 | + if (hasAttr(node, '...attributes')) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + let labelCount = 0; |
| 82 | + const validLabel = hasValidLabelParent(); |
| 83 | + if (validLabel) { |
| 84 | + labelCount++; |
| 85 | + } |
| 86 | + |
| 87 | + const hasId = hasAttr(node, 'id'); |
| 88 | + const hasAriaLabel = hasAttr(node, 'aria-label'); |
| 89 | + const hasAriaLabelledBy = hasAttr(node, 'aria-labelledby'); |
| 90 | + if (hasId) { |
| 91 | + labelCount++; |
| 92 | + } |
| 93 | + if (hasAriaLabel) { |
| 94 | + labelCount++; |
| 95 | + } |
| 96 | + if (hasAriaLabelledBy) { |
| 97 | + labelCount++; |
| 98 | + } |
| 99 | + |
| 100 | + if (labelCount === 1) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // Special case: label parent + id is OK (common pattern) |
| 105 | + if (validLabel && hasId) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + context.report({ |
| 110 | + node, |
| 111 | + messageId: labelCount === 0 ? 'requireLabel' : 'multipleLabels', |
| 112 | + }); |
| 113 | + }, |
| 114 | + 'GlimmerElementNode:exit'() { |
| 115 | + elementStack.pop(); |
| 116 | + }, |
| 117 | + |
| 118 | + GlimmerMustacheStatement(node) { |
| 119 | + const name = node.path?.original; |
| 120 | + if (name !== 'input' && name !== 'textarea') { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + const pairs = node.hash?.pairs || []; |
| 125 | + |
| 126 | + function hasPair(key) { |
| 127 | + return pairs.some((p) => p.key === key); |
| 128 | + } |
| 129 | + |
| 130 | + // Skip if type="hidden" (literal string only) |
| 131 | + const typePair = pairs.find((p) => p.key === 'type'); |
| 132 | + if (typePair?.value?.type === 'GlimmerStringLiteral' && typePair.value.value === 'hidden') { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + // If in a valid label, it's valid |
| 137 | + if (hasValidLabelParent()) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + // If has id, it's valid |
| 142 | + if (hasPair('id')) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + context.report({ |
| 147 | + node, |
| 148 | + messageId: 'requireLabel', |
| 149 | + }); |
| 150 | + }, |
| 151 | + }; |
| 152 | + }, |
| 153 | +}; |
0 commit comments