Skip to content
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

Add prefer-t-throws rule #345

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Mesteery
Copy link

@Mesteery Mesteery commented Feb 6, 2022

Fixes: #156


IssueHunt Summary

Referenced issues

This pull request has been submitted to:


@sindresorhus
Copy link
Member

AVA now has t.throws and t.throwsAsync. See the AVA docs.

@Mesteery
Copy link
Author

Mesteery commented Feb 6, 2022

AVA now has t.throws and t.throwsAsync. See the AVA docs.

So should the rule be t-throws-async instead or should I add another rule?

@sindresorhus
Copy link
Member

The name should be the what it is, but you need to also support the async method.

const createAvaRule = require('../create-ava-rule');
const util = require('../util');

// This function checks if there is an AwaitExpression, which is not inside another function.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe you could find an AwaitExpression, then iterate upwards through its parent scopes until you encounter a function, and then check the function.

https://eslint.org/docs/developer-guide/scope-manager-interface

Copy link
Author

Choose a reason for hiding this comment

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

What about the ReturnStatement? I doubt there's anything simpler than that in the end.

Copy link
Member

Choose a reason for hiding this comment

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

Do you have a realistic example of when someone would actually use return in a try/catch when testing an error?

Copy link
Author

Choose a reason for hiding this comment

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

No, I don't have one. It's pretty hard to find a realistic example but I have a feeling that there might be some code written that way.

Copy link
Member

Choose a reason for hiding this comment

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

I have a feeling that there might be some code written that way.

Rule logic should be based on test fixtures, not feelings. My recommendation to simplify the logic here stands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule proposal: prefer-t-throws
2 participants