Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ ruleTester.run('prefer-docusaurus-link', rule, {
code: '<a href="tel:123456789">Call</a>',
options: [{ignoreFullyResolved: true}],
},
{
// eslint-disable-next-line no-template-curly-in-string
code: '<a href={`https://x.com/${"docu" + "saurus"} ${"rex"}`}>Twitter</a>',
options: [{ignoreFullyResolved: true}],
},
],
invalid: [
{
Expand Down Expand Up @@ -78,13 +83,6 @@ ruleTester.run('prefer-docusaurus-link', rule, {
options: [{ignoreFullyResolved: true}],
errors: errorsJSX,
},
{
// TODO we might want to make this test pass
// Can template literals be statically pre-evaluated? (Babel can do it)
// eslint-disable-next-line no-template-curly-in-string
code: '<a href={`https://x.com/${"docu" + "saurus"} ${"rex"}`}>Twitter</a>',
options: [{ignoreFullyResolved: true}],
errors: errorsJSX,
Comment on lines -81 to -87
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have a unit test for when the template literals are dynamic and can't be resolved at static analysis time: we should add one to ensure it keeps failing

},

],
});
18 changes: 9 additions & 9 deletions packages/eslint-plugin/src/rules/no-html-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ export default createRule<Options, MessageIds>({
const container: TSESTree.JSXExpressionContainer = hrefAttr.value;
const {expression} = container;
if (expression.type === 'TemplateLiteral') {
// Simple static string template literals
if (
expression.expressions.length === 0 &&
expression.quasis.length === 1 &&
expression.quasis[0]?.type === 'TemplateElement' &&
isFullyResolvedUrl(String(expression.quasis[0].value.raw))
) {
return;
const firstQuasi = expression.quasis[0];
if (firstQuasi?.type === 'TemplateElement') {
const prefix = String(firstQuasi.value.raw);
if (
isFullyResolvedUrl(prefix) ||
isFullyResolvedUrl(`${prefix}dummy.com`)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand, what is dummy.com?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! You're right, the dummy.com hack was fragile.
I've refactored the rule to use a more robust staticPreEvaluate utility function that recursively resolves TemplateLiteral, Literal, and BinaryExpression (string concatenation) nodes at static analysis time. This allows us to correctly verify if a generated string is a fully-resolved external URL without relying on string prefixes.
I've also added several new test cases:
Statically evaluatable template literals (e.g., https://x.com/${"docu" + "saurus"}) are now correctly allowed.
String concatenation (e.g., "https://x.com/" + "docusaurus") is also supported.
Dynamic template literals (e.g., https://github.com/${repo}) are now explicitly tested to ensure they are still reported as invalid, since their final URL cannot be statically verified.
Let me know if this approach aligns better with what you had in mind :)

) {
return;
}
}
// TODO add more complex TemplateLiteral cases here
}
}
}
Expand Down