Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reference not add to dependencies if only its type is used as typeof arg #19316

Merged
merged 2 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6983,6 +6983,30 @@ const testsTypescript = {
}
`,
},
{
code: normalizeIndent`
function MyComponent() {
const [state, setState] = React.useState<number>(0);

useEffect(() => {
const someNumber: typeof state = 2;
setState(prevState => prevState + someNumber);
}, [])
}
`,
},
{
code: normalizeIndent`
function App() {
const foo = {x: 1};
React.useEffect(() => {
const bar = {x: 2};
const baz = bar as typeof foo;
console.log(baz);
}, []);
}
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -7016,6 +7040,40 @@ const testsTypescript = {
},
],
},
{
code: normalizeIndent`
function App() {
const foo = {x: 1};
const bar = {x: 2};
useEffect(() => {
const baz = bar as typeof foo;
console.log(baz);
}, []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'bar'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [bar]',
output: normalizeIndent`
function App() {
const foo = {x: 1};
const bar = {x: 2};
useEffect(() => {
const baz = bar as typeof foo;
console.log(baz);
}, [bar]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
Expand Down Expand Up @@ -7217,6 +7275,76 @@ const testsTypescript = {
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const [state, setState] = React.useState<number>(0);

useEffect(() => {
const someNumber: typeof state = 2;
setState(prevState => prevState + someNumber + state);
}, [])
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'state'. " +
'Either include it or remove the dependency array. ' +
`You can also do a functional update 'setState(s => ...)' ` +
`if you only need 'state' in the 'setState' call.`,
suggestions: [
{
desc: 'Update the dependencies array to be: [state]',
output: normalizeIndent`
function MyComponent() {
const [state, setState] = React.useState<number>(0);

useEffect(() => {
const someNumber: typeof state = 2;
setState(prevState => prevState + someNumber + state);
}, [state])
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const [state, setState] = React.useState<number>(0);

useMemo(() => {
const someNumber: typeof state = 2;
console.log(someNumber);
}, [state])
}
`,
errors: [
{
message:
"React Hook useMemo has an unnecessary dependency: 'state'. " +
'Either exclude it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: []',
output: normalizeIndent`
function MyComponent() {
const [state, setState] = React.useState<number>(0);

useMemo(() => {
const someNumber: typeof state = 2;
console.log(someNumber);
}, [])
}
`,
},
],
},
],
},
],
};

Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,10 @@ export default {
});
}

if (dependencyNode.parent.type === 'TSTypeQuery') {
continue;
}

const def = reference.resolved.defs[0];
if (def == null) {
continue;
Expand Down