Skip to content

Commit 26de552

Browse files
Merge pull request #2680 from johanrd/false_positive/template-no-invalid-link-text
Post-merge-review: Fix template-no-invalid-link-text: skip when link contains non-text children
2 parents e33990f + c56aadd commit 26de552

2 files changed

Lines changed: 14 additions & 24 deletions

File tree

lib/rules/template-no-invalid-link-text.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,17 @@ module.exports = {
145145
return;
146146
}
147147

148-
// Check text content
149-
let fullText = '';
150-
let hasDynamic = false;
151-
for (const child of children || []) {
152-
const result = getTextContentResult(child);
153-
fullText += result.text;
154-
if (result.hasDynamic) {
155-
hasDynamic = true;
156-
}
148+
// If the link contains any non-TextNode child, content is dynamic/opaque — don't flag.
149+
const childList = children || [];
150+
const allTextNodes = childList.every((child) => child.type === 'GlimmerTextNode');
151+
if (!allTextNodes) {
152+
return;
157153
}
158154

159-
if (hasDynamic) {
160-
return; // can't validate dynamic content
155+
// Concatenate text content (only TextNode children at this point).
156+
let fullText = '';
157+
for (const child of childList) {
158+
fullText += child.chars.replaceAll(' ', ' ');
161159
}
162160

163161
const normalized = fullText.trim().toLowerCase().replaceAll(/\s+/g, ' ');

tests/lib/rules/template-no-invalid-link-text.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const ruleTester = new RuleTester({
88

99
ruleTester.run('template-no-invalid-link-text', rule, {
1010
valid: [
11+
// Link with component child — content is opaque, can't validate.
12+
// Mirrors upstream no-invalid-link-text.js L53-56 ("do not flag when link contains additional dynamic (non-text) children").
13+
{ filename: 'test.gjs', code: '<template><a href="/x"><MyComponent /></a></template>' },
14+
{ filename: 'test.gjs', code: '<template><a href="/x">prefix <MyComponent /></a></template>' },
15+
1116
{ filename: 'test.gjs', code: '<template><a href="/about">About Us</a></template>' },
1217
{
1318
filename: 'test.gjs',
@@ -149,13 +154,6 @@ ruleTester.run('template-no-invalid-link-text', rule, {
149154
output: null,
150155
errors: [{ messageId: 'invalidText' }],
151156
},
152-
{
153-
// Nested element content
154-
filename: 'test.gjs',
155-
code: '<template><a href="/page"><span>click here</span></a></template>',
156-
output: null,
157-
errors: [{ messageId: 'invalidText' }],
158-
},
159157
{
160158
// aria-label with disallowed text overrides content check
161159
filename: 'test.gjs',
@@ -277,12 +275,6 @@ hbsRuleTester.run('template-no-invalid-link-text (hbs)', rule, {
277275
output: null,
278276
errors: [{ messageId: 'invalidText' }],
279277
},
280-
{
281-
// nested element content — text is in a child element
282-
code: '<a href="/page"><span>click here</span></a>',
283-
output: null,
284-
errors: [{ messageId: 'invalidText' }],
285-
},
286278
{
287279
code: '<MyLink>click here</MyLink>',
288280
output: null,

0 commit comments

Comments
 (0)