Skip to content

Commit 51a0858

Browse files
committed
feat(no-navigation-without-resolve): added support for ResolvedPathname types
1 parent f209157 commit 51a0858

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

5356
const ignoreGoto = context.options[0]?.ignoreGoto ?? false;
@@ -66,7 +69,7 @@ export default createRule('no-navigation-without-resolve', {
6669
} = extractFunctionCallReferences(referenceTracker);
6770
if (!ignoreGoto) {
6871
for (const gotoCall of gotoCalls) {
69-
checkGotoCall(context, gotoCall, resolveReferences);
72+
checkGotoCall(context, gotoCall, resolveReferences, tsTools);
7073
}
7174
}
7275
if (!ignorePushState) {
@@ -75,6 +78,7 @@ export default createRule('no-navigation-without-resolve', {
7578
context,
7679
pushStateCall,
7780
resolveReferences,
81+
tsTools,
7882
'pushStateWithoutResolve'
7983
);
8084
}
@@ -85,6 +89,7 @@ export default createRule('no-navigation-without-resolve', {
8589
context,
8690
replaceStateCall,
8791
resolveReferences,
92+
tsTools,
8893
'replaceStateWithoutResolve'
8994
);
9095
}
@@ -133,7 +138,8 @@ export default createRule('no-navigation-without-resolve', {
133138
!isResolveCall(
134139
new FindVariableContext(context),
135140
node.value[0].expression,
136-
resolveReferences
141+
resolveReferences,
142+
tsTools
137143
))
138144
) {
139145
context.report({ loc: node.value[0].loc, messageId: 'linkWithoutResolve' });
@@ -221,13 +227,14 @@ function extractFunctionCallReferences(referenceTracker: ReferenceTracker): {
221227
function checkGotoCall(
222228
context: RuleContext,
223229
call: TSESTree.CallExpression,
224-
resolveReferences: Set<TSESTree.Identifier>
230+
resolveReferences: Set<TSESTree.Identifier>,
231+
tsTools: TSTools | null
225232
): void {
226233
if (call.arguments.length < 1) {
227234
return;
228235
}
229236
const url = call.arguments[0];
230-
if (!isResolveCall(new FindVariableContext(context), url, resolveReferences)) {
237+
if (!isResolveCall(new FindVariableContext(context), url, resolveReferences, tsTools)) {
231238
context.report({ loc: url.loc, messageId: 'gotoWithoutResolve' });
232239
}
233240
}
@@ -236,6 +243,7 @@ function checkShallowNavigationCall(
236243
context: RuleContext,
237244
call: TSESTree.CallExpression,
238245
resolveReferences: Set<TSESTree.Identifier>,
246+
tsTools: TSTools | null,
239247
messageId: string
240248
): void {
241249
if (call.arguments.length < 1) {
@@ -244,7 +252,7 @@ function checkShallowNavigationCall(
244252
const url = call.arguments[0];
245253
if (
246254
!expressionIsEmpty(url) &&
247-
!isResolveCall(new FindVariableContext(context), url, resolveReferences)
255+
!isResolveCall(new FindVariableContext(context), url, resolveReferences, tsTools)
248256
) {
249257
context.report({ loc: url.loc, messageId });
250258
}
@@ -255,7 +263,8 @@ function checkShallowNavigationCall(
255263
function isResolveCall(
256264
ctx: FindVariableContext,
257265
node: TSESTree.CallExpressionArgument,
258-
resolveReferences: Set<TSESTree.Identifier>
266+
resolveReferences: Set<TSESTree.Identifier>,
267+
tsTools: TSTools | null
259268
): boolean {
260269
if (
261270
node.type === 'CallExpression' &&
@@ -266,9 +275,13 @@ function isResolveCall(
266275
) {
267276
return true;
268277
}
269-
if (node.type !== 'Identifier') {
278+
if (node.type !== 'Identifier' || tsTools === null) {
270279
return false;
271280
}
281+
const tsNode = tsTools.service.esTreeNodeToTSNodeMap.get(node);
282+
console.log(tsNode);
283+
console.log(tsTools.service.program.getTypeChecker().getTypeAtLocation(tsNode));
284+
/*
272285
const variable = ctx.findVariable(node);
273286
if (
274287
variable === null ||
@@ -279,6 +292,7 @@ function isResolveCall(
279292
return false;
280293
}
281294
return isResolveCall(ctx, variable.identifiers[0].parent.init, resolveReferences);
295+
*/
282296
}
283297

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

0 commit comments

Comments
 (0)