|
| 1 | +const ROLES_REQUIRING_CONTEXT = { |
| 2 | + listitem: ['list', 'group', 'directory'], |
| 3 | + option: ['listbox', 'group'], |
| 4 | + tab: ['tablist'], |
| 5 | + menuitem: ['menu', 'menubar', 'group'], |
| 6 | + menuitemcheckbox: ['menu', 'menubar', 'group'], |
| 7 | + menuitemradio: ['menu', 'menubar', 'group'], |
| 8 | + treeitem: ['tree', 'group'], |
| 9 | + row: ['table', 'grid', 'treegrid', 'rowgroup'], |
| 10 | + rowgroup: ['grid', 'table', 'treegrid'], |
| 11 | + rowheader: ['row'], |
| 12 | + columnheader: ['row'], |
| 13 | + gridcell: ['row'], |
| 14 | +}; |
| 15 | + |
| 16 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 17 | +module.exports = { |
| 18 | + meta: { |
| 19 | + type: 'problem', |
| 20 | + docs: { |
| 21 | + description: 'require ARIA roles to be used in appropriate context', |
| 22 | + category: 'Accessibility', |
| 23 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-context-role.md', |
| 24 | + templateMode: 'both', |
| 25 | + }, |
| 26 | + fixable: null, |
| 27 | + schema: [], |
| 28 | + messages: { |
| 29 | + missingContext: |
| 30 | + 'Role "{{role}}" must be contained in an element with one of these roles: {{requiredRoles}}', |
| 31 | + }, |
| 32 | + originallyFrom: { |
| 33 | + name: 'ember-template-lint', |
| 34 | + rule: 'lib/rules/require-context-role.js', |
| 35 | + docs: 'docs/rule/require-context-role.md', |
| 36 | + tests: 'test/unit/rules/require-context-role-test.js', |
| 37 | + }, |
| 38 | + }, |
| 39 | + |
| 40 | + create(context) { |
| 41 | + const elementStack = []; |
| 42 | + |
| 43 | + return { |
| 44 | + GlimmerElementNode(node) { |
| 45 | + elementStack.push(node); |
| 46 | + |
| 47 | + const role = getRoleFromNode(node); |
| 48 | + |
| 49 | + if (role && ROLES_REQUIRING_CONTEXT[role]) { |
| 50 | + // Skip check if at root level (no parent elements — context may be external) |
| 51 | + if (elementStack.length > 1 && !isInsideAriaHidden(elementStack)) { |
| 52 | + const parentRole = getAccessibleParentRole(elementStack); |
| 53 | + if (parentRole === undefined) { |
| 54 | + // No non-transparent parent found (effectively root) — skip |
| 55 | + } else if (!parentRole || !ROLES_REQUIRING_CONTEXT[role].includes(parentRole)) { |
| 56 | + context.report({ |
| 57 | + node, |
| 58 | + messageId: 'missingContext', |
| 59 | + data: { |
| 60 | + role, |
| 61 | + requiredRoles: ROLES_REQUIRING_CONTEXT[role].join(', '), |
| 62 | + }, |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + }, |
| 68 | + |
| 69 | + 'GlimmerElementNode:exit'() { |
| 70 | + elementStack.pop(); |
| 71 | + }, |
| 72 | + }; |
| 73 | + }, |
| 74 | +}; |
| 75 | + |
| 76 | +function getRoleFromNode(node) { |
| 77 | + const roleAttr = node.attributes?.find((a) => a.name === 'role'); |
| 78 | + if (roleAttr?.value?.type === 'GlimmerTextNode') { |
| 79 | + return roleAttr.value.chars; |
| 80 | + } |
| 81 | + return null; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Check if any ancestor element in the stack has aria-hidden="true". |
| 86 | + */ |
| 87 | +function isInsideAriaHidden(elementStack) { |
| 88 | + // Check ancestors (all elements except the current one) |
| 89 | + for (let i = elementStack.length - 2; i >= 0; i--) { |
| 90 | + const node = elementStack[i]; |
| 91 | + const ariaHidden = node.attributes?.find((a) => a.name === 'aria-hidden'); |
| 92 | + if (ariaHidden?.value?.type === 'GlimmerTextNode' && ariaHidden.value.chars === 'true') { |
| 93 | + return true; |
| 94 | + } |
| 95 | + } |
| 96 | + return false; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Get the role of the nearest non-transparent ancestor element. |
| 101 | + * Transparent elements are those with role="presentation"/"none" or named blocks (tag starts with ':'). |
| 102 | + * Returns: |
| 103 | + * - a role string if a non-transparent ancestor with a role is found |
| 104 | + * - null if a non-transparent ancestor WITHOUT a role is found (breaks context) |
| 105 | + * - undefined if no non-transparent ancestor exists (root level) |
| 106 | + */ |
| 107 | +function getAccessibleParentRole(elementStack) { |
| 108 | + for (let i = elementStack.length - 2; i >= 0; i--) { |
| 109 | + const node = elementStack[i]; |
| 110 | + |
| 111 | + // Named blocks (e.g. <:content>) and <template> wrapper are transparent |
| 112 | + if (node.tag && (node.tag.startsWith(':') || node.tag === 'template')) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + const role = getRoleFromNode(node); |
| 117 | + |
| 118 | + // Presentation/none roles are transparent in the accessibility tree |
| 119 | + if (role === 'presentation' || role === 'none') { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + return role; // could be null (element with no role) or a role string |
| 124 | + } |
| 125 | + return undefined; // no non-transparent ancestor found |
| 126 | +} |
0 commit comments