Skip to content

Commit

Permalink
[POC] Warn on constant constructions coming from default params
Browse files Browse the repository at this point in the history
This seems to work, but we would probably want to improve the error message for this case.
  • Loading branch information
captbaritone committed Aug 14, 2020
1 parent 1d5e10f commit 29d560b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7518,6 +7518,42 @@ const tests = {
},
],
},
{
only: true,
code: normalizeIndent`
function Foo({foo = []}) {
useEffect(() => {
console.log(foo);
}, [foo]);
}
`,
errors: [
{
message:
"The 'foo' array makes the dependencies of useEffect Hook (at line 5) change on every render. " +
"Move it inside the useEffect callback. Alternatively, wrap the initialization of 'foo' in its own useMemo() Hook.",
suggestions: undefined,
},
],
},
{
only: true,
code: normalizeIndent`
function useFoo(foo = []) {
useEffect(() => {
console.log(foo);
}, [foo]);
}
`,
errors: [
{
message:
"The 'foo' array makes the dependencies of useEffect Hook (at line 5) change on every render. " +
"Move it inside the useEffect callback. Alternatively, wrap the initialization of 'foo' in its own useMemo() Hook.",
suggestions: undefined,
},
],
},
],
};

Expand Down
16 changes: 16 additions & 0 deletions packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -1474,11 +1474,27 @@ function scanForConstructions({
if (node == null) {
return null;
}

// function(foo = []) {}
// function({foo = []}) {}
if (
node.type === 'Parameter' &&
node.name.parent.type === 'AssignmentPattern'
) {
const constantExpressionType = getConstructionExpressionType(
node.name.parent.right,
);
if (constantExpressionType != null) {
return [ref, constantExpressionType];
}
}

// const handleChange = function () {}
// const handleChange = () => {}
// const foo = {}
// const foo = []
// etc.

if (
node.type === 'Variable' &&
node.node.type === 'VariableDeclarator' &&
Expand Down

0 comments on commit 29d560b

Please sign in to comment.