Skip to content

Commit 5596c25

Browse files
committed
fix(no-navigation-without-base): fixed an infinite loop
1 parent e87c08d commit 5596c25

File tree

1 file changed

+31
-65
lines changed

1 file changed

+31
-65
lines changed

packages/eslint-plugin-svelte/src/utils/expression-affixes.ts

Lines changed: 31 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,43 @@ import type { RuleContext } from '../types.js';
44
import type { AST } from 'svelte-eslint-parser';
55
import { ASTSearchHelper } from './ast-search-helper.js';
66

7-
// Variable prefix extraction
8-
97
export function extractExpressionPrefixVariable(
108
context: RuleContext,
119
expression: TSESTree.Expression
1210
): TSESTree.Identifier | null {
13-
switch (expression.type) {
14-
case 'BinaryExpression':
15-
return extractBinaryExpressionPrefixVariable(context, expression);
16-
case 'Identifier':
17-
return extractVariablePrefixVariable(context, expression);
18-
case 'MemberExpression':
19-
return extractMemberExpressionPrefixVariable(expression);
20-
case 'TemplateLiteral':
21-
return extractTemplateLiteralPrefixVariable(context, expression);
22-
default:
11+
return ASTSearchHelper(expression, {
12+
BinaryExpression: (node, searchAnotherNode) =>
13+
node.left.type !== 'PrivateIdentifier' ? searchAnotherNode(node.left) : null,
14+
Identifier: (node, searchAnotherNode) => {
15+
const variable = findVariable(context, node);
16+
if (
17+
variable === null ||
18+
variable.identifiers.length !== 1 ||
19+
variable.identifiers[0].parent.type !== 'VariableDeclarator' ||
20+
variable.identifiers[0].parent.init === null
21+
) {
22+
return node;
23+
}
24+
return searchAnotherNode(variable.identifiers[0].parent.init) ?? node;
25+
},
26+
MemberExpression: (node) => (node.property.type === 'Identifier' ? node.property : null),
27+
TemplateLiteral: (node, searchAnotherNode) => {
28+
const literalParts = [...node.expressions, ...node.quasis].sort((a, b) =>
29+
a.range[0] < b.range[0] ? -1 : 1
30+
);
31+
for (const part of literalParts) {
32+
if (part.type === 'TemplateElement' && part.value.raw === '') {
33+
// Skip empty quasi in the begining
34+
continue;
35+
}
36+
if (part.type !== 'TemplateElement') {
37+
return searchAnotherNode(part);
38+
}
39+
return null;
40+
}
2341
return null;
24-
}
25-
}
26-
27-
function extractBinaryExpressionPrefixVariable(
28-
context: RuleContext,
29-
expression: TSESTree.BinaryExpression
30-
): TSESTree.Identifier | null {
31-
return expression.left.type !== 'PrivateIdentifier'
32-
? extractExpressionPrefixVariable(context, expression.left)
33-
: null;
34-
}
35-
36-
function extractVariablePrefixVariable(
37-
context: RuleContext,
38-
expression: TSESTree.Identifier
39-
): TSESTree.Identifier | null {
40-
const variable = findVariable(context, expression);
41-
if (
42-
variable === null ||
43-
variable.identifiers.length !== 1 ||
44-
variable.identifiers[0].parent.type !== 'VariableDeclarator' ||
45-
variable.identifiers[0].parent.init === null
46-
) {
47-
return expression;
48-
}
49-
return (
50-
extractExpressionPrefixVariable(context, variable.identifiers[0].parent.init) ?? expression
51-
);
52-
}
53-
54-
function extractMemberExpressionPrefixVariable(
55-
expression: TSESTree.MemberExpression
56-
): TSESTree.Identifier | null {
57-
return expression.property.type === 'Identifier' ? expression.property : null;
58-
}
59-
60-
function extractTemplateLiteralPrefixVariable(
61-
context: RuleContext,
62-
expression: TSESTree.TemplateLiteral
63-
): TSESTree.Identifier | null {
64-
const literalParts = [...expression.expressions, ...expression.quasis].sort((a, b) =>
65-
a.range[0] < b.range[0] ? -1 : 1
66-
);
67-
for (const part of literalParts) {
68-
if (part.type === 'TemplateElement' && part.value.raw === '') {
69-
// Skip empty quasi in the begining
70-
continue;
7142
}
72-
if (part.type !== 'TemplateElement') {
73-
return extractExpressionPrefixVariable(context, part);
74-
}
75-
return null;
76-
}
77-
return null;
43+
});
7844
}
7945

8046
export function extractExpressionPrefixLiteral(

0 commit comments

Comments
 (0)