From 62b35b7f48d469eaa5b0100457fac5a28e0d2430 Mon Sep 17 00:00:00 2001 From: Newbie012 Date: Thu, 3 Nov 2022 22:21:21 +0200 Subject: [PATCH 1/4] fix unary expression --- .../src/rules/exhaustive-deps/exhaustive-deps.test.ts | 8 ++++++++ packages/eslint-plugin-query/src/utils/ast-utils.ts | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts b/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts index 9f199fe3e7..f58d645099 100644 --- a/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts +++ b/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts @@ -46,6 +46,14 @@ ruleTester.run('exhaustive-deps', rule, { } `, }, + { + name: 'identify !!props.id (unary expression)', + code: ` + function MyComponent(props) { + useQuery({ queryKey: ["entity", !!props.id], queryFn: () => api.entity.get(props.id) }); + } + `, + }, { name: 'should ignore keys from callback', code: ` diff --git a/packages/eslint-plugin-query/src/utils/ast-utils.ts b/packages/eslint-plugin-query/src/utils/ast-utils.ts index dc16fe4e9f..2dc734a1ea 100644 --- a/packages/eslint-plugin-query/src/utils/ast-utils.ts +++ b/packages/eslint-plugin-query/src/utils/ast-utils.ts @@ -67,6 +67,10 @@ export const ASTUtils = { identifiers.push(...ASTUtils.getNestedIdentifiers(node.object)) } + if (node.type === AST_NODE_TYPES.UnaryExpression) { + identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument)) + } + return identifiers }, isAncestorIsCallee(identifier: TSESTree.Node) { From 830184acf5edc0e61bfbb5efe5be50886edacd65 Mon Sep 17 00:00:00 2001 From: Newbie012 Date: Thu, 3 Nov 2022 22:21:46 +0200 Subject: [PATCH 2/4] fix chain expression --- .../src/rules/exhaustive-deps/exhaustive-deps.test.ts | 8 ++++++++ packages/eslint-plugin-query/src/utils/ast-utils.ts | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts b/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts index f58d645099..a225bfb692 100644 --- a/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts +++ b/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts @@ -54,6 +54,14 @@ ruleTester.run('exhaustive-deps', rule, { } `, }, + { + name: 'identify props?.id (chain expression)', + code: ` + function MyComponent(props) { + useQuery({ queryKey: ["entity", props?.id], queryFn: () => api.entity.get(props?.id) }); + } + `, + }, { name: 'should ignore keys from callback', code: ` diff --git a/packages/eslint-plugin-query/src/utils/ast-utils.ts b/packages/eslint-plugin-query/src/utils/ast-utils.ts index 2dc734a1ea..76c9c09c95 100644 --- a/packages/eslint-plugin-query/src/utils/ast-utils.ts +++ b/packages/eslint-plugin-query/src/utils/ast-utils.ts @@ -71,6 +71,10 @@ export const ASTUtils = { identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument)) } + if (node.type === AST_NODE_TYPES.ChainExpression) { + identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression)) + } + return identifiers }, isAncestorIsCallee(identifier: TSESTree.Node) { From 3b7bc1c705b2a16885a8ea578510f4ed910b9ada Mon Sep 17 00:00:00 2001 From: Newbie012 Date: Thu, 3 Nov 2022 22:23:52 +0200 Subject: [PATCH 3/4] fix ts non null expression --- .../src/rules/exhaustive-deps/exhaustive-deps.test.ts | 9 +++++++++ packages/eslint-plugin-query/src/utils/ast-utils.ts | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts b/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts index a225bfb692..9c3ab957f6 100644 --- a/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts +++ b/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts @@ -62,6 +62,15 @@ ruleTester.run('exhaustive-deps', rule, { } `, }, + { + only: true, + name: 'identify props!.id (ts non null expression)', + code: ` + function MyComponent(props) { + useQuery({ queryKey: ["entity", props!.id], queryFn: () => api.entity.get(props!.id) }); + } + `, + }, { name: 'should ignore keys from callback', code: ` diff --git a/packages/eslint-plugin-query/src/utils/ast-utils.ts b/packages/eslint-plugin-query/src/utils/ast-utils.ts index 76c9c09c95..f0223f3db2 100644 --- a/packages/eslint-plugin-query/src/utils/ast-utils.ts +++ b/packages/eslint-plugin-query/src/utils/ast-utils.ts @@ -75,6 +75,10 @@ export const ASTUtils = { identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression)) } + if (node.type === AST_NODE_TYPES.TSNonNullExpression) { + identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression)) + } + return identifiers }, isAncestorIsCallee(identifier: TSESTree.Node) { From 3e16948add82a2c590b44cf3e311a7b7ad9473f7 Mon Sep 17 00:00:00 2001 From: Newbie012 Date: Thu, 3 Nov 2022 22:31:40 +0200 Subject: [PATCH 4/4] format --- packages/eslint-plugin-query/src/utils/ast-utils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin-query/src/utils/ast-utils.ts b/packages/eslint-plugin-query/src/utils/ast-utils.ts index f0223f3db2..6badbd8a98 100644 --- a/packages/eslint-plugin-query/src/utils/ast-utils.ts +++ b/packages/eslint-plugin-query/src/utils/ast-utils.ts @@ -68,15 +68,15 @@ export const ASTUtils = { } if (node.type === AST_NODE_TYPES.UnaryExpression) { - identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument)) + identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument)) } if (node.type === AST_NODE_TYPES.ChainExpression) { - identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression)) + identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression)) } if (node.type === AST_NODE_TYPES.TSNonNullExpression) { - identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression)) + identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression)) } return identifiers