Skip to content
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 @@ -275,7 +275,7 @@ ruleTester.run('exhaustive-deps', rule, {
`,
},
{
name: 'should ignore constants defined out of scope (react component)',
name: 'should ignore constants defined out of scope (react component, function declaration)',
code: `
const CONST_VAL = 1
function MyComponent() {
Expand All @@ -287,7 +287,31 @@ ruleTester.run('exhaustive-deps', rule, {
`,
},
{
name: 'should ignore constants defined out of scope (react hook)',
name: 'should ignore constants defined out of scope (react component, function expression)',
code: `
const CONST_VAL = 1
const MyComponent = () => {
useQuery({
queryKey: ["foo"],
queryFn: () => CONST_VAL
});
}
`,
},
{
name: 'should ignore constants defined out of scope (react component, anonymous function)',
code: `
const CONST_VAL = 1
const MyComponent = function () {
useQuery({
queryKey: ["foo"],
queryFn: () => CONST_VAL
});
}
`,
},
{
name: 'should ignore constants defined out of scope (react hook, function declaration)',
code: `
const CONST_VAL = 1
function useHook() {
Expand All @@ -298,6 +322,30 @@ ruleTester.run('exhaustive-deps', rule, {
}
`,
},
{
name: 'should ignore constants defined out of scope (react hook, function expression)',
code: `
const CONST_VAL = 1
const useHook = () => {
useQuery({
queryKey: ["foo"],
queryFn: () => CONST_VAL
});
}
`,
},
{
name: 'should ignore constants defined out of scope (react hook, anonymous function)',
code: `
const CONST_VAL = 1
const useHook = function () {
useQuery({
queryKey: ["foo"],
queryFn: () => CONST_VAL
});
}
`,
},
{
name: 'should ignore references of the queryClient',
code: `
Expand Down
15 changes: 13 additions & 2 deletions packages/eslint-plugin-query/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,29 @@ export const ASTUtils = {
]),
)
},
isValidReactComponentOrHookName(identifier: TSESTree.Identifier | null) {
return identifier !== null && /^(use|[A-Z])/.test(identifier.name)
},
getReactComponentOrHookAncestor(
context: Readonly<RuleContext<string, readonly unknown[]>>,
) {
return context.getAncestors().find((x) => {
if (
x.type === AST_NODE_TYPES.FunctionDeclaration &&
ASTUtils.isValidReactComponentOrHookName(x.id)
) {
return true
}

return (
x.parent?.type === AST_NODE_TYPES.VariableDeclarator &&
x.parent.id.type === AST_NODE_TYPES.Identifier &&
ASTUtils.isNodeOfOneOf(x, [
AST_NODE_TYPES.FunctionDeclaration,
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.ArrowFunctionExpression,
]) &&
x.id !== null &&
/^(use|[A-Z])/.test(x.id.name)
ASTUtils.isValidReactComponentOrHookName(x.parent.id)
)
}) as
| TSESTree.FunctionDeclaration
Expand Down