|
| 1 | +const { roles, elementRoles } = require('aria-query'); |
| 2 | + |
| 3 | +/** |
| 4 | + * Get the implicit ARIA role for an HTML element based on its tag name and type attribute. |
| 5 | + * Uses the aria-query elementRoles mapping. |
| 6 | + */ |
| 7 | +function getImplicitRole(tagName, typeAttribute) { |
| 8 | + // For input elements, match against entries with the specific type attribute |
| 9 | + if (tagName === 'input') { |
| 10 | + for (const key of elementRoles.keys()) { |
| 11 | + if (key.name === tagName && key.attributes) { |
| 12 | + for (const attribute of key.attributes) { |
| 13 | + if (attribute.name === 'type' && attribute.value === typeAttribute) { |
| 14 | + return elementRoles.get(key)[0]; |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | + // For all elements, fall back to the first matching entry by tag name |
| 21 | + for (const key of elementRoles.keys()) { |
| 22 | + if (key.name === tagName) { |
| 23 | + return elementRoles.get(key)[0]; |
| 24 | + } |
| 25 | + } |
| 26 | + return null; |
| 27 | +} |
| 28 | + |
| 29 | +function getExplicitRole(node) { |
| 30 | + const roleAttr = node.attributes?.find((attr) => attr.name === 'role'); |
| 31 | + if (roleAttr && roleAttr.value?.type === 'GlimmerTextNode') { |
| 32 | + return roleAttr.value.chars.trim(); |
| 33 | + } |
| 34 | + return null; |
| 35 | +} |
| 36 | + |
| 37 | +function getTypeAttribute(node) { |
| 38 | + const typeAttr = node.attributes?.find((attr) => attr.name === 'type'); |
| 39 | + if (typeAttr && typeAttr.value?.type === 'GlimmerTextNode') { |
| 40 | + return typeAttr.value.chars.trim(); |
| 41 | + } |
| 42 | + return null; |
| 43 | +} |
| 44 | + |
| 45 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 46 | +module.exports = { |
| 47 | + meta: { |
| 48 | + type: 'problem', |
| 49 | + docs: { |
| 50 | + description: 'disallow ARIA attributes that are not supported by the element role', |
| 51 | + category: 'Accessibility', |
| 52 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-unsupported-role-attributes.md', |
| 53 | + templateMode: 'both', |
| 54 | + }, |
| 55 | + fixable: null, |
| 56 | + schema: [], |
| 57 | + messages: { |
| 58 | + unsupported: |
| 59 | + 'ARIA attribute "{{attribute}}" is not supported for role "{{role}}". Remove the attribute or change the role.', |
| 60 | + }, |
| 61 | + originallyFrom: { |
| 62 | + name: 'ember-template-lint', |
| 63 | + rule: 'lib/rules/no-unsupported-role-attributes.js', |
| 64 | + docs: 'docs/rule/no-unsupported-role-attributes.md', |
| 65 | + tests: 'test/unit/rules/no-unsupported-role-attributes-test.js', |
| 66 | + }, |
| 67 | + }, |
| 68 | + |
| 69 | + create(context) { |
| 70 | + return { |
| 71 | + GlimmerElementNode(node) { |
| 72 | + // Determine the role: explicit first, then implicit |
| 73 | + let role = getExplicitRole(node); |
| 74 | + if (!role) { |
| 75 | + const tagName = node.tag; |
| 76 | + const typeAttribute = getTypeAttribute(node); |
| 77 | + role = getImplicitRole(tagName, typeAttribute); |
| 78 | + } |
| 79 | + |
| 80 | + if (!role) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + const roleDefinition = roles.get(role); |
| 85 | + if (!roleDefinition) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const supportedProps = Object.keys(roleDefinition.props); |
| 90 | + const ariaAttributes = node.attributes?.filter( |
| 91 | + (attr) => attr.type === 'GlimmerAttrNode' && attr.name && attr.name.startsWith('aria-') |
| 92 | + ); |
| 93 | + |
| 94 | + for (const attr of ariaAttributes || []) { |
| 95 | + if (!supportedProps.includes(attr.name)) { |
| 96 | + context.report({ |
| 97 | + node: attr, |
| 98 | + messageId: 'unsupported', |
| 99 | + data: { |
| 100 | + attribute: attr.name, |
| 101 | + role, |
| 102 | + }, |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | + }, |
| 107 | + |
| 108 | + GlimmerMustacheStatement(node) { |
| 109 | + if (!node.hash || !node.hash.pairs) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + const rolePair = node.hash.pairs.find((pair) => pair.key === 'role'); |
| 114 | + if (!rolePair || rolePair.value?.type !== 'GlimmerStringLiteral') { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + const role = rolePair.value.original; |
| 119 | + if (!role) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + const roleDefinition = roles.get(role); |
| 124 | + if (!roleDefinition) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + const supportedProps = Object.keys(roleDefinition.props); |
| 129 | + const ariaPairs = node.hash.pairs.filter((pair) => pair.key.startsWith('aria-')); |
| 130 | + |
| 131 | + for (const pair of ariaPairs) { |
| 132 | + if (!supportedProps.includes(pair.key)) { |
| 133 | + context.report({ |
| 134 | + node: pair, |
| 135 | + messageId: 'unsupported', |
| 136 | + data: { |
| 137 | + attribute: pair.key, |
| 138 | + role, |
| 139 | + }, |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
| 143 | + }, |
| 144 | + }; |
| 145 | + }, |
| 146 | +}; |
0 commit comments