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..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 @@ -46,6 +46,31 @@ 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: 'identify props?.id (chain expression)', + code: ` + function MyComponent(props) { + useQuery({ queryKey: ["entity", props?.id], queryFn: () => api.entity.get(props?.id) }); + } + `, + }, + { + 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 dc16fe4e9f..6badbd8a98 100644 --- a/packages/eslint-plugin-query/src/utils/ast-utils.ts +++ b/packages/eslint-plugin-query/src/utils/ast-utils.ts @@ -67,6 +67,18 @@ export const ASTUtils = { identifiers.push(...ASTUtils.getNestedIdentifiers(node.object)) } + if (node.type === AST_NODE_TYPES.UnaryExpression) { + identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument)) + } + + if (node.type === AST_NODE_TYPES.ChainExpression) { + 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) {