Skip to content

Commit

Permalink
feat(eslint-plugin): [no-float-prom] fixer + msg for ignoreVoid (#1473)
Browse files Browse the repository at this point in the history
  • Loading branch information
phaux committed Feb 3, 2020
1 parent b22424e commit 159b16e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 41 deletions.
35 changes: 30 additions & 5 deletions packages/eslint-plugin/src/rules/no-floating-promises.ts
@@ -1,5 +1,6 @@
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import { TSESLint } from '@typescript-eslint/experimental-utils';

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

Expand All @@ -9,7 +10,9 @@ type Options = [
},
];

export default util.createRule<Options, 'floating'>({
type MessageId = 'floating' | 'floatingVoid' | 'floatingFixVoid';

export default util.createRule<Options, MessageId>({
name: 'no-floating-promises',
meta: {
docs: {
Expand All @@ -20,6 +23,10 @@ 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',
floatingFixVoid: 'Add void operator to ignore',
},
schema: [
{
Expand All @@ -41,16 +48,34 @@ export default util.createRule<Options, 'floating'>({
create(context, [options]) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
const sourceCode = context.getSourceCode();

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

if (isUnhandledPromise(checker, expression)) {
context.report({
messageId: 'floating',
node,
});
if (options.ignoreVoid) {
context.report({
node,
messageId: 'floatingVoid',
suggest: [
{
messageId: 'floatingFixVoid',
fix(fixer): TSESLint.RuleFix {
let code = sourceCode.getText(node);
code = `void ${code}`;
return fixer.replaceText(node, code);
},
},
],
});
} else {
context.report({
node,
messageId: 'floating',
});
}
}
},
};
Expand Down
95 changes: 59 additions & 36 deletions packages/eslint-plugin/tests/rules/no-floating-promises.test.ts
Expand Up @@ -2,7 +2,6 @@ import rule from '../../src/rules/no-floating-promises';
import { RuleTester, getFixturesRootDir } from '../RuleTester';

const rootDir = getFixturesRootDir();
const messageId = 'floating';

const ruleTester = new RuleTester({
parserOptions: {
Expand Down Expand Up @@ -245,15 +244,39 @@ async function test() {
errors: [
{
line: 3,
messageId,
messageId: 'floating',
},
{
line: 4,
messageId,
messageId: 'floating',
},
{
line: 5,
messageId,
messageId: 'floating',
},
],
},
{
options: [{ ignoreVoid: true }],
code: `
async function test() {
Promise.resolve("value");
}
`,
errors: [
{
line: 3,
messageId: 'floatingVoid',
suggestions: [
{
messageId: 'floatingFixVoid',
output: `
async function test() {
void Promise.resolve("value");
}
`,
},
],
},
],
},
Expand All @@ -268,15 +291,15 @@ async function test() {
errors: [
{
line: 3,
messageId,
messageId: 'floating',
},
{
line: 4,
messageId,
messageId: 'floating',
},
{
line: 5,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -291,15 +314,15 @@ async function test() {
errors: [
{
line: 3,
messageId,
messageId: 'floating',
},
{
line: 4,
messageId,
messageId: 'floating',
},
{
line: 5,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -316,15 +339,15 @@ async function test() {
errors: [
{
line: 5,
messageId,
messageId: 'floating',
},
{
line: 6,
messageId,
messageId: 'floating',
},
{
line: 7,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -338,11 +361,11 @@ async function test() {
errors: [
{
line: 3,
messageId,
messageId: 'floating',
},
{
line: 4,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -357,15 +380,15 @@ async function test() {
errors: [
{
line: 3,
messageId,
messageId: 'floating',
},
{
line: 4,
messageId,
messageId: 'floating',
},
{
line: 5,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -378,7 +401,7 @@ async function test() {
errors: [
{
line: 3,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -392,7 +415,7 @@ async function test() {
errors: [
{
line: 4,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -405,7 +428,7 @@ async function test() {
errors: [
{
line: 3,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -422,15 +445,15 @@ async function test() {
errors: [
{
line: 5,
messageId,
messageId: 'floating',
},
{
line: 6,
messageId,
messageId: 'floating',
},
{
line: 7,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -445,7 +468,7 @@ async function test() {
errors: [
{
line: 5,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -462,15 +485,15 @@ async function test() {
errors: [
{
line: 5,
messageId,
messageId: 'floating',
},
{
line: 6,
messageId,
messageId: 'floating',
},
{
line: 7,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -488,15 +511,15 @@ async function test() {
errors: [
{
line: 6,
messageId,
messageId: 'floating',
},
{
line: 7,
messageId,
messageId: 'floating',
},
{
line: 8,
messageId,
messageId: 'floating',
},
],
},
Expand All @@ -517,11 +540,11 @@ async function test() {
errors: [
{
line: 10,
messageId,
messageId: 'floating',
},
{
line: 11,
messageId,
messageId: 'floating',
},
],
},
Expand Down Expand Up @@ -551,15 +574,15 @@ async function test() {
errors: [
{
line: 18,
messageId,
messageId: 'floating',
},
{
line: 19,
messageId,
messageId: 'floating',
},
{
line: 20,
messageId,
messageId: 'floating',
},
],
},
Expand Down

0 comments on commit 159b16e

Please sign in to comment.