Skip to content

Commit 87f712d

Browse files
committed
feat(no-navigation-without-resolve): added support for ResolvedPathname types
1 parent 046d91c commit 87f712d

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

.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
}
@@ -135,7 +140,8 @@ export default createRule('no-navigation-without-resolve', {
135140
!isResolveCall(
136141
new FindVariableContext(context),
137142
node.value[0].expression,
138-
resolveReferences
143+
resolveReferences,
144+
tsTools
139145
))
140146
) {
141147
context.report({ loc: node.value[0].loc, messageId: 'linkWithoutResolve' });
@@ -223,13 +229,14 @@ function extractFunctionCallReferences(referenceTracker: ReferenceTracker): {
223229
function checkGotoCall(
224230
context: RuleContext,
225231
call: TSESTree.CallExpression,
226-
resolveReferences: Set<TSESTree.Identifier>
232+
resolveReferences: Set<TSESTree.Identifier>,
233+
tsTools: TSTools | null
227234
): void {
228235
if (call.arguments.length < 1) {
229236
return;
230237
}
231238
const url = call.arguments[0];
232-
if (!isResolveCall(new FindVariableContext(context), url, resolveReferences)) {
239+
if (!isResolveCall(new FindVariableContext(context), url, resolveReferences, tsTools)) {
233240
context.report({ loc: url.loc, messageId: 'gotoWithoutResolve' });
234241
}
235242
}
@@ -238,6 +245,7 @@ function checkShallowNavigationCall(
238245
context: RuleContext,
239246
call: TSESTree.CallExpression,
240247
resolveReferences: Set<TSESTree.Identifier>,
248+
tsTools: TSTools | null,
241249
messageId: string
242250
): void {
243251
if (call.arguments.length < 1) {
@@ -246,7 +254,7 @@ function checkShallowNavigationCall(
246254
const url = call.arguments[0];
247255
if (
248256
!expressionIsEmpty(url) &&
249-
!isResolveCall(new FindVariableContext(context), url, resolveReferences)
257+
!isResolveCall(new FindVariableContext(context), url, resolveReferences, tsTools)
250258
) {
251259
context.report({ loc: url.loc, messageId });
252260
}
@@ -257,7 +265,8 @@ function checkShallowNavigationCall(
257265
function isResolveCall(
258266
ctx: FindVariableContext,
259267
node: TSESTree.CallExpressionArgument,
260-
resolveReferences: Set<TSESTree.Identifier>
268+
resolveReferences: Set<TSESTree.Identifier>,
269+
tsTools: TSTools | null
261270
): boolean {
262271
if (
263272
node.type === 'CallExpression' &&
@@ -268,9 +277,13 @@ function isResolveCall(
268277
) {
269278
return true;
270279
}
271-
if (node.type !== 'Identifier') {
280+
if (node.type !== 'Identifier' || tsTools === null) {
272281
return false;
273282
}
283+
const tsNode = tsTools.service.esTreeNodeToTSNodeMap.get(node);
284+
console.log(tsNode);
285+
console.log(tsTools.service.program.getTypeChecker().getTypeAtLocation(tsNode));
286+
/*
274287
const variable = ctx.findVariable(node);
275288
if (
276289
variable === null ||
@@ -281,6 +294,7 @@ function isResolveCall(
281294
return false;
282295
}
283296
return isResolveCall(ctx, variable.identifiers[0].parent.init, resolveReferences);
297+
*/
284298
}
285299

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

0 commit comments

Comments
 (0)