Skip to content

Commit 3cef552

Browse files
docs(no-navigation-without-resolve): update rule documentation (#1479)
Co-authored-by: Marek Dědič <developer@dedic.eu>
1 parent 98eae61 commit 3cef552

5 files changed

Lines changed: 30 additions & 24 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ These rules relate to SvelteKit and its best Practices.
370370
| Rule ID | Description | |
371371
|:--------|:------------|:---|
372372
| [svelte/no-export-load-in-svelte-module-in-kit-pages](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-export-load-in-svelte-module-in-kit-pages/) | disallow exporting load functions in `*.svelte` module in SvelteKit page components. | :star: |
373-
| [svelte/no-navigation-without-resolve](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-resolve/) | disallow using navigation (links, goto, pushState, replaceState) without a resolve() | :star: |
373+
| [svelte/no-navigation-without-resolve](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-resolve/) | disallow internal navigation (links, `goto()`, `pushState()`, `replaceState()`) without a `resolve()` | :star: |
374374
| [svelte/valid-prop-names-in-kit-pages](https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-prop-names-in-kit-pages/) | disallow props other than data or errors in SvelteKit page components. | :star: |
375375

376376
## Experimental

docs/rules.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ These rules extend the rules provided by ESLint itself, or other plugins to work
118118

119119
These rules relate to SvelteKit and its best Practices.
120120

121-
| Rule ID | Description | |
122-
| :------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------- | :----- |
123-
| [svelte/no-export-load-in-svelte-module-in-kit-pages](./rules/no-export-load-in-svelte-module-in-kit-pages.md) | disallow exporting load functions in `*.svelte` module in SvelteKit page components. | :star: |
124-
| [svelte/no-navigation-without-resolve](./rules/no-navigation-without-resolve.md) | disallow using navigation (links, goto, pushState, replaceState) without a resolve() | :star: |
125-
| [svelte/valid-prop-names-in-kit-pages](./rules/valid-prop-names-in-kit-pages.md) | disallow props other than data or errors in SvelteKit page components. | :star: |
121+
| Rule ID | Description | |
122+
| :------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------- | :----- |
123+
| [svelte/no-export-load-in-svelte-module-in-kit-pages](./rules/no-export-load-in-svelte-module-in-kit-pages.md) | disallow exporting load functions in `*.svelte` module in SvelteKit page components. | :star: |
124+
| [svelte/no-navigation-without-resolve](./rules/no-navigation-without-resolve.md) | disallow internal navigation (links, `goto()`, `pushState()`, `replaceState()`) without a `resolve()` | :star: |
125+
| [svelte/valid-prop-names-in-kit-pages](./rules/valid-prop-names-in-kit-pages.md) | disallow props other than data or errors in SvelteKit page components. | :star: |
126126

127127
## Experimental
128128

docs/rules/no-navigation-without-resolve.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,53 @@
22
pageClass: 'rule-details'
33
sidebarDepth: 0
44
title: 'svelte/no-navigation-without-resolve'
5-
description: 'disallow using navigation (links, goto, pushState, replaceState) without a resolve()'
5+
description: 'disallow internal navigation (links, `goto()`, `pushState()`, `replaceState()`) without a `resolve()`'
66
since: 'v3.12.0'
77
---
88

99
# svelte/no-navigation-without-resolve
1010

11-
> disallow using navigation (links, goto, pushState, replaceState) without a resolve()
11+
> disallow internal navigation (links, `goto()`, `pushState()`, `replaceState()`) without a `resolve()`
1212
1313
- :gear: This rule is included in `"plugin:svelte/recommended"`.
1414

1515
## :book: Rule Details
1616

17-
This rule reports navigation using HTML `<a>` tags, SvelteKit's `goto()`, `pushState()` and `replaceState()` functions without resolving a relative URL. All four of these may be used for navigation, with `goto()`, `pushState()` and `replaceState()` being intended solely for internal navigation (i.e. not leaving the site), while `<a>` tags may be used for both internal and external navigation. When using any way of internal navigation, the URL must be resolved using SvelteKit's `resolve()`, otherwise the site may break. For programmatic navigation to external URLs, using `window.location` is advised.
18-
19-
This rule checks all 4 navigation options for the presence of the `resolve()` function call, with an exception for `<a>` links to absolute URLs (and fragment URLs), which are assumed to be used for external navigation and so do not require the `resolve()` function, and for shallow routing functions with an empty string as the path, which keeps the current URL.
17+
This rule ensures internal navigation via HTML `<a>` tags, SvelteKit's `goto()`, `pushState()` and `replaceState()` uses `resolve()`. `<a>` tags will skip this check when it has an absolute URL or `rel="external"`. For programmatic external navigation, use `window.location`. Enforcing this rule ensures the base path is prefixed and internal links are type-checked.
2018

2119
<!--eslint-skip-->
2220

2321
```svelte
22+
<!-- ✓ GOOD -->
2423
<script>
2524
/* eslint svelte/no-navigation-without-resolve: "error" */
2625
2726
import { goto, pushState, replaceState } from '$app/navigation';
2827
import { resolve } from '$app/paths';
2928
30-
// ✓ GOOD
3129
goto(resolve('/foo/'));
32-
3330
pushState(resolve('/foo/'), {});
34-
pushState('', {});
35-
3631
replaceState(resolve('/foo/'), {});
32+
33+
// shallow routing
34+
pushState('', {});
3735
replaceState('', {});
36+
</script>
37+
38+
<a href={resolve('/foo/')}>Click me!</a>
39+
<a href="https://svelte.dev">Click me!</a>
40+
<a href={someURL} rel="external">Click me!</a>
41+
<a href="#top">Click me!</a>
42+
```
43+
44+
```svelte
45+
<!-- ✗ BAD -->
46+
<script>
47+
/* eslint svelte/no-navigation-without-resolve: "error" */
48+
49+
import { goto, pushState, replaceState } from '$app/navigation';
50+
import { resolve } from '$app/paths';
3851
39-
// ✗ BAD
4052
goto('/foo');
4153
goto('/foo' + resolve('/bar'));
4254
goto(resolve('/foo') + '/bar');
@@ -45,12 +57,6 @@ This rule checks all 4 navigation options for the presence of the `resolve()` fu
4557
replaceState('/foo', {});
4658
</script>
4759
48-
<!-- ✓ GOOD -->
49-
<a href={resolve('/foo/')}>Click me!</a>
50-
<a href="https://svelte.dev">Click me!</a>
51-
<a href={someURL} rel="external">Click me!</a>
52-
53-
<!-- ✗ BAD -->
5460
<a href="/foo">Click me!</a>
5561
<a href={'/foo'}>Click me!</a>
5662
```

packages/eslint-plugin-svelte/src/rule-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export interface RuleOptions {
199199
*/
200200
'svelte/no-navigation-without-base'?: Linter.RuleEntry<SvelteNoNavigationWithoutBase>
201201
/**
202-
* disallow using navigation (links, goto, pushState, replaceState) without a resolve()
202+
* disallow internal navigation (links, `goto()`, `pushState()`, `replaceState()`) without a `resolve()`
203203
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-resolve/
204204
*/
205205
'svelte/no-navigation-without-resolve'?: Linter.RuleEntry<SvelteNoNavigationWithoutResolve>

packages/eslint-plugin-svelte/src/rules/no-navigation-without-resolve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default createRule('no-navigation-without-resolve', {
1010
meta: {
1111
docs: {
1212
description:
13-
'disallow using navigation (links, goto, pushState, replaceState) without a resolve()',
13+
'disallow internal navigation (links, `goto()`, `pushState()`, `replaceState()`) without a `resolve()`',
1414
category: 'SvelteKit',
1515
recommended: true
1616
},

0 commit comments

Comments
 (0)