-
-
Notifications
You must be signed in to change notification settings - Fork 58
fix(no-navigation-without-resolve): do not report if there is data-sveltekit-reload
or rel="external"
in <a>
#1380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-svelte': patch | ||
--- | ||
|
||
fix(no-navigation-without-resolve): do not report if there is `data-sveltekit-reload` or `rel="external"` in `<a>` |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -95,6 +95,9 @@ export default createRule('no-navigation-without-resolve', { | |||||
) { | ||||||
return; | ||||||
} | ||||||
if (anchorHasSveltekitReload(node) || anchorRelIncludesExternal(node)) { | ||||||
return; | ||||||
} | ||||||
if ( | ||||||
(node.value[0].type === 'SvelteLiteral' && | ||||||
!expressionIsAbsolute(new FindVariableContext(context), node.value[0]) && | ||||||
|
@@ -373,3 +376,43 @@ function templateLiteralIsFragment( | |||||
function urlValueIsFragment(url: string): boolean { | ||||||
return url.startsWith('#'); | ||||||
} | ||||||
|
||||||
function anchorHasSveltekitReload(node: AST.SvelteAttribute): boolean { | ||||||
const startTag = node.parent; | ||||||
return startTag.attributes.some((attr): attr is AST.SvelteAttribute => { | ||||||
return attr.type === 'SvelteAttribute' && attr.key.name === 'data-sveltekit-reload'; | ||||||
}); | ||||||
} | ||||||
|
||||||
function relTokenListIncludesExternal(value: string): boolean { | ||||||
return /(?:^|\s)external(?:\s|$)/i.test(value); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
function anchorRelIncludesExternal(node: AST.SvelteAttribute): boolean { | ||||||
const startTag = node.parent; | ||||||
const relAttr = startTag.attributes.find((attr): attr is AST.SvelteAttribute => { | ||||||
return attr.type === 'SvelteAttribute' && attr.key.name === 'rel'; | ||||||
}); | ||||||
if (!relAttr) return false; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Handle literal values like rel="external" or rel="external nofollow" | ||||||
for (const v of relAttr.value) { | ||||||
if (v.type === 'SvelteLiteral') { | ||||||
if (relTokenListIncludesExternal(v.value)) return true; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merge with parent if |
||||||
} | ||||||
if (v.type === 'SvelteMustacheTag') { | ||||||
// Best-effort: detect simple string literals in mustache, e.g., rel={'external'} | ||||||
const expr = v.expression; | ||||||
if (expr.type === 'Literal' && typeof expr.value === 'string') { | ||||||
if (relTokenListIncludesExternal(expr.value)) return true; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merge with parent if |
||||||
} | ||||||
if ( | ||||||
expr.type === 'TemplateLiteral' && | ||||||
expr.expressions.length === 0 && | ||||||
expr.quasis.length === 1 | ||||||
) { | ||||||
if (relTokenListIncludesExternal(expr.quasis[0].value.raw)) return true; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merge with parent if |
||||||
} | ||||||
} | ||||||
} | ||||||
return false; | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
- message: Found a link with a url that isn't resolved. | ||
- message: Unexpected href link without resolve(). | ||
line: 5 | ||
column: 9 | ||
suggestions: null | ||
- message: Found a link with a url that isn't resolved. | ||
- message: Unexpected href link without resolve(). | ||
line: 6 | ||
column: 9 | ||
suggestions: null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- message: Unexpected href link without resolve(). | ||
line: 1 | ||
column: 28 | ||
suggestions: null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<a rel={'externals'} href="/foo">Click me!</a> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- message: Unexpected href link without resolve(). | ||
line: 1 | ||
column: 28 | ||
suggestions: null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<a rel={`externals`} href="/foo">Click me!</a> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- message: Unexpected href link without resolve(). | ||
line: 1 | ||
column: 26 | ||
suggestions: null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<a rel="externals" href="/foo">Click me!</a> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<a rel="external nofollow" href="/foo">Click me!</a> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<a rel={'external'} href="/foo">Click me!</a> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<a rel={`external`} href="/foo">Click me!</a> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<a rel="external" href="/foo">Click me!</a> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<a data-sveltekit-reload href="/foo">Click me!</a> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add this to the previous if?