Skip to content

Commit

Permalink
foo is enough for both foo.bar and foo.baz
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Jan 11, 2019
1 parent 35e19ed commit dc9bb2e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ eslintTester.run('react-hooks', ReactHooksESLintRule, {
}, [foo]);
}
`,
`
// Regression test
function MyComponent({ foo }) {
useEffect(() => {
console.log(foo.length);
console.log(foo.slice(0));
}, [foo]);
}
`,
`
// Regression test
function MyComponent({ history }) {
Expand Down Expand Up @@ -169,6 +178,19 @@ eslintTester.run('react-hooks', ReactHooksESLintRule, {
}, [props.foo, props.bar, local]);
}
`,
{
code: `
// TODO: we might want to warn "props.foo"
// is extraneous because we already have "props".
function MyComponent(props) {
const local = 42;
useEffect(() => {
console.log(props.foo);
console.log(props.bar);
}, [props, props.foo]);
}
`,
},
{
code: `
function MyComponent(props) {
Expand Down Expand Up @@ -492,6 +514,19 @@ eslintTester.run('react-hooks', ReactHooksESLintRule, {
missingError('local'),
],
},
{
code: `
function MyComponent(props) {
const local = 42;
useEffect(() => {
console.log(props.foo);
console.log(props.bar);
console.log(local);
}, [props]);
}
`,
errors: [missingError('local')],
},
{
code: `
function MyComponent(props) {
Expand Down
19 changes: 12 additions & 7 deletions packages/eslint-plugin-react-hooks/src/ReactiveDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ export default {
}
}

let usedDependencies = new Set();

// Loop through all our dependencies to make sure they have been declared.
// If the dependency has not been declared we need to report some errors.
for (const [dependency, dependencyNodes] of dependencies) {
Expand All @@ -281,11 +283,10 @@ export default {
let candidate = dependency;
while (candidate) {
if (declaredDependencies.has(candidate)) {
// Yay! Our dependency has been declared. Delete it from
// `declaredDependencies` since we will report all remaining unused
// declared dependencies.
// Yay! Our dependency has been declared.
// Record this so we don't report is unused.
isDeclared = true;
declaredDependencies.delete(candidate);
usedDependencies.add(candidate);
break;
}
const lastDotAccessIndex = candidate.lastIndexOf('.');
Expand Down Expand Up @@ -316,11 +317,15 @@ export default {

// Loop through all the unused declared dependencies and report a warning
// so the programmer removes the unused dependency from their list.
for (const declaredDependencyNode of declaredDependencies.values()) {
for (const [dependency, dependencyNode] of declaredDependencies) {
if (usedDependencies.has(dependency)) {
continue;
}

context.report(
declaredDependencyNode,
dependencyNode,
`React Hook ${context.getSource(reactiveHook)} has an extra ` +
`dependency "${context.getSource(declaredDependencyNode)}" which ` +
`dependency "${context.getSource(dependencyNode)}" which ` +
'is not used in its callback. Removing this dependency may mean ' +
'the hook needs to execute fewer times.',
);
Expand Down

0 comments on commit dc9bb2e

Please sign in to comment.