-
Notifications
You must be signed in to change notification settings - Fork 442
eslint-factory: remove useless rethrow from require-json-parse-try-catch suggestion #42537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
264bdeb
cfc8d13
62aeaab
2ab8d9a
d41e2b4
7b79d80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,20 @@ function isDeferredCallback(funcNode: TSESTree.Node): boolean { | |
| return false; | ||
| } | ||
|
|
||
| function buildTryCatchSuggestion(stmtText: string, indent: string): string { | ||
| return [ | ||
| "try {", | ||
| `${indent} ${stmtText}`, | ||
| `${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)),`, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generated 💡 Suggested fixThe emitted catch body wraps Add `${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 |
||
| `${indent} { cause: err },`, | ||
| `${indent} );`, | ||
|
Comment on lines
+64
to
+69
|
||
| `${indent}}`, | ||
| ].join("\n"); | ||
| } | ||
|
|
||
| export const requireJsonParseTryCatchRule = createRule({ | ||
| name: "require-json-parse-try-catch", | ||
| meta: { | ||
|
|
@@ -158,7 +172,7 @@ export const requireJsonParseTryCatchRule = createRule({ | |
| const startLine = stmt.loc?.start.line; | ||
| const stmtLine = startLine !== undefined ? (sourceCode.lines[startLine - 1] ?? "") : ""; | ||
| const indent = stmtLine.match(/^(\s*)/)?.[1] ?? ""; | ||
| return fixer.replaceText(stmt, `try {\n${indent} ${stmtText}\n${indent}} catch (err) {\n${indent} throw err;\n${indent}}`); | ||
| return fixer.replaceText(stmt, buildTryCatchSuggestion(stmtText, indent)); | ||
| }, | ||
| }, | ||
| ], | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:This produces auto-generated scaffolding like:
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 ascauseimproves debuggability for all consumers who apply it. @copilot please address this.