Skip to content

Commit 6620c3a

Browse files
committed
feat(no-navigation-without-resolve): added support for ResolvedPathname types
1 parent af3586f commit 6620c3a

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

.changeset/icy-mammals-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': patch
3+
---
4+
5+
feat(no-navigation-without-resolve): added support for ResolvedPathname types

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { FindVariableContext } from '../utils/ast-utils.js';
55
import { findVariable } from '../utils/ast-utils.js';
66
import type { RuleContext } from '../types.js';
77
import type { AST } from 'svelte-eslint-parser';
8+
import { type TSTools, getTypeScriptTools } from 'src/utils/ts-utils/index.js';
89

910
export default createRule('no-navigation-without-resolve', {
1011
meta: {
@@ -48,6 +49,8 @@ export default createRule('no-navigation-without-resolve', {
4849
]
4950
},
5051
create(context) {
52+
const tsTools = getTypeScriptTools(context);
53+
5154
let resolveReferences: Set<TSESTree.Identifier> = new Set<TSESTree.Identifier>();
5255
return {
5356
Program() {
@@ -60,7 +63,7 @@ export default createRule('no-navigation-without-resolve', {
6063
} = extractFunctionCallReferences(referenceTracker);
6164
if (context.options[0]?.ignoreGoto !== true) {
6265
for (const gotoCall of gotoCalls) {
63-
checkGotoCall(context, gotoCall, resolveReferences);
66+
checkGotoCall(context, gotoCall, resolveReferences, tsTools);
6467
}
6568
}
6669
if (context.options[0]?.ignorePushState !== true) {
@@ -69,6 +72,7 @@ export default createRule('no-navigation-without-resolve', {
6972
context,
7073
pushStateCall,
7174
resolveReferences,
75+
tsTools,
7276
'pushStateWithoutResolve'
7377
);
7478
}
@@ -79,6 +83,7 @@ export default createRule('no-navigation-without-resolve', {
7983
context,
8084
replaceStateCall,
8185
resolveReferences,
86+
tsTools,
8287
'replaceStateWithoutResolve'
8388
);
8489
}
@@ -105,7 +110,8 @@ export default createRule('no-navigation-without-resolve', {
105110
!isResolveCall(
106111
new FindVariableContext(context),
107112
node.value[0].expression,
108-
resolveReferences
113+
resolveReferences,
114+
tsTools
109115
))
110116
) {
111117
context.report({ loc: node.value[0].loc, messageId: 'linkWithoutResolve' });
@@ -190,13 +196,14 @@ function extractFunctionCallReferences(referenceTracker: ReferenceTracker): {
190196
function checkGotoCall(
191197
context: RuleContext,
192198
call: TSESTree.CallExpression,
193-
resolveReferences: Set<TSESTree.Identifier>
199+
resolveReferences: Set<TSESTree.Identifier>,
200+
tsTools: TSTools | null
194201
): void {
195202
if (call.arguments.length < 1) {
196203
return;
197204
}
198205
const url = call.arguments[0];
199-
if (!isResolveCall(new FindVariableContext(context), url, resolveReferences)) {
206+
if (!isResolveCall(new FindVariableContext(context), url, resolveReferences, tsTools)) {
200207
context.report({ loc: url.loc, messageId: 'gotoWithoutResolve' });
201208
}
202209
}
@@ -205,6 +212,7 @@ function checkShallowNavigationCall(
205212
context: RuleContext,
206213
call: TSESTree.CallExpression,
207214
resolveReferences: Set<TSESTree.Identifier>,
215+
tsTools: TSTools | null,
208216
messageId: string
209217
): void {
210218
if (call.arguments.length < 1) {
@@ -213,7 +221,7 @@ function checkShallowNavigationCall(
213221
const url = call.arguments[0];
214222
if (
215223
!expressionIsEmpty(url) &&
216-
!isResolveCall(new FindVariableContext(context), url, resolveReferences)
224+
!isResolveCall(new FindVariableContext(context), url, resolveReferences, tsTools)
217225
) {
218226
context.report({ loc: url.loc, messageId });
219227
}
@@ -224,7 +232,8 @@ function checkShallowNavigationCall(
224232
function isResolveCall(
225233
ctx: FindVariableContext,
226234
node: TSESTree.CallExpressionArgument,
227-
resolveReferences: Set<TSESTree.Identifier>
235+
resolveReferences: Set<TSESTree.Identifier>,
236+
tsTools: TSTools | null
228237
): boolean {
229238
if (
230239
node.type === 'CallExpression' &&
@@ -235,9 +244,13 @@ function isResolveCall(
235244
) {
236245
return true;
237246
}
238-
if (node.type !== 'Identifier') {
247+
if (node.type !== 'Identifier' || tsTools === null) {
239248
return false;
240249
}
250+
const tsNode = tsTools.service.esTreeNodeToTSNodeMap.get(node);
251+
console.log(tsNode);
252+
console.log(tsTools.service.program.getTypeChecker().getTypeAtLocation(tsNode));
253+
/*
241254
const variable = ctx.findVariable(node);
242255
if (
243256
variable === null ||
@@ -248,6 +261,7 @@ function isResolveCall(
248261
return false;
249262
}
250263
return isResolveCall(ctx, variable.identifiers[0].parent.init, resolveReferences);
264+
*/
251265
}
252266

253267
function expressionIsEmpty(url: TSESTree.CallExpressionArgument): boolean {

0 commit comments

Comments
 (0)