Skip to content

fix(no-navigation-without-base): ignoring fragment links #1107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
5 changes: 5 additions & 0 deletions .changeset/warm-plums-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

fix(no-navigation-without-base): ignoring fragment links
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,15 @@ export default createRule('no-navigation-without-base', {
}
const hrefValue = node.value[0];
if (hrefValue.type === 'SvelteLiteral') {
if (!expressionIsAbsolute(hrefValue)) {
if (!expressionIsAbsolute(hrefValue) && !expressionIsFragment(hrefValue)) {
context.report({ loc: hrefValue.loc, messageId: 'linkNotPrefixed' });
}
return;
}
if (
!expressionStartsWithBase(context, hrefValue.expression, basePathNames) &&
!expressionIsAbsolute(hrefValue.expression)
!expressionIsAbsolute(hrefValue.expression) &&
!expressionIsFragment(hrefValue.expression)
) {
context.report({ loc: hrefValue.loc, messageId: 'linkNotPrefixed' });
}
Expand Down Expand Up @@ -348,3 +349,33 @@ function templateLiteralIsAbsolute(url: TSESTree.TemplateLiteral): boolean {
function urlValueIsAbsolute(url: string): boolean {
return url.includes('://');
}

function expressionIsFragment(url: SvelteLiteral | TSESTree.Expression): boolean {
switch (url.type) {
case 'BinaryExpression':
return binaryExpressionIsFragment(url);
case 'Literal':
return typeof url.value === 'string' && urlValueIsFragment(url.value);
case 'SvelteLiteral':
return urlValueIsFragment(url.value);
case 'TemplateLiteral':
return templateLiteralIsFragment(url);
default:
return false;
}
}

function binaryExpressionIsFragment(url: TSESTree.BinaryExpression): boolean {
return url.left.type !== 'PrivateIdentifier' && expressionIsFragment(url.left);
}

function templateLiteralIsFragment(url: TSESTree.TemplateLiteral): boolean {
return (
(url.expressions.length >= 1 && expressionIsFragment(url.expressions[0])) ||
(url.quasis.length >= 1 && urlValueIsFragment(url.quasis[0].value.raw))
);
}

function urlValueIsFragment(url: string): boolean {
return url.startsWith('#');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- message: Found a link with a url that isn't prefixed with the base path.
line: 4
column: 10
suggestions: null
- message: Found a link with a url that isn't prefixed with the base path.
line: 5
column: 9
suggestions: null
- message: Found a link with a url that isn't prefixed with the base path.
line: 6
column: 9
suggestions: null
- message: Found a link with a url that isn't prefixed with the base path.
line: 7
column: 9
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
const value = "/foo#section";
</script>
<a href="/foo#section">Click me!</a>
<a href={'/foo#section'}>Click me!</a>
<a href={'/' + 'foo#section'}>Click me!</a>
<a href={value}>Click me!</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
const section = 'sectionName';
</script>

<a href="#">Click me!</a>
<a href="#section">Click me!</a>
<a href={'#section'}>Click me!</a>
<a href={'#' + 'section'}>Click me!</a>
<a href={'#' + section}>Click me!</a>
<a href={`#${section}`}>Click me!</a>