eslint-react icon indicating copy to clipboard operation
eslint-react copied to clipboard

[bug] Immutability has false positives for Next.js router

Open WikiRik opened this issue 4 months ago • 3 comments

Describe the bug

This is just a quick bug report. If anything is unclear, I can take the time for a proper reproduction etc later. I just wanted to bring this to your attention. Thanks for all the great work for this project!


The fix done in https://github.com/Rel1cx/eslint-react/pull/1897 for globals fixed most of the false positives we've encountered with this rule. The ones that remain are due to the Next.js router (useRouter import from next/navigation). I'm not very knowledgeable about this area, so I don't know how easy it is to fix this.

An alternative, that might be better for other as well, is to allow configuration of the immutability rule to disable it for some imports. That way nothing needs to be built for specific frameworks.

Reproduction

No response

Expected behavior

No response

Platform and versions


Stack trace


Additional context

No response

WikiRik avatar Jul 12 '26 15:07 WikiRik

Thank you for the feedback. Although I didn't have a direct code snippet to reproduce the issue, I manually searched using https://grep.app/search?f.lang=TypeScript&f.lang=TSX&q=from 'next/navigation and found a potential snippet that successfully reproduces the problem. After minimizing it, it looks like this:

function Component() {
  const router = useRouter()
  return <button onClick={() => router.push('/dashboard')} />
}

I have identified the root cause, and a fix is ​​currently in progress.

I also appreciate your suggestion regarding the addition of a configuration option; however, we currently lack sufficient usage data for this rule. We will consider adding such an option once we have gathered more feedback.

Rel1cx avatar Jul 12 '26 16:07 Rel1cx

I reproduced this issue with useRouter() from next/navigation. The immutability rule reports router.push("/dashboard") because push is included in the static MUTATING_METHODS list, so the rule treats any receiver method named push as mutating regardless of the receiver type.

Simply removing push from MUTATING_METHODS would break detection of actual mutations such as array.push().

I also looked into the existing resolveImportSource helper, but router is a local variable initialized from useRouter(), so handling this case would require tracing the variable initializer to useRouter and then resolving its import source.

Before implementing a fix, which direction would you prefer: a Next.js-specific exception, a configurable allowlist/ignore option, or another approach?

Ehtasham-Yasin avatar Jul 12 '26 18:07 Ehtasham-Yasin

I reproduced this issue with useRouter() from next/navigation. The immutability rule reports router.push("/dashboard") because push is included in the static MUTATING_METHODS list, so the rule treats any receiver method named push as mutating regardless of the receiver type.

Simply removing push from MUTATING_METHODS would break detection of actual mutations such as array.push().

I also looked into the existing resolveImportSource helper, but router is a local variable initialized from useRouter(), so handling this case would require tracing the variable initializer to useRouter and then resolving its import source.

Before implementing a fix, which direction would you prefer: a Next.js-specific exception, a configurable allowlist/ignore option, or another approach?

Thanks for investigating this. I chose to start by recognizing values returned by useRouter()-like hook calls directly in the rule, then refine and extend this handling through follow-up iterations.

When the receiver traces through variable initializer aliases to useRouter(), mutation analysis stops at that API boundary and treats the call as non-mutating.

We intentionally do not continue to resolve the import source. As discussed in #1172, APIs are identified primarily by shape or name rather than package source (React Compiler follows the same approach for built-in APIs and Hooks). Only some rules resolve an API’s import source when applying an auto-fix, where the additional check is needed to avoid destructive changes. One example is plugins/eslint-plugin-react-x/src/rules/no-forward-ref/no-forward-ref.ts.

For rules ported or derived from the upstream eslint-plugin-react-hooks, we also prefer to avoid prematurely adding configuration options that the upstream rule does not provide.

Rel1cx avatar Jul 12 '26 20:07 Rel1cx