Skip to content

Commit

Permalink
fix(eslint-plugin-react-hooks): Support optional chaining when access…
Browse files Browse the repository at this point in the history
…ing prototype method facebook#19061
  • Loading branch information
fredvollmer committed Jun 1, 2020
1 parent 4c7036e commit fb8040d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,24 @@ const tests = {
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
useCallback(() => {
console.log(props.foo?.x.y.toString());
}, [props.foo?.x.y]);
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
useCallback(() => {
console.log(props.foo?.toString());
}, [props.foo]);
}
`,
},
{
code: normalizeIndent`
function MyComponent() {
Expand Down Expand Up @@ -1221,6 +1239,34 @@ const tests = {
},
],
invalid: [
{
code: normalizeIndent`
function MyComponent(props) {
useCallback(() => {
console.log(props.foo?.toString());
}, []);
}
`,
errors: [
{
message:
"React Hook useCallback has a missing dependency: 'props.foo?.toString'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [props.foo?.toString]',
output: normalizeIndent`
function MyComponent(props) {
useCallback(() => {
console.log(props.foo?.toString());
}, [props.foo?.toString]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
Expand Down
4 changes: 3 additions & 1 deletion packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,9 @@ function collectRecommendations({
if (child.isSatisfiedRecursively) {
if (child.hasRequiredNodesBelow) {
// Remember this dep actually satisfied something.
satisfyingPaths.add(path);
// Here we only want to compare the "normalized" path, without any optional chaining ("?.") operator
// foo?.bar -> foo.bar
satisfyingPaths.add(path.replace(/\?$/, ""));
}
// It doesn't matter if there's something deeper.
// It would be transitively satisfied since we assume immutability.
Expand Down

0 comments on commit fb8040d

Please sign in to comment.