Skip to content

Commit 159b16e

Browse files
authored
feat(eslint-plugin): [no-float-prom] fixer + msg for ignoreVoid (#1473)
1 parent b22424e commit 159b16e

File tree

2 files changed

+89
-41
lines changed

2 files changed

+89
-41
lines changed

Diff for: packages/eslint-plugin/src/rules/no-floating-promises.ts

+30-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as tsutils from 'tsutils';
22
import * as ts from 'typescript';
3+
import { TSESLint } from '@typescript-eslint/experimental-utils';
34

45
import * as util from '../util';
56

@@ -9,7 +10,9 @@ type Options = [
910
},
1011
];
1112

12-
export default util.createRule<Options, 'floating'>({
13+
type MessageId = 'floating' | 'floatingVoid' | 'floatingFixVoid';
14+
15+
export default util.createRule<Options, MessageId>({
1316
name: 'no-floating-promises',
1417
meta: {
1518
docs: {
@@ -20,6 +23,10 @@ export default util.createRule<Options, 'floating'>({
2023
},
2124
messages: {
2225
floating: 'Promises must be handled appropriately',
26+
floatingVoid:
27+
'Promises must be handled appropriately' +
28+
' or explicitly marked as ignored with the `void` operator',
29+
floatingFixVoid: 'Add void operator to ignore',
2330
},
2431
schema: [
2532
{
@@ -41,16 +48,34 @@ export default util.createRule<Options, 'floating'>({
4148
create(context, [options]) {
4249
const parserServices = util.getParserServices(context);
4350
const checker = parserServices.program.getTypeChecker();
51+
const sourceCode = context.getSourceCode();
4452

4553
return {
4654
ExpressionStatement(node): void {
4755
const { expression } = parserServices.esTreeNodeToTSNodeMap.get(node);
4856

4957
if (isUnhandledPromise(checker, expression)) {
50-
context.report({
51-
messageId: 'floating',
52-
node,
53-
});
58+
if (options.ignoreVoid) {
59+
context.report({
60+
node,
61+
messageId: 'floatingVoid',
62+
suggest: [
63+
{
64+
messageId: 'floatingFixVoid',
65+
fix(fixer): TSESLint.RuleFix {
66+
let code = sourceCode.getText(node);
67+
code = `void ${code}`;
68+
return fixer.replaceText(node, code);
69+
},
70+
},
71+
],
72+
});
73+
} else {
74+
context.report({
75+
node,
76+
messageId: 'floating',
77+
});
78+
}
5479
}
5580
},
5681
};

Diff for: packages/eslint-plugin/tests/rules/no-floating-promises.test.ts

+59-36
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import rule from '../../src/rules/no-floating-promises';
22
import { RuleTester, getFixturesRootDir } from '../RuleTester';
33

44
const rootDir = getFixturesRootDir();
5-
const messageId = 'floating';
65

76
const ruleTester = new RuleTester({
87
parserOptions: {
@@ -245,15 +244,39 @@ async function test() {
245244
errors: [
246245
{
247246
line: 3,
248-
messageId,
247+
messageId: 'floating',
249248
},
250249
{
251250
line: 4,
252-
messageId,
251+
messageId: 'floating',
253252
},
254253
{
255254
line: 5,
256-
messageId,
255+
messageId: 'floating',
256+
},
257+
],
258+
},
259+
{
260+
options: [{ ignoreVoid: true }],
261+
code: `
262+
async function test() {
263+
Promise.resolve("value");
264+
}
265+
`,
266+
errors: [
267+
{
268+
line: 3,
269+
messageId: 'floatingVoid',
270+
suggestions: [
271+
{
272+
messageId: 'floatingFixVoid',
273+
output: `
274+
async function test() {
275+
void Promise.resolve("value");
276+
}
277+
`,
278+
},
279+
],
257280
},
258281
],
259282
},
@@ -268,15 +291,15 @@ async function test() {
268291
errors: [
269292
{
270293
line: 3,
271-
messageId,
294+
messageId: 'floating',
272295
},
273296
{
274297
line: 4,
275-
messageId,
298+
messageId: 'floating',
276299
},
277300
{
278301
line: 5,
279-
messageId,
302+
messageId: 'floating',
280303
},
281304
],
282305
},
@@ -291,15 +314,15 @@ async function test() {
291314
errors: [
292315
{
293316
line: 3,
294-
messageId,
317+
messageId: 'floating',
295318
},
296319
{
297320
line: 4,
298-
messageId,
321+
messageId: 'floating',
299322
},
300323
{
301324
line: 5,
302-
messageId,
325+
messageId: 'floating',
303326
},
304327
],
305328
},
@@ -316,15 +339,15 @@ async function test() {
316339
errors: [
317340
{
318341
line: 5,
319-
messageId,
342+
messageId: 'floating',
320343
},
321344
{
322345
line: 6,
323-
messageId,
346+
messageId: 'floating',
324347
},
325348
{
326349
line: 7,
327-
messageId,
350+
messageId: 'floating',
328351
},
329352
],
330353
},
@@ -338,11 +361,11 @@ async function test() {
338361
errors: [
339362
{
340363
line: 3,
341-
messageId,
364+
messageId: 'floating',
342365
},
343366
{
344367
line: 4,
345-
messageId,
368+
messageId: 'floating',
346369
},
347370
],
348371
},
@@ -357,15 +380,15 @@ async function test() {
357380
errors: [
358381
{
359382
line: 3,
360-
messageId,
383+
messageId: 'floating',
361384
},
362385
{
363386
line: 4,
364-
messageId,
387+
messageId: 'floating',
365388
},
366389
{
367390
line: 5,
368-
messageId,
391+
messageId: 'floating',
369392
},
370393
],
371394
},
@@ -378,7 +401,7 @@ async function test() {
378401
errors: [
379402
{
380403
line: 3,
381-
messageId,
404+
messageId: 'floating',
382405
},
383406
],
384407
},
@@ -392,7 +415,7 @@ async function test() {
392415
errors: [
393416
{
394417
line: 4,
395-
messageId,
418+
messageId: 'floating',
396419
},
397420
],
398421
},
@@ -405,7 +428,7 @@ async function test() {
405428
errors: [
406429
{
407430
line: 3,
408-
messageId,
431+
messageId: 'floating',
409432
},
410433
],
411434
},
@@ -422,15 +445,15 @@ async function test() {
422445
errors: [
423446
{
424447
line: 5,
425-
messageId,
448+
messageId: 'floating',
426449
},
427450
{
428451
line: 6,
429-
messageId,
452+
messageId: 'floating',
430453
},
431454
{
432455
line: 7,
433-
messageId,
456+
messageId: 'floating',
434457
},
435458
],
436459
},
@@ -445,7 +468,7 @@ async function test() {
445468
errors: [
446469
{
447470
line: 5,
448-
messageId,
471+
messageId: 'floating',
449472
},
450473
],
451474
},
@@ -462,15 +485,15 @@ async function test() {
462485
errors: [
463486
{
464487
line: 5,
465-
messageId,
488+
messageId: 'floating',
466489
},
467490
{
468491
line: 6,
469-
messageId,
492+
messageId: 'floating',
470493
},
471494
{
472495
line: 7,
473-
messageId,
496+
messageId: 'floating',
474497
},
475498
],
476499
},
@@ -488,15 +511,15 @@ async function test() {
488511
errors: [
489512
{
490513
line: 6,
491-
messageId,
514+
messageId: 'floating',
492515
},
493516
{
494517
line: 7,
495-
messageId,
518+
messageId: 'floating',
496519
},
497520
{
498521
line: 8,
499-
messageId,
522+
messageId: 'floating',
500523
},
501524
],
502525
},
@@ -517,11 +540,11 @@ async function test() {
517540
errors: [
518541
{
519542
line: 10,
520-
messageId,
543+
messageId: 'floating',
521544
},
522545
{
523546
line: 11,
524-
messageId,
547+
messageId: 'floating',
525548
},
526549
],
527550
},
@@ -551,15 +574,15 @@ async function test() {
551574
errors: [
552575
{
553576
line: 18,
554-
messageId,
577+
messageId: 'floating',
555578
},
556579
{
557580
line: 19,
558-
messageId,
581+
messageId: 'floating',
559582
},
560583
{
561584
line: 20,
562-
messageId,
585+
messageId: 'floating',
563586
},
564587
],
565588
},

0 commit comments

Comments
 (0)