eslint-factory: remove useless rethrow from require-json-parse-try-catch suggestion#42537
Conversation
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
|
✅ PR Code Quality Reviewer completed the code quality review. |
|
🧠 Matt Pocock Skills Reviewer is reviewing this pull request using Matt Pocock's engineering skills... |
|
✅ Design Decision Gate 🏗️ completed the design decision gate check. No ADR enforcement needed: PR does not have the 'implementation' label and has ≤100 new lines of code in business logic directories (0 additions detected in default business logic dirs). |
|
✅ Test Quality Sentinel completed test quality analysis. |
There was a problem hiding this comment.
Pull request overview
Updates the require-json-parse-try-catch ESLint rule in eslint-factory to emit a non-trivial try/catch suggestion (avoiding a no-op throw err; catch body that can trigger no-useless-catch) and refreshes the rule’s fixtures to match the new suggestion output.
Changes:
- Introduced a
buildTryCatchSuggestion()helper to centralize and simplify suggestion string construction. - Updated the rule’s autofix suggestion to include a TODO comment and a more descriptive rethrow (
throw new Error("Failed to parse JSON: " + ...)). - Updated all affected test fixture expected outputs to match the new suggestion text across top-level and deferred-callback cases.
Show a summary per file
| File | Description |
|---|---|
| eslint-factory/src/rules/require-json-parse-try-catch.ts | Refactors suggestion construction and replaces the catch-body output to avoid a useless rethrow. |
| eslint-factory/src/rules/require-json-parse-try-catch.test.ts | Updates expected suggestion outputs for all impacted cases to align with the new emitted catch block. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Low
| `${indent}} catch (err) {`, | ||
| `${indent} // TODO: handle parse failure for this code path.`, | ||
| `${indent} throw new Error(`, | ||
| `${indent} "Failed to parse JSON: " + (err instanceof Error ? err.message : String(err)),`, | ||
| `${indent} );`, |
|
@copilot run pr-finisher skill |
🤖 PR Triage
Score breakdown: Impact 15/50 · Urgency 5/30 · Quality 10/20 Rationale: DRAFT fix for misleading
|
There was a problem hiding this comment.
Non-blocking observations
The core change is correct — replacing the no-op rethrow with a meaningful error message is a genuine improvement, and extracting buildTryCatchSuggestion is a clean refactor. Two medium issues worth addressing before this pattern proliferates across codebases that copy the generated suggestion.
Issues found
1. Generated Error drops the original SyntaxError (medium) — require-json-parse-try-catch.ts line 67
new Error("Failed to parse JSON: " + ...) loses err as .cause. Anyone debugging a parse failure in production gets only a generic message; the actual SyntaxError (with position info and its own stack) is gone. Fix: pass { cause: err } as the second argument.
2. Indentation path is completely untested (medium) — require-json-parse-try-catch.test.ts
Every fixture uses a top-level statement, so indent is always "". The ${indent} prefixes in buildTryCatchSuggestion are never exercised. A single test with JSON.parse inside an if body would cover this path.
🔎 Code quality review by PR Code Quality Reviewer · 43.4 AIC · ⌖ 7.49 AIC · ⊞ 1.6K
Comment /review to run again
| `${indent}} catch (err) {`, | ||
| `${indent} // TODO: handle parse failure for this code path.`, | ||
| `${indent} throw new Error(`, | ||
| `${indent} "Failed to parse JSON: " + (err instanceof Error ? err.message : String(err)),`, |
There was a problem hiding this comment.
Generated Error silently drops the original SyntaxError, making parse failures harder to debug.
💡 Suggested fix
The emitted catch body wraps err into a new Error but never passes it as cause, discarding the original SyntaxError stack trace. Anyone who applies this suggestion and later needs to diagnose why JSON failed to parse (e.g., unexpected token at position X) has no way to recover that information from the rethrown error.
Add { cause: err } as the second argument to new Error() in the generated string:
`${indent} throw new Error(`,
`${indent} "Failed to parse JSON: " + (err instanceof Error ? err.message : String(err)),`,
`${indent} { cause: err },`, // <-- add this line in the generated output
`${indent} );`,This preserves the original SyntaxError as error.cause, which debuggers and structured logging can surface. Error.cause is available in Node ≥ 16.9 and all modern browsers.
| { | ||
| messageId: "useHelper", | ||
| output: `try {\n const data = JSON.parse(rawInput);\n} catch (err) {\n throw err;\n}`, | ||
| output: `try {\n const data = JSON.parse(rawInput);\n} catch (err) {\n // TODO: handle parse failure for this code path.\n throw new Error(\n "Failed to parse JSON: " + (err instanceof Error ? err.message : String(err)),\n );\n}`, |
There was a problem hiding this comment.
No test covers an indented JSON.parse call — the indent path in buildTryCatchSuggestion is untested.
💡 What to add
Every test fixture here uses a top-level statement where indent computes to "". The indentation extraction in the rule:
const indent = stmtLine.match(/^(\s*)/)?.[1] ?? "";...and the ${indent} prefixes in buildTryCatchSuggestion are therefore never exercised by any test. If a future refactor breaks indentation for nested calls (e.g., JSON.parse inside an if body), all existing tests would still pass.
Add an invalid fixture that wraps JSON.parse inside an indented block — something like:
{
code: `if (cond) {\n JSON.parse(raw);\n}`,
// expect indented try/catch in suggestion output
}This guards the indent !== "" path end-to-end.
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
There was a problem hiding this comment.
Review: eslint-factory — remove useless rethrow from require-json-parse-try-catch suggestion
Overall: ✅ The fix is correct and a clear improvement. Replacing the no-op throw err with a contextual wrapper eliminates the no-useless-catch lint trigger and gives consumers meaningful scaffolding to build on. The buildTryCatchSuggestion helper is clean and all 11 test fixtures are consistently updated.
One non-blocking suggestion (see inline comment): The generated new Error(...) discards the original error's stack trace. Adding { cause: err } would preserve the original for debugging, with no compatibility cost at Node 16.9+.
🧵 Reviewed using Impeccable skills by Impeccable Skills Reviewer · 28.5 AIC · ⌖ 8.17 AIC · ⊞ 4.9K
| `${indent} ${stmtText}`, | ||
| `${indent}} catch (err) {`, | ||
| `${indent} // TODO: handle parse failure for this code path.`, | ||
| `${indent} throw new Error(`, |
There was a problem hiding this comment.
The generated throw new Error(...) discards the original error's stack trace, which can make debugging parse failures harder.
Consider emitting { cause: err } to retain the original context:
`${indent} throw new Error(`,
`${indent} "Failed to parse JSON: " + (err instanceof Error ? err.message : String(err)),`,
`${indent} { cause: err },`,
`${indent} );`,This produces auto-generated scaffolding like:
catch (err) {
// TODO: handle parse failure for this code path.
throw new Error(
"Failed to parse JSON: " + (err instanceof Error ? err.message : String(err)),
{ cause: err },
);
}Error({ cause }) is supported in Node 16.9+ and is available in all environments this rule targets. Since the fix is auto-generated and applied broadly, preserving the original error as cause improves debuggability for all consumers who apply it. @copilot please address this.
🧪 Test Quality Sentinel Report✅ Test Quality Score: 95/100 — Excellent
📊 Metrics & Test Classification (5 tests analyzed)
Note: The changed test file is
Go: 0; JavaScript/TypeScript: 5 ( Test inflation: Test file: +11/−11 lines (net 0); production file: +14/−1 lines. Ratio: 0.79:1. ✅ Not inflated.
|
There was a problem hiding this comment.
✅ Test Quality Sentinel: 95/100. 0% implementation tests (threshold: 30%). All 5 modified test blocks verify behavioral contracts (the auto-fix suggestion output). No Go mock violations. No missing build tags. One pre-existing indentation anomaly flagged in the comment above.
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
|
🎉 This pull request is included in a new release. Release: |
The
require-json-parse-try-catchsuggestion previously auto-generatedcatch (err) { throw err; }, which adds no handling and can triggerno-useless-catch. This change updates the emitted fix to produce a meaningful catch body and aligns all rule fixtures with the new output.Suggestion output quality
require-json-parse-try-catchautofix output.// TODO: handle parse failure for this code path.new Error("Failed to parse JSON: " + ...)Rule fixture updates
require-json-parse-try-catch.test.tsexpected suggestion outputs across all affected cases.on,then,setTimeout,Promiseexecutor,nextTick).