|
| 1 | +const DISALLOWED_LINK_TEXTS = new Set(['click here', 'more info', 'read more', 'more']); |
| 2 | + |
| 3 | +function getTextContentResult(node) { |
| 4 | + if (node.type === 'GlimmerTextNode') { |
| 5 | + return { text: node.chars.replaceAll(' ', ' '), hasDynamic: false }; |
| 6 | + } |
| 7 | + if (node.type === 'GlimmerMustacheStatement' || node.type === 'GlimmerSubExpression') { |
| 8 | + return { text: '', hasDynamic: true }; |
| 9 | + } |
| 10 | + if (node.type === 'GlimmerElementNode' && node.children) { |
| 11 | + let text = ''; |
| 12 | + let hasDynamic = false; |
| 13 | + for (const child of node.children) { |
| 14 | + const result = getTextContentResult(child); |
| 15 | + text += result.text; |
| 16 | + if (result.hasDynamic) { |
| 17 | + hasDynamic = true; |
| 18 | + } |
| 19 | + } |
| 20 | + return { text, hasDynamic }; |
| 21 | + } |
| 22 | + return { text: '', hasDynamic: false }; |
| 23 | +} |
| 24 | + |
| 25 | +function isDynamicValue(value) { |
| 26 | + return value?.type === 'GlimmerMustacheStatement' || value?.type === 'GlimmerConcatStatement'; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Checks aria-labelledby and aria-label attributes. |
| 31 | + * Returns: |
| 32 | + * { skip: true } — has valid accessible name, skip element |
| 33 | + * { report: true, text: string } — aria-label is itself a disallowed text, report it |
| 34 | + * { skip: false } — no valid aria override, check text content |
| 35 | + */ |
| 36 | +function checkAriaAttributes(attrs) { |
| 37 | + const ariaLabelledby = attrs.find((a) => a.name === 'aria-labelledby'); |
| 38 | + if (ariaLabelledby) { |
| 39 | + if (isDynamicValue(ariaLabelledby.value)) { |
| 40 | + return { skip: true }; |
| 41 | + } |
| 42 | + if (ariaLabelledby.value?.type === 'GlimmerTextNode') { |
| 43 | + if (ariaLabelledby.value.chars.trim().length > 0) { |
| 44 | + return { skip: true }; // valid non-empty labelledby |
| 45 | + } |
| 46 | + } |
| 47 | + // empty aria-labelledby → fall through |
| 48 | + return { skip: false }; |
| 49 | + } |
| 50 | + |
| 51 | + const ariaLabel = attrs.find((a) => a.name === 'aria-label'); |
| 52 | + if (ariaLabel) { |
| 53 | + if (isDynamicValue(ariaLabel.value)) { |
| 54 | + return { skip: true }; |
| 55 | + } |
| 56 | + if (ariaLabel.value?.type === 'GlimmerTextNode') { |
| 57 | + const val = ariaLabel.value.chars.replaceAll(' ', ' ').toLowerCase().trim(); |
| 58 | + if (val.length > 0 && !DISALLOWED_LINK_TEXTS.has(val)) { |
| 59 | + return { skip: true }; // valid aria-label |
| 60 | + } |
| 61 | + if (val.length > 0) { |
| 62 | + return { skip: true, report: true, text: val }; // aria-label itself is disallowed |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return { skip: false }; |
| 68 | +} |
| 69 | + |
| 70 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 71 | +module.exports = { |
| 72 | + meta: { |
| 73 | + type: 'problem', |
| 74 | + docs: { |
| 75 | + description: 'disallow invalid or uninformative link text content', |
| 76 | + category: 'Accessibility', |
| 77 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-invalid-link-text.md', |
| 78 | + templateMode: 'both', |
| 79 | + }, |
| 80 | + fixable: null, |
| 81 | + schema: [ |
| 82 | + { |
| 83 | + type: 'object', |
| 84 | + properties: { |
| 85 | + allowEmptyLinks: { type: 'boolean' }, |
| 86 | + linkComponents: { type: 'array', items: { type: 'string' } }, |
| 87 | + }, |
| 88 | + additionalProperties: false, |
| 89 | + }, |
| 90 | + ], |
| 91 | + messages: { |
| 92 | + invalidText: |
| 93 | + 'Link text "{{text}}" is not descriptive. Use meaningful text that describes the link destination.', |
| 94 | + }, |
| 95 | + originallyFrom: { |
| 96 | + name: 'ember-template-lint', |
| 97 | + rule: 'lib/rules/no-invalid-link-text.js', |
| 98 | + docs: 'docs/rule/no-invalid-link-text.md', |
| 99 | + tests: 'test/unit/rules/no-invalid-link-text-test.js', |
| 100 | + }, |
| 101 | + }, |
| 102 | + |
| 103 | + create(context) { |
| 104 | + const options = context.options[0] || {}; |
| 105 | + const allowEmptyLinks = options.allowEmptyLinks || false; |
| 106 | + const customLinkComponents = options.linkComponents || []; |
| 107 | + |
| 108 | + const filename = context.filename ?? context.getFilename(); |
| 109 | + const isStrictMode = filename.endsWith('.gjs') || filename.endsWith('.gts'); |
| 110 | + |
| 111 | + // In HBS, LinkTo always refers to Ember's router link component. |
| 112 | + // In GJS/GTS, LinkTo must be explicitly imported from '@ember/routing'. |
| 113 | + // local alias → true (any truthy value marks it as a tracked link component) |
| 114 | + const importedLinkComponents = new Map(); |
| 115 | + |
| 116 | + const linkTags = new Set(['a', ...customLinkComponents]); |
| 117 | + if (!isStrictMode) { |
| 118 | + linkTags.add('LinkTo'); |
| 119 | + } |
| 120 | + |
| 121 | + function checkLinkContent(node, children) { |
| 122 | + const attrs = node.attributes || []; |
| 123 | + |
| 124 | + // Skip if aria-hidden="true" |
| 125 | + const ariaHidden = attrs.find((a) => a.name === 'aria-hidden'); |
| 126 | + if (ariaHidden?.value?.type === 'GlimmerTextNode' && ariaHidden.value.chars === 'true') { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + // Skip if hidden attribute present |
| 131 | + if (attrs.some((a) => a.name === 'hidden')) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + const ariaResult = checkAriaAttributes(attrs); |
| 136 | + if (ariaResult.report) { |
| 137 | + context.report({ node, messageId: 'invalidText', data: { text: ariaResult.text } }); |
| 138 | + return; |
| 139 | + } |
| 140 | + if (ariaResult.skip) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + // Check text content |
| 145 | + let fullText = ''; |
| 146 | + let hasDynamic = false; |
| 147 | + for (const child of children || []) { |
| 148 | + const result = getTextContentResult(child); |
| 149 | + fullText += result.text; |
| 150 | + if (result.hasDynamic) { |
| 151 | + hasDynamic = true; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (hasDynamic) { |
| 156 | + return; // can't validate dynamic content |
| 157 | + } |
| 158 | + |
| 159 | + const normalized = fullText.trim().toLowerCase().replaceAll(/\s+/g, ' '); |
| 160 | + |
| 161 | + if (!normalized.replaceAll(' ', '')) { |
| 162 | + if (!allowEmptyLinks) { |
| 163 | + context.report({ node, messageId: 'invalidText', data: { text: '(empty)' } }); |
| 164 | + } |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + if (DISALLOWED_LINK_TEXTS.has(normalized)) { |
| 169 | + context.report({ node, messageId: 'invalidText', data: { text: normalized } }); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return { |
| 174 | + ImportDeclaration(node) { |
| 175 | + if (node.source.value === '@ember/routing') { |
| 176 | + for (const specifier of node.specifiers) { |
| 177 | + if (specifier.type === 'ImportSpecifier' && specifier.imported.name === 'LinkTo') { |
| 178 | + importedLinkComponents.set(specifier.local.name, true); |
| 179 | + linkTags.add(specifier.local.name); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + }, |
| 184 | + |
| 185 | + GlimmerElementNode(node) { |
| 186 | + if (!linkTags.has(node.tag)) { |
| 187 | + return; |
| 188 | + } |
| 189 | + checkLinkContent(node, node.children); |
| 190 | + }, |
| 191 | + |
| 192 | + GlimmerBlockStatement(node) { |
| 193 | + if (node.path?.type === 'GlimmerPathExpression' && node.path.original === 'link-to') { |
| 194 | + checkLinkContent(node, node.program?.body); |
| 195 | + } |
| 196 | + }, |
| 197 | + }; |
| 198 | + }, |
| 199 | +}; |
0 commit comments