Skip to content

Commit 9c8206b

Browse files
committed
feat(no-navigation-without-base): added configuration options
1 parent 4054b8c commit 9c8206b

File tree

10 files changed

+97
-13
lines changed

10 files changed

+97
-13
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export interface RuleOptions {
179179
* disallow using navigation (links, goto, pushState, replaceState) without the base path
180180
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-base/
181181
*/
182-
'svelte/no-navigation-without-base'?: Linter.RuleEntry<[]>
182+
'svelte/no-navigation-without-base'?: Linter.RuleEntry<SvelteNoNavigationWithoutBase>
183183
/**
184184
* disallow use of not function in event handler
185185
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-not-function-handler/
@@ -443,6 +443,13 @@ type SvelteNoInlineStyles = []|[{
443443
type SvelteNoInnerDeclarations = []|[("functions" | "both")]|[("functions" | "both"), {
444444
blockScopedFunctions?: ("allow" | "disallow")
445445
}]
446+
// ----- svelte/no-navigation-without-base -----
447+
type SvelteNoNavigationWithoutBase = []|[{
448+
ignoreGoto?: boolean
449+
ignoreLinks?: boolean
450+
ignorePushState?: boolean
451+
ignoreReplaceState?: boolean
452+
}]
446453
// ----- svelte/no-reactive-reassign -----
447454
type SvelteNoReactiveReassign = []|[{
448455
props?: boolean

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

+43-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,26 @@ export default createRule('no-navigation-without-base', {
1414
category: 'SvelteKit',
1515
recommended: false
1616
},
17-
schema: [],
17+
schema: [
18+
{
19+
type: 'object',
20+
properties: {
21+
ignoreGoto: {
22+
type: 'boolean'
23+
},
24+
ignoreLinks: {
25+
type: 'boolean'
26+
},
27+
ignorePushState: {
28+
type: 'boolean'
29+
},
30+
ignoreReplaceState: {
31+
type: 'boolean'
32+
}
33+
},
34+
additionalProperties: false
35+
}
36+
],
1837
messages: {
1938
gotoNotPrefixed: "Found a goto() call with a url that isn't prefixed with the base path.",
2039
linkNotPrefixed: "Found a link with a url that isn't prefixed with the base path.",
@@ -38,23 +57,35 @@ export default createRule('no-navigation-without-base', {
3857
pushState: pushStateCalls,
3958
replaceState: replaceStateCalls
4059
} = extractFunctionCallReferences(referenceTracker);
41-
for (const gotoCall of gotoCalls) {
42-
checkGotoCall(context, gotoCall, basePathNames);
60+
if (context.options[0]?.ignoreGoto !== true) {
61+
for (const gotoCall of gotoCalls) {
62+
checkGotoCall(context, gotoCall, basePathNames);
63+
}
4364
}
44-
for (const pushStateCall of pushStateCalls) {
45-
checkShallowNavigationCall(context, pushStateCall, basePathNames, 'pushStateNotPrefixed');
65+
if (context.options[0]?.ignorePushState !== true) {
66+
for (const pushStateCall of pushStateCalls) {
67+
checkShallowNavigationCall(
68+
context,
69+
pushStateCall,
70+
basePathNames,
71+
'pushStateNotPrefixed'
72+
);
73+
}
4674
}
47-
for (const replaceStateCall of replaceStateCalls) {
48-
checkShallowNavigationCall(
49-
context,
50-
replaceStateCall,
51-
basePathNames,
52-
'replaceStateNotPrefixed'
53-
);
75+
if (context.options[0]?.ignoreReplaceState !== true) {
76+
for (const replaceStateCall of replaceStateCalls) {
77+
checkShallowNavigationCall(
78+
context,
79+
replaceStateCall,
80+
basePathNames,
81+
'replaceStateNotPrefixed'
82+
);
83+
}
5484
}
5585
},
5686
SvelteAttribute(node) {
5787
if (
88+
context.options[0]?.ignoreLinks === true ||
5889
node.parent.parent.type !== 'SvelteElement' ||
5990
node.parent.parent.kind !== 'html' ||
6091
node.parent.parent.name.type !== 'SvelteName' ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"options": [
3+
{
4+
"ignoreGoto": true
5+
}
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { goto } from '$app/navigation';
3+
4+
goto('/foo');
5+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"options": [
3+
{
4+
"ignoreLinks": true
5+
}
6+
]
7+
}
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,7 @@
1+
{
2+
"options": [
3+
{
4+
"ignorePushState": true
5+
}
6+
]
7+
}
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,7 @@
1+
{
2+
"options": [
3+
{
4+
"ignoreReplaceState": true
5+
}
6+
]
7+
}
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>

0 commit comments

Comments
 (0)