Skip to content

Make no-json-stringify-set-or-map binding-aware to avoid cross-scope name collisions - #54301

Merged
pelikhan merged 3 commits into
mainfrom
copilot/eslint-factory-fix-no-json-stringify-issue
Aug 20, 2026
Merged

Make no-json-stringify-set-or-map binding-aware to avoid cross-scope name collisions#54301
pelikhan merged 3 commits into
mainfrom
copilot/eslint-factory-fix-no-json-stringify-issue

Conversation

Copilot AI commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

no-json-stringify-set-or-map previously tracked Set/Map candidates by bare identifier name at file scope, so same-named variables in different lexical scopes could be misclassified. This change resolves JSON.stringify(x) against x’s actual binding before deciding whether it was initialized as new Set(...) or new Map(...).

  • Scope-aware binding resolution

    • Replaced flat Map<string, "Set" | "Map"> tracking with lexical lookup via ESLint scope (sourceCode.getScope(...) + variable defs).
    • For JSON.stringify(identifier), the rule now checks the specific resolved binding and flags only when that binding is a const initialized from global Set/Map construction.
    • Inline cases (JSON.stringify(new Set(...)) / new Map(...)) remain unchanged.
  • Regression coverage for same-name variables across scopes

    • Added tests with two functions in one file that both declare seen, where one is new Set(...) and the other is a plain object.
    • Added both declaration orders to guard against traversal-order sensitivity.
    • Added a valid-case regression confirming the non-Set same-name binding is not flagged.
function withSet() {
  const seen = new Set([1, 2]);
  JSON.stringify(seen); // flagged
}

function withObject() {
  const seen = { other: true };
  JSON.stringify(seen); // not flagged
}

Copilot AI and others added 2 commits August 20, 2026 17:57
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix no-json-stringify-set-or-map variable tracking by scope Make no-json-stringify-set-or-map binding-aware to avoid cross-scope name collisions Aug 20, 2026
Copilot AI requested a review from pelikhan August 20, 2026 18:00
@pelikhan
pelikhan marked this pull request as ready for review August 20, 2026 18:02
Copilot AI balanced review requested due to automatic review settings August 20, 2026 18:02
@pelikhan
pelikhan merged commit f886106 into main Aug 20, 2026
2 checks passed
@pelikhan
pelikhan deleted the copilot/eslint-factory-fix-no-json-stringify-issue branch August 20, 2026 18:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Makes the ESLint rule resolve Set/Map variables by lexical binding rather than identifier name.

Changes:

  • Adds scope-aware resolution for const Set/Map bindings.
  • Adds regression tests for cross-scope name collisions and declaration order.
Show a summary per file
File Description
eslint-factory/src/rules/no-json-stringify-set-or-map.ts Resolves the exact variable binding before reporting.
eslint-factory/src/rules/no-json-stringify-set-or-map.test.ts Covers same-name bindings across scopes.

Review details

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.87.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

eslint-factory: no-json-stringify-set-or-map tracks Set/Map variables by bare name, not by scope

3 participants