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-floating-promises] add suggestion fixer to add an 'await' #5943

Merged
merged 13 commits into from Dec 16, 2022
Merged
24 changes: 23 additions & 1 deletion packages/eslint-plugin/src/rules/no-floating-promises.ts
Expand Up @@ -12,7 +12,11 @@ type Options = [
},
];

type MessageId = 'floating' | 'floatingVoid' | 'floatingFixVoid';
type MessageId =
| 'floating'
| 'floatingVoid'
| 'floatingFixVoid'
| 'floatingFixAwait';

export default util.createRule<Options, MessageId>({
name: 'no-floating-promises',
Expand All @@ -27,6 +31,7 @@ export default util.createRule<Options, MessageId>({
messages: {
floating:
'Promises must be awaited, end with a call to .catch, or end with a call to .then with a rejection handler.',
floatingFixAwait: 'Add await operator.',
floatingVoid:
'Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler' +
' or be explicitly marked as ignored with the `void` operator.',
Expand Down Expand Up @@ -95,6 +100,23 @@ export default util.createRule<Options, MessageId>({
context.report({
node,
messageId: 'floating',
suggest: [
{
messageId: 'floatingFixAwait',
fix(fixer): TSESLint.RuleFix {
if (
expression.type === AST_NODE_TYPES.UnaryExpression &&
expression.operator === 'void'
) {
return fixer.replaceTextRange(
[expression.range[0], expression.range[0] + 4],
'await',
);
}
return fixer.insertTextBefore(node, 'await ');
},
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
},
],
});
}
}
Expand Down
62 changes: 62 additions & 0 deletions packages/eslint-plugin/tests/rules/no-floating-promises.test.ts
Expand Up @@ -656,6 +656,68 @@ async function test() {
{
line: 3,
messageId: 'floating',
suggestions: [
{
messageId: 'floatingFixAwait',
output: `
async function test() {
await Promise.resolve();
}
`,
},
],
},
],
},
{
code: `
async function test() {
const promise = new Promise((resolve, reject) => resolve('value'));
promise;
}
`,
options: [{ ignoreVoid: false }],
errors: [
{
line: 4,
messageId: 'floating',
suggestions: [
{
messageId: 'floatingFixAwait',
output: `
async function test() {
const promise = new Promise((resolve, reject) => resolve('value'));
await promise;
}
`,
},
],
},
],
},
{
code: `
async function returnsPromise() {
return 'value';
}
void returnsPromise();
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
`,
options: [{ ignoreVoid: false }],
errors: [
{
line: 5,
messageId: 'floating',
suggestions: [
{
messageId: 'floatingFixAwait',
output: `
async function returnsPromise() {
return 'value';
}
await returnsPromise();
`,
},
],
},
],
},
Expand Down