Skip to content

Commit a81abe5

Browse files
committed
feat(no-navigation-without-resolve): added support for ResolvedPathname types
1 parent 0e29f49 commit a81abe5

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' });
@@ -193,13 +199,14 @@ function extractFunctionCallReferences(referenceTracker: ReferenceTracker): {
193199
function checkGotoCall(
194200
context: RuleContext,
195201
call: TSESTree.CallExpression,
196-
resolveReferences: Set<TSESTree.Identifier>
202+
resolveReferences: Set<TSESTree.Identifier>,
203+
tsTools: TSTools | null
197204
): void {
198205
if (call.arguments.length < 1) {
199206
return;
200207
}
201208
const url = call.arguments[0];
202-
if (!isResolveCall(new FindVariableContext(context), url, resolveReferences)) {
209+
if (!isResolveCall(new FindVariableContext(context), url, resolveReferences, tsTools)) {
203210
context.report({ loc: url.loc, messageId: 'gotoWithoutResolve' });
204211
}
205212
}
@@ -208,6 +215,7 @@ function checkShallowNavigationCall(
208215
context: RuleContext,
209216
call: TSESTree.CallExpression,
210217
resolveReferences: Set<TSESTree.Identifier>,
218+
tsTools: TSTools | null,
211219
messageId: string
212220
): void {
213221
if (call.arguments.length < 1) {
@@ -216,7 +224,7 @@ function checkShallowNavigationCall(
216224
const url = call.arguments[0];
217225
if (
218226
!expressionIsEmpty(url) &&
219-
!isResolveCall(new FindVariableContext(context), url, resolveReferences)
227+
!isResolveCall(new FindVariableContext(context), url, resolveReferences, tsTools)
220228
) {
221229
context.report({ loc: url.loc, messageId });
222230
}
@@ -227,7 +235,8 @@ function checkShallowNavigationCall(
227235
function isResolveCall(
228236
ctx: FindVariableContext,
229237
node: TSESTree.CallExpressionArgument,
230-
resolveReferences: Set<TSESTree.Identifier>
238+
resolveReferences: Set<TSESTree.Identifier>,
239+
tsTools: TSTools | null
231240
): boolean {
232241
if (
233242
node.type === 'CallExpression' &&
@@ -238,9 +247,13 @@ function isResolveCall(
238247
) {
239248
return true;
240249
}
241-
if (node.type !== 'Identifier') {
250+
if (node.type !== 'Identifier' || tsTools === null) {
242251
return false;
243252
}
253+
const tsNode = tsTools.service.esTreeNodeToTSNodeMap.get(node);
254+
console.log(tsNode);
255+
console.log(tsTools.service.program.getTypeChecker().getTypeAtLocation(tsNode));
256+
/*
244257
const variable = ctx.findVariable(node);
245258
if (
246259
variable === null ||
@@ -251,6 +264,7 @@ function isResolveCall(
251264
return false;
252265
}
253266
return isResolveCall(ctx, variable.identifiers[0].parent.init, resolveReferences);
267+
*/
254268
}
255269

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

0 commit comments

Comments
 (0)