Skip to content

eslint-factory: fix require-json-parse-try-catch to catch computed JSON["parse"] access#42000

Merged
pelikhan merged 2 commits into
mainfrom
copilot/fix-eslint-factory-json-parse-rule
Jun 28, 2026
Merged

eslint-factory: fix require-json-parse-try-catch to catch computed JSON["parse"] access#42000
pelikhan merged 2 commits into
mainfrom
copilot/fix-eslint-factory-json-parse-rule

Conversation

Copilot AI commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

The require-json-parse-try-catch rule silently missed JSON["parse"](x) — computed member access with a string literal key — because the property guard required an Identifier node, causing early return on Literal nodes.

Changes

  • Rule fix (require-json-parse-try-catch.ts): Replace two separate property.type/property.name guards with a single isParseProperty predicate that accepts both Identifier (JSON.parse) and string Literal (JSON["parse"]) property nodes. Aliased and destructured bindings (const p = JSON.parse; p(x)) are documented as intentionally out of scope via inline comment.

  • Tests (require-json-parse-try-catch.test.ts): Add JSON["parse"] inside try to the valid set; add new invalid test suite covering computed access both as assignment and bare expression statement.

// Before: only matched this
JSON.parse(raw);

// After: also matches this
JSON["parse"](raw);
// ↑ reports requireTryCatch with suggestion to wrap in try/catch

…SON["parse"] access

- Accept both `JSON.parse(x)` (Identifier property) and `JSON["parse"](x)` (string Literal property) as matches
- Add comment documenting aliased/destructured bindings as intentionally out of scope
- Add test: computed access inside try block is valid (not flagged)
- Add test: computed access outside try block is flagged with suggestion

Closes #41962

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix require-json-parse-try-catch for indirect JSON.parse access eslint-factory: fix require-json-parse-try-catch to catch computed JSON["parse"] access Jun 28, 2026
Copilot AI requested a review from pelikhan June 28, 2026 04:06
@pelikhan pelikhan marked this pull request as ready for review June 28, 2026 04:38
Copilot AI review requested due to automatic review settings June 28, 2026 04:38
@pelikhan pelikhan merged commit 34dd07f into main Jun 28, 2026
@pelikhan pelikhan deleted the copilot/fix-eslint-factory-json-parse-rule branch June 28, 2026 04:38

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

This PR fixes the require-json-parse-try-catch ESLint rule so it also flags computed string-literal JSON["parse"](...) calls (previously missed due to an overly strict property-node guard), and extends the rule’s tests to cover the new matching behavior.

Changes:

  • Update rule logic to recognize both JSON.parse and JSON["parse"] member accesses.
  • Add tests for computed-access valid/invalid cases and suggestion output.
Show a summary per file
File Description
eslint-factory/src/rules/require-json-parse-try-catch.ts Extends the rule’s callee-property matching to include computed string-literal access.
eslint-factory/src/rules/require-json-parse-try-catch.test.ts Adds valid/invalid cases to ensure JSON["parse"](...) is handled and suggested fixes are correct.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comments suppressed due to low confidence (1)

eslint-factory/src/rules/require-json-parse-try-catch.test.ts:30

  • The new computed-access support is only exercised in the CommonJS valid suite. Adding the same JSON["parse"] try/catch case to the ES module valid suite would help ensure the behavior is consistent across both sourceType modes.
  it("valid: JSON.parse inside try block passes (CommonJS)", () => {
    cjsRuleTester.run("require-json-parse-try-catch", requireJsonParseTryCatchRule, {
      valid: [`try { const x = JSON.parse(str); } catch (e) {}`, `try { return JSON.parse(str); } catch (e) {}`, `function f() { try { JSON.parse(str); } catch (e) {} }`, `try { const x = JSON["parse"](str); } catch (e) {}`],
      invalid: [],
    });
  });

  it("valid: JSON.parse inside try block passes (ES module)", () => {
    esmRuleTester.run("require-json-parse-try-catch", requireJsonParseTryCatchRule, {
      valid: [`try { const x = JSON.parse(str); } catch (e) {}`],
      invalid: [],
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Low

Comment on lines +68 to 72
const property = node.callee.property;
const isParseProperty = (property.type === "Identifier" && property.name === "parse") || (property.type === "Literal" && property.value === "parse");

if (node.callee.property.name !== "parse") {
if (!isParseProperty) {
return;
Comment on lines +63 to +69
// Accept both direct property access (JSON.parse) and computed string-literal
// access (JSON["parse"]). Aliased (const p = JSON.parse; p(raw)) and
// destructured (const { parse } = JSON; parse(raw)) bindings are intentionally
// out of scope: tracking them reliably requires full scope analysis and is
// disproportionate to the current risk surface.
const property = node.callee.property;
const isParseProperty = (property.type === "Identifier" && property.name === "parse") || (property.type === "Literal" && property.value === "parse");
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.82.0

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.

3 participants