Skip to content

Conversation

fasttime
Copy link
Member

@fasttime fasttime commented Sep 12, 2025

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[X] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

Tell us about your environment (npx eslint --env-info):

Node version: v22.19.0
npm version: v10.9.3
Local ESLint version: v9.35.0 (Currently used)
Global ESLint version: v9.35.0
Operating System: darwin 24.6.0

What parser are you using (place an "X" next to just one item)?

[x] Default (Espree)
[ ] @typescript-eslint/parser
[ ] @babel/eslint-parser
[ ] vue-eslint-parser
[ ] @angular-eslint/template-parser
[ ] Other

Please show your full configuration:

inline config only

What did you do? Please include the actual source code causing the issue.

/* eslint preserve-caught-error: "error" */

try {
  trySomething();
} catch (error) {
  throw new Error("Something's broken", {
    get cause() {
      return error;
    },
  });
}

Playground link

/* eslint preserve-caught-error: "error" */

try {
  trySomething();
} catch (error) {
  throw new Error("Something's broken", {
    cause: error,
    cause: bar,
  });
}

Playground link

What did you expect to happen?

In the first example with the getter, the suggestion should produce valid code but it doesn't.

In the second example, the rule should report an error because the cause value will be bar.

What actually happened? Please include the actual, raw output from ESLint.

In the first example, the suggested fix is producing invalid JavaScript.

In the second example, no error is being reported.

What changes did you make? (Give an overview)

This pull request ensures that if there are multiple cause definitions, only the last one is checked and no suggestions are provided. The suggestion for getters and setters has been fixed.

Is there anything you'd like reviewers to focus on?

@github-project-automation github-project-automation bot moved this to Needs Triage in Triage Sep 12, 2025
Copy link

netlify bot commented Sep 12, 2025

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit dde7686
🔍 Latest deploy log https://app.netlify.com/projects/docs-eslint/deploys/68c8dd53a8e5ef0008f19b87
😎 Deploy Preview https://deploy-preview-20109--docs-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@eslint-github-bot eslint-github-bot bot added the bug ESLint is working incorrectly label Sep 12, 2025
@github-actions github-actions bot added the rule Relates to ESLint's core rules label Sep 12, 2025
// Multiple cause properties found, no reliable suggestion can be made

context.report({
messageId: "incorrectCause",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there should be a different error message when cause is defined multiple times.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the The symptom error is being thrown with an incorrect `cause`. message would be confusing in the following case because the error is actually thrown with the correct cause:

/* eslint preserve-caught-error: "error" */

try {
  trySomething();
} catch (error) {
  throw new Error("Something's broken", {
    cause: bar,
    cause: error,
  });
}

We could either:

  1. Use a different message saying that cause is defined multiple times, though it would overlap with the no-dupe-keys rule.
  2. Not report any errors, because this is more a case for the no-dupe-keys rule.
  3. Check the last cause property definition only because that's the one that will be set on the object.

I think I'd vote for 2.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea not to report an error when the last cause has the expected value. It seems that the current message should be clear enough in other cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we know the last cause has the expected value, then I would expect that we'd go off of that one. Whether a project has no-dupe-keys enabled doesn't impact whether the issues caught by this rule are impactful. I vote for 3. +1 that the current message should be clear enough in other cases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions! I've updated the rule to check the last cause definition and only provide suggestions when a single definition is present. If we decide to ignore cases that overlap with no-dupe-keys altogether, we'll have to make an exception for getter and setter pairs, which aren't reported, and account for other edge cases of that rule, so I thought that checking the last definition would keep the logic simple.

@fasttime fasttime marked this pull request as ready for review September 12, 2025 16:25
@fasttime fasttime requested a review from a team as a code owner September 12, 2025 16:25
@mdjermanovic mdjermanovic moved this from Needs Triage to Implementing in Triage Sep 12, 2025
@mdjermanovic mdjermanovic added the accepted There is consensus among the team that this change meets the criteria for inclusion label Sep 12, 2025
@mdjermanovic mdjermanovic changed the title fix: correct preserve-caught-error edge cases feat: correct preserve-caught-error edge cases Sep 16, 2025
@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label Sep 16, 2025
Comment on lines +732 to +737
{
code: `try {} catch (error) {
throw new Error("Something failed", { cause: error, cause: anotherError });
}`,
errors: [{ messageId: "incorrectCause" }],
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was valid before this change, so I've changed the tag to feat because this change can produce more lint errors.

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@mdjermanovic mdjermanovic merged commit 47afcf6 into main Sep 16, 2025
30 checks passed
@mdjermanovic mdjermanovic deleted the fix-preserve-caught-error branch September 16, 2025 12:47
@github-project-automation github-project-automation bot moved this from Implementing to Complete in Triage Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly feature This change adds a new feature to ESLint rule Relates to ESLint's core rules

Projects

Status: Complete

Development

Successfully merging this pull request may close these issues.

3 participants