|
| 1 | +const PRESENTATIONAL_ROLES = new Set(['none', 'presentation']); |
| 2 | + |
| 3 | +const PRESENTATIONAL_CHILDREN = { |
| 4 | + table: ['tr', 'td', 'th', 'thead', 'tbody', 'tfoot'], |
| 5 | + select: ['option', 'optgroup'], |
| 6 | + ol: ['li'], |
| 7 | + ul: ['li'], |
| 8 | + dl: ['dt', 'dd'], |
| 9 | +}; |
| 10 | + |
| 11 | +// Roles that require all descendants to be presentational |
| 12 | +// https://w3c.github.io/aria-practices/#children_presentational |
| 13 | +const ROLES_REQUIRING_PRESENTATIONAL_CHILDREN = new Set([ |
| 14 | + 'button', |
| 15 | + 'checkbox', |
| 16 | + 'img', |
| 17 | + 'meter', |
| 18 | + 'menuitemcheckbox', |
| 19 | + 'menuitemradio', |
| 20 | + 'option', |
| 21 | + 'progressbar', |
| 22 | + 'radio', |
| 23 | + 'scrollbar', |
| 24 | + 'separator', |
| 25 | + 'slider', |
| 26 | + 'switch', |
| 27 | + 'tab', |
| 28 | +]); |
| 29 | + |
| 30 | +// Tags that do not have semantic meaning |
| 31 | +const NON_SEMANTIC_TAGS = new Set([ |
| 32 | + 'span', |
| 33 | + 'div', |
| 34 | + 'basefont', |
| 35 | + 'big', |
| 36 | + 'blink', |
| 37 | + 'center', |
| 38 | + 'font', |
| 39 | + 'marquee', |
| 40 | + 's', |
| 41 | + 'spacer', |
| 42 | + 'strike', |
| 43 | + 'tt', |
| 44 | + 'u', |
| 45 | +]); |
| 46 | + |
| 47 | +const SKIPPED_TAGS = new Set([ |
| 48 | + // SVG tags can contain a lot of special child tags |
| 49 | + // Instead of marking all possible SVG child tags as NON_SEMANTIC_TAG, |
| 50 | + // we skip checking this rule for presentational SVGs |
| 51 | + 'svg', |
| 52 | +]); |
| 53 | + |
| 54 | +function getRoleValue(node) { |
| 55 | + const roleAttr = node.attributes?.find((a) => a.name === 'role'); |
| 56 | + if (!roleAttr || roleAttr.value?.type !== 'GlimmerTextNode') { |
| 57 | + return null; |
| 58 | + } |
| 59 | + return roleAttr.value.chars; |
| 60 | +} |
| 61 | + |
| 62 | +function hasPresentationalRole(node) { |
| 63 | + const role = getRoleValue(node); |
| 64 | + return role !== null && PRESENTATIONAL_ROLES.has(role); |
| 65 | +} |
| 66 | + |
| 67 | +function findAllSemanticDescendants(children, nonSemanticTags, results) { |
| 68 | + for (const child of children || []) { |
| 69 | + if (child.type === 'GlimmerElementNode') { |
| 70 | + // If child tag starts with ':', it's a named block — skip it but recurse into its children |
| 71 | + if (child.tag.startsWith(':')) { |
| 72 | + findAllSemanticDescendants(child.children, nonSemanticTags, results); |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + const isPresentational = hasPresentationalRole(child); |
| 77 | + |
| 78 | + // Include this node in results if it's not non-semantic and not presentational |
| 79 | + if (!nonSemanticTags.has(child.tag) && !isPresentational) { |
| 80 | + results.push(child); |
| 81 | + } |
| 82 | + |
| 83 | + // Always recurse into children — even if the current node is presentational, |
| 84 | + // its descendants may still be semantic and need to be reported |
| 85 | + findAllSemanticDescendants(child.children, nonSemanticTags, results); |
| 86 | + } |
| 87 | + } |
| 88 | + return results; |
| 89 | +} |
| 90 | + |
| 91 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 92 | +module.exports = { |
| 93 | + meta: { |
| 94 | + type: 'problem', |
| 95 | + docs: { |
| 96 | + description: 'require presentational elements to only contain presentational children', |
| 97 | + category: 'Accessibility', |
| 98 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-presentational-children.md', |
| 99 | + templateMode: 'both', |
| 100 | + }, |
| 101 | + fixable: null, |
| 102 | + schema: [ |
| 103 | + { |
| 104 | + type: 'object', |
| 105 | + properties: { |
| 106 | + additionalNonSemanticTags: { |
| 107 | + type: 'array', |
| 108 | + items: { type: 'string' }, |
| 109 | + uniqueItems: true, |
| 110 | + }, |
| 111 | + }, |
| 112 | + additionalProperties: false, |
| 113 | + }, |
| 114 | + ], |
| 115 | + messages: { |
| 116 | + invalid: |
| 117 | + 'Element <{{parent}}> has role="{{role}}" but contains semantic child <{{child}}>. Presentational elements should only contain presentational children.', |
| 118 | + }, |
| 119 | + originallyFrom: { |
| 120 | + name: 'ember-template-lint', |
| 121 | + rule: 'lib/rules/require-presentational-children.js', |
| 122 | + docs: 'docs/rule/require-presentational-children.md', |
| 123 | + tests: 'test/unit/rules/require-presentational-children-test.js', |
| 124 | + }, |
| 125 | + }, |
| 126 | + |
| 127 | + create(context) { |
| 128 | + const options = context.options[0] || {}; |
| 129 | + const nonSemanticTags = new Set([ |
| 130 | + ...NON_SEMANTIC_TAGS, |
| 131 | + ...(options.additionalNonSemanticTags || []), |
| 132 | + ]); |
| 133 | + |
| 134 | + return { |
| 135 | + GlimmerElementNode(node) { |
| 136 | + const roleAttr = node.attributes?.find((a) => a.name === 'role'); |
| 137 | + if (!roleAttr || roleAttr.value?.type !== 'GlimmerTextNode') { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + const role = roleAttr.value.chars; |
| 142 | + |
| 143 | + // Case 1: Presentational role (none/presentation) on specific parent elements |
| 144 | + if (PRESENTATIONAL_ROLES.has(role)) { |
| 145 | + const semanticChildren = PRESENTATIONAL_CHILDREN[node.tag]; |
| 146 | + if (!semanticChildren) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + if (node.children) { |
| 151 | + for (const child of node.children) { |
| 152 | + if ( |
| 153 | + child.type === 'GlimmerElementNode' && |
| 154 | + semanticChildren.includes(child.tag) && |
| 155 | + !nonSemanticTags.has(child.tag) |
| 156 | + ) { |
| 157 | + context.report({ |
| 158 | + node: child, |
| 159 | + messageId: 'invalid', |
| 160 | + data: { |
| 161 | + parent: node.tag, |
| 162 | + role, |
| 163 | + child: child.tag, |
| 164 | + }, |
| 165 | + }); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + // Case 2: Roles that require all descendants to be presentational |
| 173 | + if (ROLES_REQUIRING_PRESENTATIONAL_CHILDREN.has(role)) { |
| 174 | + // Skip SVG and similar tags |
| 175 | + if (SKIPPED_TAGS.has(node.tag)) { |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + const semanticDescendants = findAllSemanticDescendants( |
| 180 | + node.children, |
| 181 | + nonSemanticTags, |
| 182 | + [] |
| 183 | + ); |
| 184 | + for (const semanticChild of semanticDescendants) { |
| 185 | + context.report({ |
| 186 | + node: semanticChild, |
| 187 | + messageId: 'invalid', |
| 188 | + data: { |
| 189 | + parent: node.tag, |
| 190 | + role, |
| 191 | + child: semanticChild.tag, |
| 192 | + }, |
| 193 | + }); |
| 194 | + } |
| 195 | + } |
| 196 | + }, |
| 197 | + }; |
| 198 | + }, |
| 199 | +}; |
0 commit comments