Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions lib/rules/template-no-invalid-link-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,17 @@ module.exports = {
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 the link contains any non-TextNode child, content is dynamic/opaque — don't flag.
const childList = children || [];
const allTextNodes = childList.every((child) => child.type === 'GlimmerTextNode');
if (!allTextNodes) {
return;
}

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

const normalized = fullText.trim().toLowerCase().replaceAll(/\s+/g, ' ');
Expand Down
18 changes: 5 additions & 13 deletions tests/lib/rules/template-no-invalid-link-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const ruleTester = new RuleTester({

ruleTester.run('template-no-invalid-link-text', rule, {
valid: [
// Link with component child — content is opaque, can't validate.
// Mirrors upstream no-invalid-link-text.js L53-56 ("do not flag when link contains additional dynamic (non-text) children").
{ filename: 'test.gjs', code: '<template><a href="/x"><MyComponent /></a></template>' },
{ filename: 'test.gjs', code: '<template><a href="/x">prefix <MyComponent /></a></template>' },

{ filename: 'test.gjs', code: '<template><a href="/about">About Us</a></template>' },
{
filename: 'test.gjs',
Expand Down Expand Up @@ -149,13 +154,6 @@ ruleTester.run('template-no-invalid-link-text', rule, {
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
// Nested element content
filename: 'test.gjs',
code: '<template><a href="/page"><span>click here</span></a></template>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
// aria-label with disallowed text overrides content check
filename: 'test.gjs',
Expand Down Expand Up @@ -277,12 +275,6 @@ hbsRuleTester.run('template-no-invalid-link-text (hbs)', rule, {
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
// nested element content — text is in a child element
code: '<a href="/page"><span>click here</span></a>',
output: null,
errors: [{ messageId: 'invalidText' }],
},
{
code: '<MyLink>click here</MyLink>',
output: null,
Expand Down
Loading