eslint-factory: fix require-json-parse-try-catch to catch computed JSON["parse"] access#42000
Merged
Merged
Conversation
…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
Contributor
There was a problem hiding this comment.
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.parseandJSON["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
validsuite. Adding the sameJSON["parse"]try/catch case to the ES modulevalidsuite would help ensure the behavior is consistent across bothsourceTypemodes.
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"); |
This was referenced Jun 28, 2026
Contributor
|
🎉 This pull request is included in a new release. Release: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
require-json-parse-try-catchrule silently missedJSON["parse"](x)— computed member access with a string literal key — because the property guard required anIdentifiernode, causing early return onLiteralnodes.Changes
Rule fix (
require-json-parse-try-catch.ts): Replace two separateproperty.type/property.nameguards with a singleisParsePropertypredicate that accepts bothIdentifier(JSON.parse) and stringLiteral(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): AddJSON["parse"]inside try to the valid set; add new invalid test suite covering computed access both as assignment and bare expression statement.