Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/public-groups-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

fix(no-navigation-without-resolve): allowing undefined and null in link hrefs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ export default createRule('no-navigation-without-resolve', {
}
if (
(node.value[0].type === 'SvelteLiteral' &&
!expressionIsNullish(new FindVariableContext(context), node.value[0]) &&
!expressionIsAbsolute(new FindVariableContext(context), node.value[0]) &&
!expressionIsFragment(new FindVariableContext(context), node.value[0])) ||
(node.value[0].type === 'SvelteMustacheTag' &&
!expressionIsNullish(new FindVariableContext(context), node.value[0].expression) &&
!expressionIsAbsolute(new FindVariableContext(context), node.value[0].expression) &&
!expressionIsFragment(new FindVariableContext(context), node.value[0].expression) &&
!isResolveCall(
Expand Down Expand Up @@ -263,6 +265,45 @@ function expressionIsEmpty(url: TSESTree.CallExpressionArgument): boolean {
);
}

function expressionIsNullish(
ctx: FindVariableContext,
url: AST.SvelteLiteral | TSESTree.Expression
): boolean {
switch (url.type) {
case 'Identifier':
return identifierIsNullish(ctx, url);
case 'Literal':
return url.value === null; // Undefined is an Identifier in ESTree, null is a Literal
case 'TemplateLiteral':
return templateLiteralIsNullish(ctx, url);
default:
return false;
}
}

function identifierIsNullish(ctx: FindVariableContext, url: TSESTree.Identifier): boolean {
if (url.name === 'undefined') {
return true;
}
const variable = ctx.findVariable(url);
if (
variable === null ||
variable.identifiers.length === 0 ||
variable.identifiers[0].parent.type !== 'VariableDeclarator' ||
variable.identifiers[0].parent.init === null
) {
return false;
}
return expressionIsNullish(ctx, variable.identifiers[0].parent.init);
}

function templateLiteralIsNullish(
ctx: FindVariableContext,
url: TSESTree.TemplateLiteral
): boolean {
return url.expressions.length === 1 && expressionIsNullish(ctx, url.expressions[0]);
}

function expressionIsAbsolute(
ctx: FindVariableContext,
url: AST.SvelteLiteral | TSESTree.Expression
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- message: Unexpected href link without resolve().
line: 6
column: 10
suggestions: null
- message: Unexpected href link without resolve().
line: 7
column: 10
suggestions: null
- message: Unexpected href link without resolve().
line: 8
column: 9
suggestions: null
- message: Unexpected href link without resolve().
line: 9
column: 9
suggestions: null
- message: Unexpected href link without resolve().
line: 10
column: 9
suggestions: null
- message: Unexpected href link without resolve().
line: 11
column: 9
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
const one = "undefined";
const two = "null";
</script>

<a href="undefined">Click me!</a>
<a href="null">Click me!</a>
<a href={one}>Click me!</a>
<a href={two}>Click me!</a>
<a href={`undefined`}>Click me!</a>
<a href={`null`}>Click me!</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
const one = undefined;
const two = null;
</script>

<a href={undefined}>Click me!</a>
<a href={null}>Click me!</a>
<a href={one}>Click me!</a>
<a href={two}>Click me!</a>
<a href={`${undefined}`}>Click me!</a>
<a href={`${null}`}>Click me!</a>
<a href={`${one}`}>Click me!</a>
<a href={`${two}`}>Click me!</a>
Loading