Skip to content

fix: avoid prefer-numeric-literals false positive for shadowed globals#21047

Merged
mdjermanovic merged 1 commit into
eslint:mainfrom
koreahghg:fix-prefer-numeric-literals-shadowed-globals
Jul 3, 2026
Merged

fix: avoid prefer-numeric-literals false positive for shadowed globals#21047
mdjermanovic merged 1 commit into
eslint:mainfrom
koreahghg:fix-prefer-numeric-literals-shadowed-globals

Conversation

@koreahghg

Copy link
Copy Markdown
Contributor

Summary

prefer-numeric-literals detects calls to parseInt() / Number.parseInt() purely by identifier/property name, without checking whether parseInt or Number actually resolve to the global built-ins. If either is shadowed by a local binding, the rule still reports it, and its --fix autofix replaces the call with a numeric literal — silently changing the program's behavior since the call is no longer to the (unrelated) shadowed binding at all.

function parse(parseInt) {
  return parseInt("11", 2); // user's own `parseInt` parameter, not the global
}

--fix turns this into:

function parse(parseInt) {
  return 0b11; // the call disappeared entirely
}

Sibling rules radix and use-isnan were already fixed for the same class of bug (see #21044 and #20958), by checking sourceCode.isGlobalReference(...) in addition to the name-based check. This PR applies the same fix to prefer-numeric-literals.

Test plan

  • Added valid test cases for shadowed parseInt (both as a parameter and a var declaration) and shadowed Number (member access form)
  • npm test -- tests/lib/rules/prefer-numeric-literals.js passes (91 tests)
  • Verified manually with Linter#verify/verifyAndFix that the false positive and the destructive autofix are both gone after the fix

@koreahghg
koreahghg requested a review from a team as a code owner July 3, 2026 02:18
@github-project-automation github-project-automation Bot moved this to Needs Triage in Triage Jul 3, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 3, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: koreahghg / name: koreahghg (a64ba8c)

@netlify

netlify Bot commented Jul 3, 2026

Copy link
Copy Markdown

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit a64ba8c
🔍 Latest deploy log https://app.netlify.com/projects/docs-eslint/deploys/6a471c14be8ca40008338655

@eslint-github-bot

Copy link
Copy Markdown

Hi @koreahghg!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The length of the commit message must be less than or equal to 72

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

@github-actions github-actions Bot added the rule Relates to ESLint's core rules label Jul 3, 2026
@koreahghg koreahghg changed the title fix: avoid false positives in prefer-numeric-literals for shadowed globals fix: avoid prefer-numeric-literals false positive for shadowed globals Jul 3, 2026
@eslint-github-bot eslint-github-bot Bot added the bug ESLint is working incorrectly label Jul 3, 2026
@mdjermanovic mdjermanovic added the accepted There is consensus among the team that this change meets the criteria for inclusion label Jul 3, 2026
@mdjermanovic mdjermanovic moved this from Needs Triage to Triaging in Triage Jul 3, 2026
@mdjermanovic

Copy link
Copy Markdown
Member

Thanks for the PR. I can reproduce it in the Playground, so marking this as accepted, but next time please use our PR template.

@mdjermanovic mdjermanovic moved this from Triaging to Implementing in Triage Jul 3, 2026

@mdjermanovic mdjermanovic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@mdjermanovic
mdjermanovic merged commit 8859baf into eslint:main Jul 3, 2026
35 checks passed
@github-project-automation github-project-automation Bot moved this from Implementing to Complete in Triage Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly rule Relates to ESLint's core rules

Projects

Status: Complete

Development

Successfully merging this pull request may close these issues.

2 participants