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

feat(eslint-plugin): [no-float-prom] fixer + msg for ignoreVoid #1473

Merged
merged 3 commits into from Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/eslint-plugin/src/rules/no-floating-promises.ts
Expand Up @@ -9,7 +9,7 @@ type Options = [
},
];

export default util.createRule<Options, 'floating'>({
export default util.createRule<Options, 'floating' | 'floatingVoid'>({
name: 'no-floating-promises',
meta: {
docs: {
Expand All @@ -20,6 +20,8 @@ export default util.createRule<Options, 'floating'>({
},
messages: {
floating: 'Promises must be handled appropriately',
floatingVoid:
'Promises must be handled appropriately or explicitly marked as ignored with the `void` operator',
},
schema: [
{
Expand Down Expand Up @@ -48,7 +50,7 @@ export default util.createRule<Options, 'floating'>({

if (isUnhandledPromise(checker, expression)) {
context.report({
messageId: 'floating',
messageId: options.ignoreVoid ? 'floatingVoid' : 'floating',
Copy link
Member

Choose a reason for hiding this comment

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

This would probably be better as a suggestion fixer, to make it easy to fix to.

Suggested change
messageId: options.ignoreVoid ? 'floatingVoid' : 'floating',
messageId: options.ignoreVoid ? 'floatingVoid' : 'floating',
suggest: options.ignoreVoid ? [
{
messageId: 'xxx', // Consider explicitly marking
fix(fixer) { /* ... */ }
},
] : [],

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But won't that autofix with the --fix eslint argument? Or eslint.fixAll code action in vscode? That would suck because I think it should be the developer's conscious decision.

Copy link
Member

Choose a reason for hiding this comment

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

Nope, that's the beauty of eslint suggestions!

For fixers, there is exactly 0 or 1 fixers for each report. A fixer will be applied by eslint's --fix mode, if it exists for a report.

For suggestion fixers, on the other hand, can have 0..n suggestions for each report. Each suggestion has its own message which is used to communicate what it does to the user. Each suggestion is not automatically applied by eslint's --fix mode.

Suggestions were added relatively recently, and are great for doing these sorts of fixes - ones that might break code, or require conscious decisions from the user. And the fact that you can present multiple for a report means that you can present the user with options for correcting the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

node,
});
}
Expand Down
14 changes: 14 additions & 0 deletions packages/eslint-plugin/tests/rules/no-floating-promises.test.ts
Expand Up @@ -257,6 +257,20 @@ async function test() {
},
],
},
{
options: [{ ignoreVoid: true }],
code: `
async function test() {
Promise.resolve("value");
}
`,
errors: [
{
line: 3,
messageId: 'floatingVoid',
},
],
},
{
code: `
async function test() {
Expand Down