Skip to content

False positive in @tanstack/query/exhaustive-deps when combining query key variable with function invocation #9643

@fbieler

Description

@fbieler

Describe the bug

In https://github.com/TanStack/query/blob/main/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.rule.ts#L83 an identifier is only de-referenced if the target is an array.

This was written when the rule wasn't able to handle key factories (see TODO near the original commit: 82adc95#diff-1db59b20778c62ed3511c0664212006c2217e7c1ca7d07b79062b6e93fa3fc8b )

I wold think identifiers can always be de-referenced?

Anyway, this causes code like this

function queryKeyFactory(dependency: string) {
  return ["my-key", dependency];
}

function example(dependency: string) {
  const queryKey = queryKeyFactory(dependency);
  return queryOptions({
    queryKey,
    queryFn: () => Promise.resolve(dependency),
  })
}

to fail with "error The following dependencies are missing in your queryKey: dependency"

Note that there's a similar issue for as-expression in the same file in line 72.

Your minimal, reproducible example

https://eslint-online-playground.netlify.app/#eNp9UstuwjAQ/BXLlwSJhFPVioqqUisuPbQ9Yw6Rs0GGxE5tBxGh/HvXD4XQCk55eGZ2ZtZnajRfwKlo2hryvaFLKppWaUvO5KcD3X+2VihpyEAqrRrC6KstpLEFPyw0FNxmHsXoM5NMVp3kDh6oH9CvEaF0n5bQgixB8n5GzkwSosF2WpINo02fHQAF5uQC2qLa4ATh5L2MutHofz2OHu04lqzuOUDx0cA0Y+qVyEidTz7XcknSGVm9kC+sQRjINRhVH6+seMIwQ+90TsHUQtocjVVi96fZEioh4c0fXZoNjEVg+EYjoa27nZDfzkfAJpclBFIWIGEZCTJjcTio6Gr/HAemzuUmZMvzfCIezZpNUtWFxf1y1TQuXJlsfbYtk649TNfi7GLnboySmMw3x6jtW2B0iW+NKrsa3z2N0bEkAcYBYtPT2+StZ1zpqPCQPz3kj4z6TkeZ4/tNpVjf3v9mFBOAsdGBA4TzG4d3C73meENux8MvUHEhcw==

Steps to reproduce

  1. Open ESLint Playground
  2. Observe error message in problems tab
  3. (optional) inline either queryKey variable or queryKeyFactory function to see error disappear

Expected behavior

As a developer I would like to be able to combine variables with functions for query keys without ESLint errors.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Linux
  • Node: v22.19.0

Tanstack Query adapter

vanilla

TanStack Query version

5.83.1

TypeScript version

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions