Skip to content

eslint-factory: require-json-parse-try-catch autofix emits a useless catch (err) { throw err; } #42434

Description

@github-actions

Summary

The suggestion fix for require-json-parse-try-catch wraps the offending statement in a try/catch whose body merely re-throws:

// require-json-parse-try-catch.ts:161
return fixer.replaceText(
  stmt,
  `try {\n${indent}  ${stmtText}\n${indent}} catch (err) {\n${indent}  throw err;\n${indent}}`
);

The produced code is asserted verbatim across the test suite, e.g.:

try {
  const data = JSON.parse(rawInput);
} catch (err) {
  throw err;
}

Why this is a problem

  1. It adds zero handling. catch (err) { throw err; } rethrows the identical error with the same stack semantics — the program behaves exactly as if there were no try/catch. The rule's stated intent is "avoid uncaught runtime failures", but the fix does not avoid anything; it just silences the linter.
  2. It introduces a different lint violation. catch (e) { throw e; } is precisely what ESLint core's no-useless-catch (part of eslint:recommended) flags. The suggested fix trades one warning for another, and any project running the recommended set gets a new error from applying our suggestion.
  3. It trains the wrong pattern. Contributors who accept the suggestion learn a no-op idiom instead of real error handling.

Acceptance criteria

  • The suggestion no longer emits a bare catch (err) { throw err; }. Acceptable directions (pick one, document the choice):
    • Emit a catch body that is meaningful by default — e.g. catch (err) { throw new Error(\Failed to parse JSON: ${getErrorMessage(err)}`); }(contextual message via the establishedgetErrorMessage` helper), or
    • Emit a catch body with an explicit // TODO: handle parse failure placeholder plus a contextual rethrow, so applying the fix never produces no-useless-catch-violating code, or
    • Replace the suggestion text with guidance only (message useHelper already points to parseJsonWithRepair / parseJsonlContent) and drop the auto-wrap entirely if a safe meaningful default cannot be generated.
  • Whatever is chosen, the emitted output must not be flagged by no-useless-catch.
  • Update the test fixtures (require-json-parse-try-catch.test.ts) to the new expected output, including the deferred-callback nested cases.

Notes

Independent of the existing scope items #41962 (computed/indirect JSON.parse) and #41963 (deferred-callback ancestry); this is purely about the quality of the emitted fix.

Generated by 🤖 ESLint Refiner · 185.1 AIC · ⌖ 12.4 AIC · ⊞ 4.7K ·

  • expires on Jul 6, 2026, 10:44 PM UTC-08:00

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions