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