[ESLint] Deduplicate suggested dependencies#14982
Merged
gaearon merged 2 commits intofacebook:masterfrom Mar 1, 2019
Merged
Conversation
Details of bundled changes.Comparing: 02404d7...bff43db eslint-plugin-react-hooks
Generated by 🚫 dangerJS |
bvaughn
approved these changes
Feb 28, 2019
| }, [props.foo.bar.baz]); | ||
| } | ||
| `, | ||
| }, |
| function MyComponent(props) { | ||
| const fn = useCallback(() => { | ||
| console.log(props.foo.bar.baz); | ||
| }, [props.foo]); |
Contributor
There was a problem hiding this comment.
I guess this is the right approach, since we kind of have to assume immutability for much of this stuff to work. Seems like it's at least possible that props.foo.bar.baz was specified because foo gets mutated, but I guess we just don't support that case.
Collaborator
Author
There was a problem hiding this comment.
Yeah. (This was already the case before this PR btw.)
Contributor
There was a problem hiding this comment.
Yeah, this particular before and after example just made me think about it more closely :)
| props.onChange(); | ||
| } | ||
| }, [props, props.onChange]); | ||
| }, [props]); |
| })); | ||
| } | ||
|
|
||
| // Alphabetize the suggestions, but only if deps were already alphabetized. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 previous algorithm was pretty ad-hoc which resulted in edge cases like when it suggested
[props, props.onChange]when[props]alone would suffice. This is a rewrite of it that instead maintains a data structure representing property chains. We mark the nodes that are required (i.e. used in the code), and then mark the nodes that are satisfied (i.e. specified in the dependency list).That lets us precisely calculate the minimal necessary deps. (However, for effects we still allow extraneous deps when they’re already specified.) It should also open the door to making the rule smarter in the future (e.g. we should be able to extend this logic to suggest a parent object if we use >5 its properties or similar).
See inline comments and new tests.