-
-
Notifications
You must be signed in to change notification settings - Fork 58
feat(no-navigation-without-resolve): add allowSuffix
option
#1381
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
Draft
baseballyama
wants to merge
1
commit into
main
Choose a base branch
from
fix/no-navigation-without-resolve-search-param
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-svelte': patch | ||
--- | ||
|
||
feat(no-navigation-without-resolve): add `allowSuffix` option. This option disables error reporting when there is a suffix such as query parameters after the `resolve()` function. The default is `true`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import type { TSESTree } from '@typescript-eslint/types'; | ||
import { createRule } from '../utils/index.js'; | ||
import type { TrackedReferences } from '@eslint-community/eslint-utils'; | ||
import { ReferenceTracker } from '@eslint-community/eslint-utils'; | ||
import { FindVariableContext } from '../utils/ast-utils.js'; | ||
import { findVariable } from '../utils/ast-utils.js'; | ||
|
@@ -29,6 +30,9 @@ export default createRule('no-navigation-without-resolve', { | |
}, | ||
ignoreReplaceState: { | ||
type: 'boolean' | ||
}, | ||
allowSuffix: { | ||
type: 'boolean' | ||
} | ||
}, | ||
additionalProperties: false | ||
|
@@ -49,10 +53,14 @@ export default createRule('no-navigation-without-resolve', { | |
}, | ||
create(context) { | ||
let resolveReferences: Set<TSESTree.Identifier> = new Set<TSESTree.Identifier>(); | ||
let assetReferences: Set<TSESTree.Identifier> = new Set<TSESTree.Identifier>(); | ||
return { | ||
Program() { | ||
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope!); | ||
resolveReferences = extractResolveReferences(referenceTracker, context); | ||
({ resolve: resolveReferences, asset: assetReferences } = extractResolveReferences( | ||
referenceTracker, | ||
context | ||
)); | ||
const { | ||
goto: gotoCalls, | ||
pushState: pushStateCalls, | ||
|
@@ -102,10 +110,16 @@ export default createRule('no-navigation-without-resolve', { | |
(node.value[0].type === 'SvelteMustacheTag' && | ||
!expressionIsAbsolute(new FindVariableContext(context), node.value[0].expression) && | ||
!expressionIsFragment(new FindVariableContext(context), node.value[0].expression) && | ||
!isResolveCall( | ||
!isResolveWithOptionalSuffix( | ||
new FindVariableContext(context), | ||
node.value[0].expression, | ||
resolveReferences, | ||
context.options[0]?.allowSuffix !== false | ||
) && | ||
!isAssetOnly( | ||
new FindVariableContext(context), | ||
node.value[0].expression, | ||
resolveReferences | ||
assetReferences | ||
)) | ||
) { | ||
context.report({ loc: node.value[0].loc, messageId: 'linkWithoutResolve' }); | ||
|
@@ -120,9 +134,10 @@ export default createRule('no-navigation-without-resolve', { | |
function extractResolveReferences( | ||
referenceTracker: ReferenceTracker, | ||
context: RuleContext | ||
): Set<TSESTree.Identifier> { | ||
const set = new Set<TSESTree.Identifier>(); | ||
for (const { node } of referenceTracker.iterateEsmReferences({ | ||
): { resolve: Set<TSESTree.Identifier>; asset: Set<TSESTree.Identifier> } { | ||
const resolveSet = new Set<TSESTree.Identifier>(); | ||
const assetSet = new Set<TSESTree.Identifier>(); | ||
for (const { node, path } of referenceTracker.iterateEsmReferences({ | ||
'$app/paths': { | ||
[ReferenceTracker.ESM]: true, | ||
asset: { | ||
|
@@ -139,17 +154,16 @@ function extractResolveReferences( | |
continue; | ||
} | ||
for (const reference of variable.references) { | ||
if (reference.identifier.type === 'Identifier') set.add(reference.identifier); | ||
if (reference.identifier.type !== 'Identifier') continue; | ||
if (path[path.length - 1] === 'resolve') resolveSet.add(reference.identifier); | ||
if (path[path.length - 1] === 'asset') assetSet.add(reference.identifier); | ||
} | ||
} else if ( | ||
node.type === 'MemberExpression' && | ||
node.property.type === 'Identifier' && | ||
node.property.name === 'resolve' | ||
) { | ||
set.add(node.property); | ||
} else if (node.type === 'MemberExpression' && node.property.type === 'Identifier') { | ||
if (node.property.name === 'resolve') resolveSet.add(node.property); | ||
if (node.property.name === 'asset') assetSet.add(node.property); | ||
} | ||
} | ||
return set; | ||
return { resolve: resolveSet, asset: assetSet }; | ||
} | ||
|
||
// Extract all references to goto, pushState and replaceState | ||
|
@@ -175,16 +189,21 @@ function extractFunctionCallReferences(referenceTracker: ReferenceTracker): { | |
} | ||
}) | ||
); | ||
|
||
function onlyCallExpressions(list: TrackedReferences<boolean>[]): TSESTree.CallExpression[] { | ||
return list | ||
.filter((r) => r.node.type === 'CallExpression') | ||
.map((r) => r.node as TSESTree.CallExpression); | ||
} | ||
|
||
return { | ||
goto: rawReferences | ||
.filter(({ path }) => path[path.length - 1] === 'goto') | ||
.map(({ node }) => node as TSESTree.CallExpression), | ||
pushState: rawReferences | ||
.filter(({ path }) => path[path.length - 1] === 'pushState') | ||
.map(({ node }) => node as TSESTree.CallExpression), | ||
replaceState: rawReferences | ||
.filter(({ path }) => path[path.length - 1] === 'replaceState') | ||
.map(({ node }) => node as TSESTree.CallExpression) | ||
goto: onlyCallExpressions(rawReferences.filter(({ path }) => path[path.length - 1] === 'goto')), | ||
pushState: onlyCallExpressions( | ||
rawReferences.filter(({ path }) => path[path.length - 1] === 'pushState') | ||
), | ||
replaceState: onlyCallExpressions( | ||
rawReferences.filter(({ path }) => path[path.length - 1] === 'replaceState') | ||
) | ||
}; | ||
} | ||
|
||
|
@@ -199,7 +218,14 @@ function checkGotoCall( | |
return; | ||
} | ||
const url = call.arguments[0]; | ||
if (!isResolveCall(new FindVariableContext(context), url, resolveReferences)) { | ||
if ( | ||
!isResolveWithOptionalSuffix( | ||
new FindVariableContext(context), | ||
url, | ||
resolveReferences, | ||
context.options[0]?.allowSuffix !== false | ||
) | ||
) { | ||
context.report({ loc: url.loc, messageId: 'gotoWithoutResolve' }); | ||
} | ||
} | ||
|
@@ -216,7 +242,12 @@ function checkShallowNavigationCall( | |
const url = call.arguments[0]; | ||
if ( | ||
!expressionIsEmpty(url) && | ||
!isResolveCall(new FindVariableContext(context), url, resolveReferences) | ||
!isResolveWithOptionalSuffix( | ||
new FindVariableContext(context), | ||
url, | ||
resolveReferences, | ||
context.options[0]?.allowSuffix !== false | ||
) | ||
) { | ||
context.report({ loc: url.loc, messageId }); | ||
} | ||
|
@@ -253,6 +284,90 @@ function isResolveCall( | |
return isResolveCall(ctx, variable.identifiers[0].parent.init, resolveReferences); | ||
} | ||
|
||
function isResolveWithOptionalSuffix( | ||
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. 2 notes on the design:
|
||
ctx: FindVariableContext, | ||
node: TSESTree.Expression | TSESTree.CallExpressionArgument, | ||
resolveReferences: Set<TSESTree.Identifier>, | ||
allowSuffix: boolean | ||
): boolean { | ||
if ( | ||
(node.type === 'CallExpression' || node.type === 'Identifier') && | ||
isResolveCall(ctx, node, resolveReferences) | ||
) { | ||
return true; | ||
} | ||
|
||
if (!allowSuffix) return false; | ||
return expressionStartsWithResolve(ctx, node, resolveReferences); | ||
} | ||
|
||
function expressionStartsWithResolve( | ||
ctx: FindVariableContext, | ||
node: TSESTree.Expression | TSESTree.CallExpressionArgument, | ||
resolveReferences: Set<TSESTree.Identifier> | ||
): boolean { | ||
// Direct call | ||
if (node.type === 'CallExpression') { | ||
return isResolveCall(ctx, node, resolveReferences); | ||
} | ||
// Binary chain: ensure the left-most operand is resolve(); any right-hand content is allowed | ||
if (node.type === 'BinaryExpression') { | ||
if (node.operator !== '+' || node.left.type === 'PrivateIdentifier') return false; | ||
return expressionStartsWithResolve(ctx, node.left, resolveReferences); | ||
} | ||
// Template literal: must start with expression and that expression starts with resolve(); content after is allowed | ||
if (node.type === 'TemplateLiteral') { | ||
if ( | ||
node.expressions.length === 0 || | ||
(node.quasis.length >= 1 && node.quasis[0].value.raw !== '') | ||
) | ||
return false; | ||
return expressionStartsWithResolve(ctx, node.expressions[0], resolveReferences); | ||
} | ||
// Identifier indirection | ||
if (node.type === 'Identifier') { | ||
const variable = ctx.findVariable(node); | ||
if ( | ||
variable === null || | ||
variable.identifiers.length === 0 || | ||
variable.identifiers[0].parent.type !== 'VariableDeclarator' || | ||
variable.identifiers[0].parent.init === null | ||
) { | ||
return false; | ||
} | ||
return expressionStartsWithResolve(ctx, variable.identifiers[0].parent.init, resolveReferences); | ||
} | ||
return false; | ||
} | ||
|
||
function isAssetOnly( | ||
ctx: FindVariableContext, | ||
node: TSESTree.Expression | TSESTree.CallExpressionArgument, | ||
assetReferences: Set<TSESTree.Identifier> | ||
): boolean { | ||
if (node.type === 'CallExpression') { | ||
return ( | ||
(node.callee.type === 'Identifier' && assetReferences.has(node.callee)) || | ||
(node.callee.type === 'MemberExpression' && | ||
node.callee.property.type === 'Identifier' && | ||
assetReferences.has(node.callee.property)) | ||
); | ||
} | ||
if (node.type === 'Identifier') { | ||
const variable = ctx.findVariable(node); | ||
if ( | ||
variable === null || | ||
variable.identifiers.length === 0 || | ||
variable.identifiers[0].parent.type !== 'VariableDeclarator' || | ||
variable.identifiers[0].parent.init === null | ||
) { | ||
return false; | ||
} | ||
return isAssetOnly(ctx, variable.identifiers[0].parent.init, assetReferences); | ||
} | ||
return false; | ||
} | ||
|
||
function expressionIsEmpty(url: TSESTree.CallExpressionArgument): boolean { | ||
return ( | ||
(url.type === 'Literal' && url.value === '') || | ||
|
7 changes: 7 additions & 0 deletions
7
...s/fixtures/rules/no-navigation-without-resolve/invalid/goto-partial-resolve01-config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"options": [ | ||
{ | ||
"allowSuffix": false | ||
} | ||
] | ||
} |
4 changes: 2 additions & 2 deletions
4
...sts/fixtures/rules/no-navigation-without-resolve/invalid/link-partial-asset01-errors.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
7 changes: 7 additions & 0 deletions
7
...s/fixtures/rules/no-navigation-without-resolve/invalid/link-partial-resolve01-config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"options": [ | ||
{ | ||
"allowSuffix": false | ||
} | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
...tures/rules/no-navigation-without-resolve/invalid/pushState-partial-resolve01-config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"options": [ | ||
{ | ||
"allowSuffix": false | ||
} | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
...es/rules/no-navigation-without-resolve/invalid/replaceState-partial-resolve01-config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"options": [ | ||
{ | ||
"allowSuffix": false | ||
} | ||
] | ||
} |
9 changes: 9 additions & 0 deletions
9
...-svelte/tests/fixtures/rules/no-navigation-without-resolve/valid/allowSuffix/_config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"options": [ | ||
{ | ||
"allowSuffix": true | ||
} | ||
] | ||
} | ||
|
||
|
12 changes: 12 additions & 0 deletions
12
...es/rules/no-navigation-without-resolve/valid/allowSuffix/goto-allow-suffix01-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script> | ||
import { resolve } from '$app/paths'; | ||
import { goto } from '$app/navigation'; | ||
|
||
goto(resolve('/foo') + '?q=1'); | ||
goto(`${resolve('/bar')}&p=2`); | ||
|
||
const base = resolve('/baz'); | ||
goto(base + '#frag'); | ||
</script> | ||
|
||
|
11 changes: 11 additions & 0 deletions
11
...es/rules/no-navigation-without-resolve/valid/allowSuffix/link-allow-suffix01-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script> | ||
import { resolve } from '$app/paths'; | ||
|
||
const base = resolve('/foo'); | ||
</script> | ||
|
||
<a href={resolve('/foo') + '?q=1'}>Click me!</a> | ||
<a href={`${resolve('/bar')}#frag`}>Click me!</a> | ||
<a href={base + '&x=1'}>Click me!</a> | ||
|
||
|
10 changes: 10 additions & 0 deletions
10
...les/no-navigation-without-resolve/valid/allowSuffix/pushState-allow-suffix01-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<script> | ||
import { resolve } from '$app/paths'; | ||
import { pushState } from '$app/navigation'; | ||
|
||
pushState(resolve('/foo') + '?q=1'); | ||
pushState(`${resolve('/bar')}#frag`); | ||
|
||
const base = resolve('/baz'); | ||
pushState(base + '&x=1'); | ||
</script> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you please explain why this is needed? I don't get it :/