Skip to content

Commit e5ca553

Browse files
committed
chore(no-navigation-without-resolve): removed unneeded recursion
1 parent fcb942b commit e5ca553

1 file changed

Lines changed: 21 additions & 70 deletions

File tree

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

Lines changed: 21 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,11 @@ function isValueAllowed(
292292
}
293293
}
294294
if (
295-
(config.allowAbsolute && expressionIsAbsoluteUrl(ctx, value)) ||
295+
(config.allowAbsolute && expressionIsAbsoluteUrl(value)) ||
296296
(config.allowEmpty && expressionIsEmpty(value)) ||
297-
(config.allowFragment && expressionStartsWith(ctx, value, '#')) ||
297+
(config.allowFragment && expressionStartsWith(value, '#')) ||
298298
(config.allowNullish && expressionIsNullish(value)) ||
299-
expressionIsResolveCall(ctx, value, resolveReferences)
299+
expressionIsResolveCall(value, resolveReferences)
300300
) {
301301
return true;
302302
}
@@ -306,32 +306,16 @@ function isValueAllowed(
306306
// Helper functions
307307

308308
function expressionIsResolveCall(
309-
ctx: FindVariableContext,
310309
node: TSESTree.CallExpressionArgument | AST.SvelteLiteral,
311310
resolveReferences: Set<TSESTree.Identifier>
312311
): boolean {
313-
if (
312+
return (
314313
node.type === 'CallExpression' &&
315314
((node.callee.type === 'Identifier' && resolveReferences.has(node.callee)) ||
316315
(node.callee.type === 'MemberExpression' &&
317316
node.callee.property.type === 'Identifier' &&
318317
resolveReferences.has(node.callee.property)))
319-
) {
320-
return true;
321-
}
322-
if (node.type !== 'Identifier') {
323-
return false;
324-
}
325-
const variable = ctx.findVariable(node);
326-
if (
327-
variable === null ||
328-
variable.identifiers.length === 0 ||
329-
variable.identifiers[0].parent.type !== 'VariableDeclarator' ||
330-
variable.identifiers[0].parent.init === null
331-
) {
332-
return false;
333-
}
334-
return expressionIsResolveCall(ctx, variable.identifiers[0].parent.init, resolveReferences);
318+
);
335319
}
336320

337321
function expressionIsEmpty(
@@ -360,39 +344,33 @@ function expressionIsNullish(
360344
}
361345

362346
function expressionIsAbsoluteUrl(
363-
ctx: FindVariableContext,
364347
node: TSESTree.CallExpressionArgument | TSESTree.Expression | AST.SvelteLiteral
365348
): boolean {
366349
switch (node.type) {
367350
case 'BinaryExpression':
368-
return binaryExpressionIsAbsoluteUrl(ctx, node);
351+
return binaryExpressionIsAbsoluteUrl(node);
369352
case 'Literal':
370353
return typeof node.value === 'string' && valueIsAbsoluteUrl(node.value);
371354
case 'SvelteLiteral':
372355
return valueIsAbsoluteUrl(node.value);
373356
case 'TemplateLiteral':
374-
return templateLiteralIsAbsoluteUrl(ctx, node);
357+
return templateLiteralIsAbsoluteUrl(node);
375358
default:
376359
return false;
377360
}
378361
}
379362

380-
function binaryExpressionIsAbsoluteUrl(
381-
ctx: FindVariableContext,
382-
node: TSESTree.BinaryExpression
383-
): boolean {
363+
// TODO: Check the operator
364+
function binaryExpressionIsAbsoluteUrl(node: TSESTree.BinaryExpression): boolean {
384365
return (
385-
(node.left.type !== 'PrivateIdentifier' && expressionIsAbsoluteUrl(ctx, node.left)) ||
386-
expressionIsAbsoluteUrl(ctx, node.right)
366+
(node.left.type !== 'PrivateIdentifier' && expressionIsAbsoluteUrl(node.left)) ||
367+
expressionIsAbsoluteUrl(node.right)
387368
);
388369
}
389370

390-
function templateLiteralIsAbsoluteUrl(
391-
ctx: FindVariableContext,
392-
node: TSESTree.TemplateLiteral
393-
): boolean {
371+
function templateLiteralIsAbsoluteUrl(node: TSESTree.TemplateLiteral): boolean {
394372
return (
395-
node.expressions.some((expression) => expressionIsAbsoluteUrl(ctx, expression)) ||
373+
node.expressions.some((expression) => expressionIsAbsoluteUrl(expression)) ||
396374
node.quasis.some((quasi) => valueIsAbsoluteUrl(quasi.value.raw))
397375
);
398376
}
@@ -402,7 +380,6 @@ function valueIsAbsoluteUrl(node: string): boolean {
402380
}
403381

404382
function expressionStartsWith(
405-
ctx: FindVariableContext,
406383
node:
407384
| TSESTree.CallExpressionArgument
408385
| TSESTree.Expression
@@ -412,54 +389,28 @@ function expressionStartsWith(
412389
): boolean {
413390
switch (node.type) {
414391
case 'BinaryExpression':
415-
return binaryExpressionStartsWith(ctx, node, prefix);
416-
case 'Identifier':
417-
return identifierStartsWith(ctx, node, prefix);
392+
return binaryExpressionStartsWith(node, prefix);
418393
case 'Literal':
419394
return typeof node.value === 'string' && node.value.startsWith(prefix);
420395
case 'SvelteLiteral':
421396
return node.value.startsWith(prefix);
422397
case 'TemplateElement':
423398
return node.value.raw.startsWith(prefix);
424399
case 'TemplateLiteral':
425-
return templateLiteralStartsWith(ctx, node, prefix);
400+
return templateLiteralStartsWith(node, prefix);
426401
default:
427402
return false;
428403
}
429404
}
430405

431-
function binaryExpressionStartsWith(
432-
ctx: FindVariableContext,
433-
node: TSESTree.BinaryExpression,
434-
prefix: string
435-
): boolean {
436-
return node.left.type !== 'PrivateIdentifier' && expressionStartsWith(ctx, node.left, prefix);
437-
}
438-
439-
function identifierStartsWith(
440-
ctx: FindVariableContext,
441-
node: TSESTree.Identifier,
442-
prefix: string
443-
): boolean {
444-
const variable = ctx.findVariable(node);
445-
if (
446-
variable === null ||
447-
variable.identifiers.length === 0 ||
448-
variable.identifiers[0].parent.type !== 'VariableDeclarator' ||
449-
variable.identifiers[0].parent.init === null
450-
) {
451-
return false;
452-
}
453-
return expressionStartsWith(ctx, variable.identifiers[0].parent.init, prefix);
406+
// TODO: Check the operator
407+
function binaryExpressionStartsWith(node: TSESTree.BinaryExpression, prefix: string): boolean {
408+
return node.left.type !== 'PrivateIdentifier' && expressionStartsWith(node.left, prefix);
454409
}
455410

456-
function templateLiteralStartsWith(
457-
ctx: FindVariableContext,
458-
node: TSESTree.TemplateLiteral,
459-
prefix: string
460-
): boolean {
411+
function templateLiteralStartsWith(node: TSESTree.TemplateLiteral, prefix: string): boolean {
461412
return (
462-
(node.expressions.length >= 1 && expressionStartsWith(ctx, node.expressions[0], prefix)) ||
463-
(node.quasis.length >= 1 && expressionStartsWith(ctx, node.quasis[0], prefix))
413+
(node.expressions.length >= 1 && expressionStartsWith(node.expressions[0], prefix)) ||
414+
(node.quasis.length >= 1 && expressionStartsWith(node.quasis[0], prefix))
464415
);
465416
}

0 commit comments

Comments
 (0)