-
-
Notifications
You must be signed in to change notification settings - Fork 56
Preventing infinite loops #1338
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
Closed
marekdedic
wants to merge
9
commits into
sveltejs:main
from
marekdedic:consistent-selector-style-infinite-loop-fix
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8845b94
chore: added an AST search helper that prevents infinite loops
marekdedic 189cef5
fix(consistent-selector-style): fixed an infinite loop
marekdedic b02ec93
fix(no-dynamic-slot-name): fixed an infinite loop
marekdedic 9da2648
chore: migrated custom code in getStringIfConstant to ASTSearchHelper
marekdedic c2d7877
fix(no-navigation-without-base): fixed an infinite loop
marekdedic 41f9b02
chore: extracted URL utils and fixed an infinite loop
marekdedic fd830ee
fix(no-navigation-without-resolve): fixed an infinite loop
marekdedic cd346c0
test(no-navigation-without-resolve): testing for infinite recursion
marekdedic 407a1c8
chore: migrated custom code in getSimpleNameFromNode to ASTSearchHelper
marekdedic 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
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 | ||
--- | ||
|
||
fix: preventing infinite loops in multiple rules |
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
29 changes: 29 additions & 0 deletions
29
packages/eslint-plugin-svelte/src/utils/ast-search-helper.ts
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,29 @@ | ||
import type { TSESTree } from '@typescript-eslint/types'; | ||
import type { AST } from 'svelte-eslint-parser'; | ||
|
||
type ASTNode = AST.SvelteNode | TSESTree.Node; | ||
type NodeResolvers<T> = { | ||
[K in ASTNode['type']]?: ( | ||
node: ASTNode & { type: K }, | ||
searchAnotherNode: (node: ASTNode) => T | null | ||
) => T | null; | ||
}; | ||
|
||
export function ASTSearchHelper<T>(startNode: ASTNode, nodeResolvers: NodeResolvers<T>): T | null { | ||
const visitedNodes = new Set<ASTNode>(); | ||
|
||
function searchNode(node: ASTNode): T | null { | ||
if (!(node.type in nodeResolvers) || visitedNodes.has(node)) { | ||
return null; | ||
} | ||
visitedNodes.add(node); | ||
return ( | ||
nodeResolvers[node.type] as ( | ||
node: ASTNode, | ||
searchAnotherNode: (node: ASTNode) => T | null | ||
) => T | null | ||
)(node, searchNode); | ||
} | ||
|
||
return searchNode(startNode); | ||
} |
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.
What is the use of this function? I don't understand it. Can you explain?
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.
svelte/no-dynamic-slot-name
now causes an error.https://eslint-online-playground.netlify.app/#eNq1VM1u00AQfpVhTxDFXgkkQG6KUmjhQgkS3DBCxh4bp/autbspiaI8BQcuPB1PwqzXf6mtIrXqJfHOfN/Mfp9nvGdaxfxy90aWlRQojK+vsTDIArbQscor8yoUAHwGqItcGHBpLqSX7ERU5rGnC2k8esQAQoZKSRUymHFLi6XQBlI4hU/vV5+/fTi7vDjp412M8inFF7zrSIdHngd///yCd6vVOXgeBRe2E9hOpyHLpEyoDx+Cf8PrszF23/U5EJzNmVPi0yXSPPPXmrTmpF4ZWGtIlSxJx9KB+FqHjK7W5J34j8Umy0WLdECvqoOeQ0xwIqVRtRwX81pqnRtyjG7cbvBmV6HzpuHUYNzW4ATTaFP0pEbYY2v0Wjcn7SuMZVmiSDCZ25Tv+zcYk5ih5Bb3JWRpERk+gIfsa83Y2x+ANC9QB0DA2YzPmiIhm5OW/uQYAEUksk2U4aoyOQ1G0BYBcM4ERx42pDY5JvW0QQjIHjufy5GVvLW/KwxwGDzj1qjoLem52BoU2jUjYSMZPe1g/9zhNj/I/2NL6sCDu9K9dRe48+3tahzd3gbudfuu85MTWtMqiq+oApWVgla0LuBWIWT2VZYy2RR2qFwiwetzrOwwijhHukrX83ibLZWGFzUtUdO3XeJbkzc33EJ/GFPpgPPqKvMr5Qv8yafAy5dPvz/DF88HNYdVRg2nPw+T0H6c/wvoPh1DXO14KA7s8A+2f/5/
It worked fine before.
https://eslint-online-playground.netlify.app/#eNqNUsFO3DAQ/ZWpj9E6vqdLVVqqXkq3UrlhhKJkEnlx7Mj2IlarfAUHLnwdX8I4TpZdQIhLYs97b2bejHfMu0qcb3/arrcGTcj9LeqArGBLXznVh2/SAIgM0GtlAiRYGMvrrSk7VXGvbeB0xAIkQ+eskwwyEWWVNT5AAyfw/8/q4vrv6fmvry/xfYzwhuJLsa9Ily+cw9PjPfxerc6AcwouYyWIlU4ka62tqY44JD/Aj9O33N2+zkB0tmDJSU5NNKrN1568KnLvAqw9NM525ON7Iom1l4xam/Bk/p/etMrMzETk/RjkifGOpnQe3axJMT5LR2zU4N2oqbEpNzrAZRzW2k+t+txhZbsOTY31IkJ5Pq0rtTTzLiVrdBnEAV2yq1Gxix+ARmn0BeVnWSayKQmNQrIFtXcUSDoAXZp2U7a46oOi9RVzKoDUf3HkdBIN8TfQ5YrM0ej7srqhHJTYGhr7mEKysO1pZPH5dLbeaDqPcslqvD3DPhowlUJqZl/1eENRSobRh0kaCQn/EHy9tXepnwBfb/KQOk5AmoENzxpgJqo=
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.
This function is the core of this PR. It abstracts away a common pattern where you search through the AST and depending on the node you return some value or look in another node (usually parent, child, but also e.g. variable definition).
As we noticed in the linked PR, there is an issue in both
consistent-selector-style
as well asno-navigation-without-resolve
where you could get an infinite recursion in the rule code.This function prevents infinite loops as well. The pattern seemed to repeat in multiple places in the codebase, so I created this abstraction.
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.
Re: no-dynamic-slot-name - Ugh, that's not in the tests, could you add it? The playground doesn't work for me.
Since the rule is deprecated, my first choice would be to leave it as it is and remove the change to this rule, what do you think?
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.
So that's not working right now, right?
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.
I think it would be a good idea to refactor the deprecation rules as well, to test whether the new functions work well.
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.
Try testing it with the following file:
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.
Thanks, with the test case I was able to fix that rule as well :)