Skip to content

Commit c461b6b

Browse files
committed
chore(no-navigation-without-resolve): simplified link checking logic
1 parent ef87440 commit c461b6b

File tree

2 files changed

+39
-48
lines changed

2 files changed

+39
-48
lines changed

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

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -90,55 +90,25 @@ export default createRule('no-navigation-without-resolve', {
9090
}
9191
}
9292
},
93-
SvelteShorthandAttribute(node) {
94-
if (
95-
ignoreLinks ||
96-
node.parent.parent.type !== 'SvelteElement' ||
97-
node.parent.parent.kind !== 'html' ||
98-
node.parent.parent.name.type !== 'SvelteName' ||
99-
node.parent.parent.name.name !== 'a' ||
100-
node.key.name !== 'href' ||
101-
node.value.type !== 'Identifier'
102-
) {
103-
return;
104-
}
105-
if (
106-
!expressionIsAbsolute(new FindVariableContext(context), node.value) &&
107-
!expressionIsFragment(new FindVariableContext(context), node.value) &&
108-
!isResolveCall(new FindVariableContext(context), node.value, resolveReferences)
109-
) {
110-
context.report({ loc: node.loc, messageId: 'linkWithoutResolve' });
111-
}
112-
},
113-
SvelteAttribute(node) {
114-
if (
115-
ignoreLinks ||
116-
node.parent.parent.type !== 'SvelteElement' ||
117-
node.parent.parent.kind !== 'html' ||
118-
node.parent.parent.name.type !== 'SvelteName' ||
119-
node.parent.parent.name.name !== 'a' ||
120-
node.key.name !== 'href'
121-
) {
122-
return;
123-
}
124-
if (
125-
(node.value[0].type === 'SvelteLiteral' &&
126-
!expressionIsNullish(new FindVariableContext(context), node.value[0]) &&
127-
!expressionIsAbsolute(new FindVariableContext(context), node.value[0]) &&
128-
!expressionIsFragment(new FindVariableContext(context), node.value[0])) ||
129-
(node.value[0].type === 'SvelteMustacheTag' &&
130-
!expressionIsNullish(new FindVariableContext(context), node.value[0].expression) &&
131-
!expressionIsAbsolute(new FindVariableContext(context), node.value[0].expression) &&
132-
!expressionIsFragment(new FindVariableContext(context), node.value[0].expression) &&
133-
!isResolveCall(
134-
new FindVariableContext(context),
135-
node.value[0].expression,
93+
...(!ignoreLinks && {
94+
SvelteShorthandAttribute(node) {
95+
if (!isLinkAttributeOk(context, node, node.value, resolveReferences)) {
96+
context.report({ loc: node.loc, messageId: 'linkWithoutResolve' });
97+
}
98+
},
99+
SvelteAttribute(node) {
100+
if (
101+
!isLinkAttributeOk(
102+
context,
103+
node,
104+
node.value[0].type === 'SvelteMustacheTag' ? node.value[0].expression : node.value[0],
136105
resolveReferences
137-
))
138-
) {
139-
context.report({ loc: node.value[0].loc, messageId: 'linkWithoutResolve' });
106+
)
107+
) {
108+
context.report({ loc: node.value[0].loc, messageId: 'linkWithoutResolve' });
109+
}
140110
}
141-
}
111+
})
142112
};
143113
}
144114
});
@@ -250,11 +220,30 @@ function checkShallowNavigationCall(
250220
}
251221
}
252222

223+
function isLinkAttributeOk(
224+
context: RuleContext,
225+
attribute: AST.SvelteAttribute | AST.SvelteShorthandAttribute,
226+
value: TSESTree.Expression | AST.SvelteLiteral,
227+
resolveReferences: Set<TSESTree.Identifier>
228+
): boolean {
229+
return (
230+
attribute.parent.parent.type !== 'SvelteElement' ||
231+
attribute.parent.parent.kind !== 'html' ||
232+
attribute.parent.parent.name.type !== 'SvelteName' ||
233+
attribute.parent.parent.name.name !== 'a' ||
234+
attribute.key.name !== 'href' ||
235+
expressionIsNullish(new FindVariableContext(context), value) ||
236+
expressionIsAbsolute(new FindVariableContext(context), value) ||
237+
expressionIsFragment(new FindVariableContext(context), value) ||
238+
isResolveCall(new FindVariableContext(context), value, resolveReferences)
239+
);
240+
}
241+
253242
// Helper functions
254243

255244
function isResolveCall(
256245
ctx: FindVariableContext,
257-
node: TSESTree.CallExpressionArgument,
246+
node: TSESTree.CallExpressionArgument | AST.SvelteLiteral,
258247
resolveReferences: Set<TSESTree.Identifier>
259248
): boolean {
260249
if (
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<script>
22
const one = undefined;
33
const two = null;
4+
const href = null;
45
</script>
56

67
<a href={undefined}>Click me!</a>
78
<a href={null}>Click me!</a>
89
<a href={one}>Click me!</a>
910
<a href={two}>Click me!</a>
11+
<a {href}>Click me!</a>

0 commit comments

Comments
 (0)