|
| 1 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 2 | +module.exports = { |
| 3 | + meta: { |
| 4 | + type: 'problem', |
| 5 | + docs: { |
| 6 | + description: 'disallow duplicate landmark elements without unique labels', |
| 7 | + category: 'Accessibility', |
| 8 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-duplicate-landmark-elements.md', |
| 9 | + templateMode: 'both', |
| 10 | + }, |
| 11 | + fixable: null, |
| 12 | + schema: [], |
| 13 | + messages: { |
| 14 | + duplicate: |
| 15 | + 'If multiple landmark elements (or elements with an equivalent role) of the same type are found on a page, they must each have a unique label.', |
| 16 | + }, |
| 17 | + originallyFrom: { |
| 18 | + name: 'ember-template-lint', |
| 19 | + rule: 'lib/rules/no-duplicate-landmark-elements.js', |
| 20 | + docs: 'docs/rule/no-duplicate-landmark-elements.md', |
| 21 | + tests: 'test/unit/rules/no-duplicate-landmark-elements-test.js', |
| 22 | + }, |
| 23 | + }, |
| 24 | + |
| 25 | + create(context) { |
| 26 | + // Map HTML5 landmark elements to their implicit ARIA roles |
| 27 | + const ELEMENT_TO_ROLE = { |
| 28 | + header: 'banner', |
| 29 | + footer: 'contentinfo', |
| 30 | + main: 'main', |
| 31 | + nav: 'navigation', |
| 32 | + aside: 'complementary', |
| 33 | + form: 'form', |
| 34 | + }; |
| 35 | + |
| 36 | + // Landmark ARIA roles |
| 37 | + const LANDMARK_ROLES = new Set([ |
| 38 | + 'banner', |
| 39 | + 'complementary', |
| 40 | + 'contentinfo', |
| 41 | + 'form', |
| 42 | + 'main', |
| 43 | + 'navigation', |
| 44 | + 'region', |
| 45 | + 'search', |
| 46 | + ]); |
| 47 | + |
| 48 | + // Sectioning elements that strip banner/contentinfo roles from header/footer |
| 49 | + const SECTIONING_ELEMENTS = new Set(['article', 'aside', 'main', 'nav', 'section']); |
| 50 | + const elementStack = []; |
| 51 | + |
| 52 | + // Stack-based scoping for landmarks to handle conditional branches. |
| 53 | + // Each scope is a Map<role, [{node, tag}]>. |
| 54 | + // When entering a GlimmerBlock that is a branch of an if/unless, |
| 55 | + // we push a copy of the current scope so that each branch starts |
| 56 | + // from the same baseline and landmarks from one branch don't |
| 57 | + // leak into another. |
| 58 | + const landmarksStack = [new Map()]; |
| 59 | + |
| 60 | + function currentLandmarks() { |
| 61 | + return landmarksStack.at(-1); |
| 62 | + } |
| 63 | + |
| 64 | + function cloneLandmarks(landmarks) { |
| 65 | + const clone = new Map(); |
| 66 | + for (const [role, entries] of landmarks) { |
| 67 | + clone.set(role, [...entries]); |
| 68 | + } |
| 69 | + return clone; |
| 70 | + } |
| 71 | + |
| 72 | + function isInsideSectioningElement() { |
| 73 | + return elementStack.some((tag) => SECTIONING_ELEMENTS.has(tag)); |
| 74 | + } |
| 75 | + |
| 76 | + function checkDuplicates(landmarks) { |
| 77 | + for (const [, entries] of landmarks) { |
| 78 | + if (entries.length > 1) { |
| 79 | + const labeled = []; |
| 80 | + const unlabeled = []; |
| 81 | + |
| 82 | + for (const entry of entries) { |
| 83 | + const label = getLabel(entry.node); |
| 84 | + if (label) { |
| 85 | + labeled.push({ node: entry.node, tag: entry.tag, label }); |
| 86 | + } else { |
| 87 | + unlabeled.push({ node: entry.node, tag: entry.tag }); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // When multiple landmarks of same type exist, unlabeled ones are violations |
| 92 | + if (unlabeled.length > 0) { |
| 93 | + if (unlabeled.length === entries.length) { |
| 94 | + // All unlabeled — report all but the first |
| 95 | + for (let i = 1; i < unlabeled.length; i++) { |
| 96 | + context.report({ |
| 97 | + node: unlabeled[i].node, |
| 98 | + messageId: 'duplicate', |
| 99 | + }); |
| 100 | + } |
| 101 | + } else { |
| 102 | + // Some are labeled, some aren't — report the unlabeled ones |
| 103 | + for (const entry of unlabeled) { |
| 104 | + context.report({ |
| 105 | + node: entry.node, |
| 106 | + messageId: 'duplicate', |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Report same-label duplicates among labeled landmarks |
| 113 | + const labelGroups = new Map(); |
| 114 | + for (const entry of labeled) { |
| 115 | + if (!labelGroups.has(entry.label)) { |
| 116 | + labelGroups.set(entry.label, []); |
| 117 | + } |
| 118 | + labelGroups.get(entry.label).push(entry); |
| 119 | + } |
| 120 | + for (const [, groupEntries] of labelGroups) { |
| 121 | + if (groupEntries.length > 1) { |
| 122 | + for (let i = 1; i < groupEntries.length; i++) { |
| 123 | + context.report({ |
| 124 | + node: groupEntries[i].node, |
| 125 | + messageId: 'duplicate', |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return { |
| 135 | + GlimmerElementNode(node) { |
| 136 | + elementStack.push(node.tag); |
| 137 | + |
| 138 | + const landmarkRole = getLandmarkRole( |
| 139 | + node, |
| 140 | + LANDMARK_ROLES, |
| 141 | + ELEMENT_TO_ROLE, |
| 142 | + isInsideSectioningElement() |
| 143 | + ); |
| 144 | + if (landmarkRole) { |
| 145 | + const landmarks = currentLandmarks(); |
| 146 | + if (!landmarks.has(landmarkRole)) { |
| 147 | + landmarks.set(landmarkRole, []); |
| 148 | + } |
| 149 | + landmarks.get(landmarkRole).push({ node, tag: node.tag }); |
| 150 | + } |
| 151 | + }, |
| 152 | + |
| 153 | + 'GlimmerElementNode:exit'() { |
| 154 | + elementStack.pop(); |
| 155 | + }, |
| 156 | + |
| 157 | + GlimmerBlock(node) { |
| 158 | + const parent = node.parent; |
| 159 | + if (parent && isIfUnless(parent)) { |
| 160 | + // Entering a branch of an if/unless block. |
| 161 | + // Push a copy of the scope from BEFORE the conditional |
| 162 | + // (which is the second-to-last on the stack, since the |
| 163 | + // conditional's own scope was pushed on enter). |
| 164 | + const parentScope = landmarksStack.at(-2) || landmarksStack.at(-1); |
| 165 | + landmarksStack.push(cloneLandmarks(parentScope)); |
| 166 | + } |
| 167 | + }, |
| 168 | + |
| 169 | + 'GlimmerBlock:exit'(node) { |
| 170 | + const parent = node.parent; |
| 171 | + if (parent && isIfUnless(parent)) { |
| 172 | + landmarksStack.pop(); |
| 173 | + } |
| 174 | + }, |
| 175 | + |
| 176 | + GlimmerBlockStatement(node) { |
| 177 | + if (isIfUnless(node)) { |
| 178 | + // Push a scope for the conditional block itself. |
| 179 | + // Each branch (GlimmerBlock) will push its own scope on top. |
| 180 | + landmarksStack.push(cloneLandmarks(currentLandmarks())); |
| 181 | + } |
| 182 | + }, |
| 183 | + |
| 184 | + 'GlimmerBlockStatement:exit'(node) { |
| 185 | + if (isIfUnless(node)) { |
| 186 | + landmarksStack.pop(); |
| 187 | + } |
| 188 | + }, |
| 189 | + |
| 190 | + 'Program:exit'() { |
| 191 | + checkDuplicates(currentLandmarks()); |
| 192 | + }, |
| 193 | + }; |
| 194 | + }, |
| 195 | +}; |
| 196 | + |
| 197 | +function isIfUnless(node) { |
| 198 | + if (node.type === 'GlimmerBlockStatement' && node.path?.type === 'GlimmerPathExpression') { |
| 199 | + return ['if', 'unless'].includes(node.path.original); |
| 200 | + } |
| 201 | + return false; |
| 202 | +} |
| 203 | + |
| 204 | +function getLabel(node) { |
| 205 | + // Check aria-label |
| 206 | + const ariaLabel = node.attributes?.find((attr) => attr.name === 'aria-label'); |
| 207 | + if (ariaLabel) { |
| 208 | + if (ariaLabel.value?.type === 'GlimmerTextNode') { |
| 209 | + return ariaLabel.value.chars.trim(); |
| 210 | + } |
| 211 | + // Dynamic aria-label — treat as a unique label (can't statically determine duplicates) |
| 212 | + return `__dynamic:${ariaLabel.range?.[0] || Math.random()}`; |
| 213 | + } |
| 214 | + |
| 215 | + // Check aria-labelledby - extract the ID value |
| 216 | + const ariaLabelledby = node.attributes?.find((attr) => attr.name === 'aria-labelledby'); |
| 217 | + if (ariaLabelledby) { |
| 218 | + if (ariaLabelledby.value?.type === 'GlimmerTextNode') { |
| 219 | + return `__labelledby:${ariaLabelledby.value.chars.trim()}`; |
| 220 | + } |
| 221 | + return `__dynamic:${ariaLabelledby.range?.[0] || Math.random()}`; |
| 222 | + } |
| 223 | + |
| 224 | + return null; |
| 225 | +} |
| 226 | + |
| 227 | +function getRoleValue(node) { |
| 228 | + const roleAttr = node.attributes?.find((attr) => attr.name === 'role'); |
| 229 | + if (roleAttr && roleAttr.value?.type === 'GlimmerTextNode') { |
| 230 | + return roleAttr.value.chars.trim(); |
| 231 | + } |
| 232 | + return null; |
| 233 | +} |
| 234 | + |
| 235 | +function getLandmarkRole(node, LANDMARK_ROLES, ELEMENT_TO_ROLE, insideSectioning) { |
| 236 | + const role = getRoleValue(node); |
| 237 | + if (role && LANDMARK_ROLES.has(role)) { |
| 238 | + return role; |
| 239 | + } |
| 240 | + const implicitRole = ELEMENT_TO_ROLE[node.tag]; |
| 241 | + if (implicitRole) { |
| 242 | + // header and footer lose their landmark role when inside sectioning elements |
| 243 | + if (insideSectioning && (node.tag === 'header' || node.tag === 'footer')) { |
| 244 | + return null; |
| 245 | + } |
| 246 | + return implicitRole; |
| 247 | + } |
| 248 | + return null; |
| 249 | +} |
0 commit comments