Skip to content

Commit 2a03e88

Browse files
committed
test(no-navigation-without-base): added test for pushState, replaceState and links
1 parent 394acc5 commit 2a03e88

38 files changed

+175
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ These rules relate to SvelteKit and its best Practices.
409409

410410
| Rule ID | Description | |
411411
|:--------|:------------|:---|
412-
| [svelte/no-navigation-without-base](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-base/) | disallow using goto() without the base path | |
412+
| [svelte/no-navigation-without-base](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-base/) | disallow using navigation (links, goto, pushState, replaceState) without the base path | |
413413

414414
## Experimental
415415

docs/rules.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ These rules extend the rules provided by ESLint itself, or other plugins to work
109109

110110
These rules relate to SvelteKit and its best Practices.
111111

112-
| Rule ID | Description | |
113-
| :------------------------------------------------------------------------- | :------------------------------------------ | :-- |
114-
| [svelte/no-navigation-without-base](./rules/no-navigation-without-base.md) | disallow using goto() without the base path | |
112+
| Rule ID | Description | |
113+
| :------------------------------------------------------------------------- | :------------------------------------------------------------------------------------- | :-- |
114+
| [svelte/no-navigation-without-base](./rules/no-navigation-without-base.md) | disallow using navigation (links, goto, pushState, replaceState) without the base path | |
115115

116116
## Experimental
117117

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
pageClass: 'rule-details'
33
sidebarDepth: 0
44
title: 'svelte/no-navigation-without-base'
5-
description: 'disallow using goto() without the base path'
5+
description: 'disallow using navigation (links, goto, pushState, replaceState) without the base path'
66
since: 'v2.36.0-next.9'
77
---
88

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

11-
> disallow using goto() without the base path
11+
> disallow using navigation (links, goto, pushState, replaceState) without the base path
1212
1313
## :book: Rule Details
1414

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export interface RuleOptions {
181181
*/
182182
'svelte/no-inspect'?: Linter.RuleEntry<[]>
183183
/**
184-
* disallow using goto() without the base path
184+
* disallow using navigation (links, goto, pushState, replaceState) without the base path
185185
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-base/
186186
*/
187187
'svelte/no-navigation-without-base'?: Linter.RuleEntry<[]>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- message: Found a link with a url that isn't prefixed with the base path.
2+
line: 5
3+
column: 9
4+
suggestions: null
5+
- message: Found a link with a url that isn't prefixed with the base path.
6+
line: 6
7+
column: 9
8+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import { base } from '$app/paths';
3+
</script>
4+
5+
<a href={'/foo/' + base}>Click me!</a>
6+
<a href={`/foo/${base}`}>Click me!</a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- message: Found a link with a url that isn't prefixed with the base path.
2+
line: 1
3+
column: 10
4+
suggestions: null
5+
- message: Found a link with a url that isn't prefixed with the base path.
6+
line: 2
7+
column: 9
8+
suggestions: null
9+
- message: Found a link with a url that isn't prefixed with the base path.
10+
line: 3
11+
column: 9
12+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<a href="/foo">Click me!</a>
2+
<a href={'/foo'}>Click me!</a>
3+
<a href={'/' + 'foo'}>Click me!</a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- message: Found a pushState() call with a url that isn't prefixed with the base path.
2+
line: 4
3+
column: 8
4+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { pushState as alias } from '$app/navigation';
3+
4+
alias('/foo');
5+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- message: Found a pushState() call with a url that isn't prefixed with the base path.
2+
line: 5
3+
column: 12
4+
suggestions: null
5+
- message: Found a pushState() call with a url that isn't prefixed with the base path.
6+
line: 6
7+
column: 12
8+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { base } from '$app/paths';
3+
import { pushState } from '$app/navigation';
4+
5+
pushState('/foo/' + base);
6+
pushState(`/foo/${base}`);
7+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- message: Found a pushState() call with a url that isn't prefixed with the base path.
2+
line: 4
3+
column: 12
4+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { pushState } from '$app/navigation';
3+
4+
pushState('/foo');
5+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- message: Found a replaceState() call with a url that isn't prefixed with the
2+
base path.
3+
line: 4
4+
column: 8
5+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { replaceState as alias } from '$app/navigation';
3+
4+
alias('/foo');
5+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- message: Found a replaceState() call with a url that isn't prefixed with the
2+
base path.
3+
line: 5
4+
column: 15
5+
suggestions: null
6+
- message: Found a replaceState() call with a url that isn't prefixed with the
7+
base path.
8+
line: 6
9+
column: 15
10+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { base } from '$app/paths';
3+
import { replaceState } from '$app/navigation';
4+
5+
replaceState('/foo/' + base);
6+
replaceState(`/foo/${base}`);
7+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- message: Found a replaceState() call with a url that isn't prefixed with the
2+
base path.
3+
line: 4
4+
column: 15
5+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { replaceState } from '$app/navigation';
3+
4+
replaceState('/foo');
5+
</script>

packages/eslint-plugin-svelte/tests/fixtures/rules/no-navigation-without-base/valid/absolute-uri01-input.svelte

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
const protocol = 'https';
3+
</script>
4+
5+
<a href="http://svelte.dev">Click me!</a>
6+
<a href="https://svelte.dev">Click me!</a>
7+
<a href={'http://svelte.dev'}>Click me!</a>
8+
<a href={'https://svelte.dev'}>Click me!</a>
9+
<a href={'http://svelte' + '.dev'}>Click me!</a>
10+
<a href={'https://svelte' + '.dev'}>Click me!</a>
11+
<a href={'http' + '://svelte.dev'}>Click me!</a>
12+
<a href={'https' + '://svelte.dev'}>Click me!</a>
13+
<a href={`${protocol}://svelte.dev`}>Click me!</a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import { base as alias } from '$app/paths';
3+
</script>
4+
5+
<a href={alias + '/foo/'}>Click me!</a>;
6+
<a href={`${alias}/foo/`}>Click me!</a>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import { base } from '$app/paths';
3+
</script>
4+
5+
<a href={base + '/foo/'}>Click me!</a>
6+
<a href={`${base}/foo/`}>Click me!</a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
import { base as alias } from '$app/paths';
3+
import { pushState } from '$app/navigation';
4+
5+
// eslint-disable-next-line prefer-template -- Testing both variants
6+
pushState(alias + '/foo/');
7+
pushState(`${alias}/foo/`);
8+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
import { base } from '$app/paths';
3+
import { pushState } from '$app/navigation';
4+
5+
// eslint-disable-next-line prefer-template -- Testing both variants
6+
pushState(base + '/foo/');
7+
pushState(`${base}/foo/`);
8+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import { pushState } from '$app/navigation';
3+
4+
pushState('');
5+
pushState(``);
6+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
import { base as alias } from '$app/paths';
3+
import { replaceState } from '$app/navigation';
4+
5+
// eslint-disable-next-line prefer-template -- Testing both variants
6+
replaceState(alias + '/foo/');
7+
replaceState(`${alias}/foo/`);
8+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
import { base } from '$app/paths';
3+
import { replaceState } from '$app/navigation';
4+
5+
// eslint-disable-next-line prefer-template -- Testing both variants
6+
replaceState(base + '/foo/');
7+
replaceState(`${base}/foo/`);
8+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import { replaceState } from '$app/navigation';
3+
4+
replaceState('');
5+
replaceState(``);
6+
</script>

0 commit comments

Comments
 (0)