[ESLint] Treat functions that don't capture anything as static #14996
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.
The exact heuristics will take me some time to figure out. This is just the first minimal draft.
It lifts the restriction on specifying functions as deps where it's safe. In particular, a function like
would be considered safe because it doesn't capture any render-phase values except knowingly static ones (we only do this if
setCountcomes fromuseStateHook).So we could omit it:
But if we change
tickto beit would warn, asking you to add
tickto dependencies.We only do this check one level deep. So it helps for helper functions that only set state or dispatch, but you might end up in a situation where you can't add a prop to one of them without triggering warnings for all effects using it. Ideally we might want to push you to
useReducera bit sooner but I haven't figured out what a good flow is.This PR makes the rule strictly more relaxed. It doesn't add any new behaviors, it just fires the warnings less often in cases where it's safe.
In follow-ups, we'll need to tweak the behavior and warnings to be more useful for functions. Such as we might want to detect the above special case and suggest the updater form if it would fix the warning. Similarly, we could detect always-new deps (like
tick) and in that case suggest wrapping inuseCallback.