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 all commits
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
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