Skip to content

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Feb 28, 2019

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.

@sizebot
Copy link

sizebot commented Feb 28, 2019

Details of bundled changes.

Comparing: 02404d7...bff43db

eslint-plugin-react-hooks

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
eslint-plugin-react-hooks.development.js +11.9% +10.9% 49.71 KB 55.61 KB 11.73 KB 13 KB NODE_DEV
eslint-plugin-react-hooks.production.min.js 🔺+12.1% 🔺+7.7% 12.37 KB 13.86 KB 4.47 KB 4.81 KB NODE_PROD
ESLintPluginReactHooks-dev.js +11.1% +10.6% 53.24 KB 59.16 KB 12.06 KB 13.34 KB FB_WWW_DEV

Generated by 🚫 dangerJS

}, [props.foo.bar.baz]);
}
`,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test! 👍

function MyComponent(props) {
const fn = useCallback(() => {
console.log(props.foo.bar.baz);
}, [props.foo]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. (This was already the case before this PR btw.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this particular before and after example just made me think about it more closely :)

output: `
function MyComponent(props) {
useEffect(() => {
if (props.onChange) {
props.onChange();
}
}, [props, props.onChange]);
}, [props]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

}));
}

// Alphabetize the suggestions, but only if deps were already alphabetized.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@gaearon gaearon merged commit df7b476 into facebook:master Mar 1, 2019
@gaearon gaearon deleted the eslint-refactor branch March 1, 2019 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants