|
| 1 | +const DEFAULT_CONFIG = { |
| 2 | + checkAllHTMLElements: true, |
| 3 | +}; |
| 4 | + |
| 5 | +function parseConfig(config) { |
| 6 | + if (config === true) { |
| 7 | + return DEFAULT_CONFIG; |
| 8 | + } |
| 9 | + return { ...DEFAULT_CONFIG, ...config }; |
| 10 | +} |
| 11 | + |
| 12 | +function createErrorMessageLandmarkElement(element, role) { |
| 13 | + return `Use of redundant or invalid role: ${role} on <${element}> detected. If a landmark element is used, any role provided will either be redundant or incorrect.`; |
| 14 | +} |
| 15 | + |
| 16 | +function createErrorMessageAnyElement(element, role) { |
| 17 | + return `Use of redundant or invalid role: ${role} on <${element}> detected.`; |
| 18 | +} |
| 19 | + |
| 20 | +// https://www.w3.org/TR/html-aria/#docconformance |
| 21 | +const LANDMARK_ROLES = new Set([ |
| 22 | + 'banner', |
| 23 | + 'main', |
| 24 | + 'complementary', |
| 25 | + 'search', |
| 26 | + 'form', |
| 27 | + 'navigation', |
| 28 | + 'contentinfo', |
| 29 | +]); |
| 30 | + |
| 31 | +const ALLOWED_ELEMENT_ROLES = [ |
| 32 | + { name: 'nav', role: 'navigation' }, |
| 33 | + { name: 'form', role: 'search' }, |
| 34 | + { name: 'ol', role: 'list' }, |
| 35 | + { name: 'ul', role: 'list' }, |
| 36 | + { name: 'a', role: 'link' }, |
| 37 | + { name: 'input', role: 'combobox' }, |
| 38 | +]; |
| 39 | + |
| 40 | +// Mapping of roles to their corresponding HTML elements |
| 41 | +// From https://www.w3.org/TR/html-aria/ |
| 42 | +const ROLE_TO_ELEMENTS = { |
| 43 | + article: ['article'], |
| 44 | + banner: ['header'], |
| 45 | + button: ['button'], |
| 46 | + cell: ['td'], |
| 47 | + checkbox: ['input'], |
| 48 | + columnheader: ['th'], |
| 49 | + complementary: ['aside'], |
| 50 | + contentinfo: ['footer'], |
| 51 | + definition: ['dd'], |
| 52 | + dialog: ['dialog'], |
| 53 | + document: ['body'], |
| 54 | + figure: ['figure'], |
| 55 | + form: ['form'], |
| 56 | + grid: ['table'], |
| 57 | + gridcell: ['td'], |
| 58 | + group: ['details', 'fieldset', 'optgroup'], |
| 59 | + heading: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'], |
| 60 | + img: ['img'], |
| 61 | + link: ['a'], |
| 62 | + list: ['ol', 'ul'], |
| 63 | + listbox: ['select'], |
| 64 | + listitem: ['li'], |
| 65 | + main: ['main'], |
| 66 | + navigation: ['nav'], |
| 67 | + option: ['option'], |
| 68 | + radio: ['input'], |
| 69 | + region: ['section'], |
| 70 | + row: ['tr'], |
| 71 | + rowgroup: ['tbody', 'tfoot', 'thead'], |
| 72 | + rowheader: ['th'], |
| 73 | + search: ['search'], |
| 74 | + searchbox: ['input'], |
| 75 | + separator: ['hr'], |
| 76 | + slider: ['input'], |
| 77 | + spinbutton: ['input'], |
| 78 | + status: ['output'], |
| 79 | + table: ['table'], |
| 80 | + term: ['dfn', 'dt'], |
| 81 | + textbox: ['input', 'textarea'], |
| 82 | +}; |
| 83 | + |
| 84 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 85 | +module.exports = { |
| 86 | + meta: { |
| 87 | + type: 'suggestion', |
| 88 | + docs: { |
| 89 | + description: 'disallow redundant role attributes', |
| 90 | + category: 'Accessibility', |
| 91 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-redundant-role.md', |
| 92 | + templateMode: 'both', |
| 93 | + }, |
| 94 | + fixable: 'code', |
| 95 | + schema: [ |
| 96 | + { |
| 97 | + type: 'object', |
| 98 | + properties: { |
| 99 | + checkAllHTMLElements: { |
| 100 | + type: 'boolean', |
| 101 | + }, |
| 102 | + }, |
| 103 | + additionalProperties: false, |
| 104 | + }, |
| 105 | + ], |
| 106 | + messages: {}, |
| 107 | + originallyFrom: { |
| 108 | + name: 'ember-template-lint', |
| 109 | + rule: 'lib/rules/no-redundant-role.js', |
| 110 | + docs: 'docs/rule/no-redundant-role.md', |
| 111 | + tests: 'test/unit/rules/no-redundant-role-test.js', |
| 112 | + }, |
| 113 | + }, |
| 114 | + |
| 115 | + create(context) { |
| 116 | + const config = parseConfig(context.options[0]); |
| 117 | + |
| 118 | + return { |
| 119 | + GlimmerElementNode(node) { |
| 120 | + const roleAttr = node.attributes?.find((attr) => attr.name === 'role'); |
| 121 | + |
| 122 | + if (!roleAttr) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + let roleValue; |
| 127 | + if (roleAttr.value && roleAttr.value.type === 'GlimmerTextNode') { |
| 128 | + roleValue = roleAttr.value.chars || ''; |
| 129 | + } else { |
| 130 | + // Skip dynamic role values |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + const isLandmarkRole = LANDMARK_ROLES.has(roleValue); |
| 135 | + if (!config.checkAllHTMLElements && !isLandmarkRole) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + const elementsWithRole = ROLE_TO_ELEMENTS[roleValue]; |
| 140 | + if (!elementsWithRole) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + const isRedundant = |
| 145 | + elementsWithRole.includes(node.tag) && |
| 146 | + !ALLOWED_ELEMENT_ROLES.some((e) => e.name === node.tag && e.role === roleValue); |
| 147 | + |
| 148 | + if (isRedundant) { |
| 149 | + const errorMessage = isLandmarkRole |
| 150 | + ? createErrorMessageLandmarkElement(node.tag, roleValue) |
| 151 | + : createErrorMessageAnyElement(node.tag, roleValue); |
| 152 | + |
| 153 | + context.report({ |
| 154 | + node, |
| 155 | + message: errorMessage, |
| 156 | + fix(fixer) { |
| 157 | + const sourceCode = context.getSourceCode(); |
| 158 | + const elementText = sourceCode.getText(node); |
| 159 | + const roleAttrText = sourceCode.getText(roleAttr); |
| 160 | + |
| 161 | + // Find the role attribute in the element text and remove it along with preceding space |
| 162 | + const roleAttrPattern = new RegExp( |
| 163 | + `\\s+${roleAttrText.replaceAll(/[$()*+.?[\\\]^{|}]/g, '\\$&')}` |
| 164 | + ); |
| 165 | + const fixedText = elementText.replace(roleAttrPattern, ''); |
| 166 | + |
| 167 | + return fixer.replaceText(node, fixedText); |
| 168 | + }, |
| 169 | + }); |
| 170 | + } |
| 171 | + }, |
| 172 | + }; |
| 173 | + }, |
| 174 | +}; |
0 commit comments