Skip to content
Open
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/icy-planets-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': minor
---

feat(no-navigation-without-resolve): checking link shorthand attributes
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ export default createRule('no-navigation-without-resolve', {
}
}
},
SvelteShorthandAttribute(node) {
if (
context.options[0]?.ignoreLinks === true ||
node.parent.parent.type !== 'SvelteElement' ||
node.parent.parent.kind !== 'html' ||
node.parent.parent.name.type !== 'SvelteName' ||
node.parent.parent.name.name !== 'a' ||
node.key.name !== 'href' ||
node.value.type !== 'Identifier'
) {
return;
}
if (
!expressionIsAbsolute(new FindVariableContext(context), node.value) &&
!expressionIsFragment(new FindVariableContext(context), node.value) &&
!isResolveCall(new FindVariableContext(context), node.value, resolveReferences)
) {
context.report({ loc: node.loc, messageId: 'linkWithoutResolve' });
}
},
SvelteAttribute(node) {
if (
context.options[0]?.ignoreLinks === true ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
- message: Found a link with a url that isn't resolved.
line: 7
line: 8
column: 9
suggestions: null
- message: Found a link with a url that isn't resolved.
line: 8
line: 9
column: 9
suggestions: null
- message: Found a link with a url that isn't resolved.
line: 9
line: 10
column: 9
suggestions: null
- message: Found a link with a url that isn't resolved.
line: 11
column: 4
suggestions: null
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import { resolve } from '$app/paths';

const value = resolve('/foo') + '/bar';
const href = resolve('/foo') + '/bar';
</script>

<a href={resolve('/foo') + '/bar'}>Click me!</a>
<a href={'/foo' + resolve('/bar')}>Click me!</a>
<a href={value}>Click me!</a>
<a {href}>Click me!</a>
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
- message: Found a link with a url that isn't resolved.
line: 5
column: 10
suggestions: null
- message: Found a link with a url that isn't resolved.
line: 6
column: 9
column: 10
suggestions: null
- message: Found a link with a url that isn't resolved.
line: 7
Expand All @@ -18,3 +14,11 @@
line: 9
column: 9
suggestions: null
- message: Found a link with a url that isn't resolved.
line: 10
column: 4
suggestions: null
- message: Found a link with a url that isn't resolved.
line: 11
column: 9
suggestions: null
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script>
const value = "/foo#section";
const href = "/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>
<a {href}>Click me!</a>
<a href={'/foo#section:42'}>Click me!</a>
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
- message: Found a link with a url that isn't resolved.
line: 5
column: 10
suggestions: null
- message: Found a link with a url that isn't resolved.
line: 6
column: 9
column: 10
suggestions: null
- message: Found a link with a url that isn't resolved.
line: 7
Expand All @@ -18,3 +14,11 @@
line: 9
column: 9
suggestions: null
- message: Found a link with a url that isn't resolved.
line: 10
column: 4
suggestions: null
- message: Found a link with a url that isn't resolved.
line: 11
column: 9
suggestions: null
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script>
const value = "/foo";
const href = "/foo";
</script>

<a href="/foo">Click me!</a>
<a href={'/foo'}>Click me!</a>
<a href={'/' + 'foo'}>Click me!</a>
<a href={value}>Click me!</a>
<a {href}>Click me!</a>
<a href={'/user:42'}>Click me!</a>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const protocol = 'https';

const value = "https://svelte.dev";
const href = "https://svelte.dev";
</script>

<a href="http://svelte.dev">Click me!</a>
Expand All @@ -16,3 +17,4 @@
<a href="mailto:[email protected]">Click me!</a>
<a href="tel:+123456789">Click me!</a>
<a href={value}>Click me!</a>
<a {href}>Click me!</a>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const section = 'sectionName';

const value = '#section';
const href = '#section';
</script>

<a href="#">Click me!</a>
Expand All @@ -12,3 +13,4 @@
<a href={`#${section}`}>Click me!</a>
<a href={'#user:42'}>Click me!</a>
<a href={value}>Click me!</a>
<a {href}>Click me!</a>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import { resolve } from '$app/paths';

const value = resolve('/foo/');
const href = resolve('/foo/');
</script>

<a href={resolve('/foo/')}>Click me!</a>
<a href={value}>Click me!</a>
<a {href}>Click me!</a>
Loading