From ff3ce3d93e88e76e0f5b754fc1aa71b49e97e5fa Mon Sep 17 00:00:00 2001 From: Anix Date: Wed, 25 Mar 2020 19:18:32 +0000 Subject: [PATCH 01/15] fix: ignores iife for no-floating-promise (fixes #647) --- .../src/rules/no-floating-promises.ts | 18 ++++++++++++++++++ .../tests/rules/no-floating-promises.test.ts | 4 ---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-floating-promises.ts b/packages/eslint-plugin/src/rules/no-floating-promises.ts index c3ee7ffccf3c..2c5a41b10d65 100644 --- a/packages/eslint-plugin/src/rules/no-floating-promises.ts +++ b/packages/eslint-plugin/src/rules/no-floating-promises.ts @@ -12,6 +12,11 @@ type Options = [ type MessageId = 'floating' | 'floatingVoid' | 'floatingFixVoid'; +const possibleIifeCalleType = new Set([ + 'FunctionExpression', + 'ArrowFunctionExpression', +]); + export default util.createRule({ name: 'no-floating-promises', meta: { @@ -54,6 +59,10 @@ export default util.createRule({ ExpressionStatement(node): void { const { expression } = parserServices.esTreeNodeToTSNodeMap.get(node); + if (isIife(node)) { + return; + } + if (isUnhandledPromise(checker, expression)) { if (options.ignoreVoid) { context.report({ @@ -80,6 +89,15 @@ export default util.createRule({ }, }; + function isIife(node: ts.Node): Boolean { + if (node?.expression.type === 'CallExpression') { + if (possibleIifeCalleType.has(node?.expression?.callee.type)) { + return true; + } + } + return false; + } + function isUnhandledPromise( checker: ts.TypeChecker, node: ts.Node, diff --git a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts index c7e688c9852e..20953bcb2326 100644 --- a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts @@ -428,10 +428,6 @@ async function test() { } `, errors: [ - { - line: 3, - messageId: 'floating', - }, { line: 4, messageId: 'floating', From f52b591260dd40784fcc761e3be44f7412cbc328 Mon Sep 17 00:00:00 2001 From: Anix Date: Thu, 26 Mar 2020 21:22:23 +0000 Subject: [PATCH 02/15] feat: ignoreIIFE options and tests --- .../docs/rules/no-floating-promises.md | 29 ++++ .../src/rules/no-floating-promises.ts | 33 ++-- .../tests/rules/no-floating-promises.test.ts | 164 ++++++++++++++++++ 3 files changed, 213 insertions(+), 13 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.md b/packages/eslint-plugin/docs/rules/no-floating-promises.md index ffaa542f3f11..bd5d333f09a3 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.md +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.md @@ -51,6 +51,8 @@ The rule accepts an options object with the following properties: type Options = { // if true, checking void expressions will be skipped ignoreVoid?: boolean; + // if true, checking for async iife will be skipped + ignoreIIFE?: boolean; }; const defaults = { @@ -73,6 +75,33 @@ void returnsPromise(); void Promise.reject('value'); ``` +### `ignoreIIFE` + +This allows to skip checking of async iife + +Examples of **correct** code for this rule with `{ ignoreIIFE: true }`: + +```ts +(async function() { + await res(1); +})(); + +const foo = () => + new Promise(res => { + (async function() { + await res(1); + })(); + }); +``` + +Examples of **incorrect** code for this rule with `{ ignoreIIFE: true }`: + +```ts +(async function() { + Promise.resolve(); +})(); +``` + ## When Not To Use It If you do not use Promise-like values in your codebase or want to allow them to diff --git a/packages/eslint-plugin/src/rules/no-floating-promises.ts b/packages/eslint-plugin/src/rules/no-floating-promises.ts index 2c5a41b10d65..da755a5f0fa8 100644 --- a/packages/eslint-plugin/src/rules/no-floating-promises.ts +++ b/packages/eslint-plugin/src/rules/no-floating-promises.ts @@ -1,22 +1,22 @@ import * as tsutils from 'tsutils'; import * as ts from 'typescript'; -import { TSESLint } from '@typescript-eslint/experimental-utils'; +import { + TSESLint, + AST_NODE_TYPES, + TSESTree, +} from '@typescript-eslint/experimental-utils'; import * as util from '../util'; type Options = [ { ignoreVoid?: boolean; + ignoreIIFE?: boolean; }, ]; type MessageId = 'floating' | 'floatingVoid' | 'floatingFixVoid'; -const possibleIifeCalleType = new Set([ - 'FunctionExpression', - 'ArrowFunctionExpression', -]); - export default util.createRule({ name: 'no-floating-promises', meta: { @@ -38,6 +38,7 @@ export default util.createRule({ type: 'object', properties: { ignoreVoid: { type: 'boolean' }, + ignoreIIFE: { type: 'boolean' }, }, additionalProperties: false, }, @@ -47,6 +48,7 @@ export default util.createRule({ defaultOptions: [ { ignoreVoid: false, + ignoreIIFE: false, }, ], @@ -59,7 +61,7 @@ export default util.createRule({ ExpressionStatement(node): void { const { expression } = parserServices.esTreeNodeToTSNodeMap.get(node); - if (isIife(node)) { + if (options.ignoreIIFE && isAsyncIife(node)) { return; } @@ -89,13 +91,18 @@ export default util.createRule({ }, }; - function isIife(node: ts.Node): Boolean { - if (node?.expression.type === 'CallExpression') { - if (possibleIifeCalleType.has(node?.expression?.callee.type)) { - return true; - } + function isAsyncIife( + node: TSESTree.ExpressionStatement | TSESTree.ArrowFunctionExpression, + ): boolean { + if (node.expression.type !== AST_NODE_TYPES.CallExpression) { + return false; } - return false; + + return ( + node.expression.callee.type === + AST_NODE_TYPES.ArrowFunctionExpression || + node.expression.callee.type === AST_NODE_TYPES.FunctionExpression + ); } function isUnhandledPromise( diff --git a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts index 20953bcb2326..bc62770a7ae8 100644 --- a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts @@ -334,7 +334,53 @@ async function test() { returnsPromise()?.finally(() => {}); return returnsPromise(); } + + `, + // ignoreIIFE + { + code: ` + (async () => { + await something(); + })(); + `, + options: [{ ignoreIIFE: true }], + }, + { + code: ` + (async () => { + something(); + })(); + `, + options: [{ ignoreIIFE: true }], + }, + { + code: '(async function foo(){})();', + options: [{ ignoreIIFE: true }], + }, + { + code: ` + function foo(){ + (async function bar() { + })() + } `, + options: [{ ignoreIIFE: true }], + }, + { + code: `const foo = () => new Promise((res) => { + (async function(){ + await res(1) + })() + }) + `, + options: [{ ignoreIIFE: true }], + }, + { + code: `(async function(){ + await res(1) + })()`, + options: [{ ignoreIIFE: true }], + }, ], invalid: [ @@ -428,6 +474,10 @@ async function test() { } `, errors: [ + { + line: 3, + messageId: 'floating', + }, { line: 4, messageId: 'floating', @@ -722,5 +772,119 @@ async function test() { }, ], }, + { + code: ` + (async () => { + await something(); + })(); + `, + errors: [ + { + line: 2, + messageId: 'floating', + }, + ], + }, + { + code: ` + (async () => { + something(); + })(); + `, + errors: [ + { + line: 2, + messageId: 'floating', + }, + ], + }, + { + code: '(async function foo(){})();', + errors: [ + { + line: 1, + messageId: 'floating', + }, + ], + }, + { + code: ` + function foo(){ + (async function bar() { + })() + } + `, + errors: [ + { + line: 3, + messageId: 'floating', + }, + ], + }, + { + code: `const foo = () => new Promise((res) => { + (async function(){ + await res(1) + })() + }) + `, + errors: [ + { + line: 2, + messageId: 'floating', + }, + ], + }, + { + code: `(async function(){ + await res(1) + })()`, + errors: [ + { + line: 1, + messageId: 'floating', + }, + ], + }, + { + code: `(async function(){ + Promise.resolve(); + })()`, + options: [{ ignoreIIFE: true }], + errors: [ + { + line: 2, + messageId: 'floating', + }, + ], + }, + { + code: `(async function(){ + const promiseIntersection: Promise & number; + promiseIntersection; + promiseIntersection.then(() => {}) + promiseIntersection.catch(); + promiseIntersection.finally(); + })()`, + options: [{ ignoreIIFE: true }], + errors: [ + { + line: 3, + messageId: 'floating', + }, + { + line: 4, + messageId: 'floating', + }, + { + line: 5, + messageId: 'floating', + }, + { + line: 6, + messageId: 'floating', + }, + ], + }, ], }); From 7b754bb6578e305cf4f600e1f7d125a878b78dec Mon Sep 17 00:00:00 2001 From: Anix Date: Fri, 27 Mar 2020 18:01:18 +0000 Subject: [PATCH 03/15] chore: docs and typing updates --- packages/eslint-plugin/docs/rules/no-floating-promises.md | 4 ++-- packages/eslint-plugin/src/rules/no-floating-promises.ts | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.md b/packages/eslint-plugin/docs/rules/no-floating-promises.md index bd5d333f09a3..b8125ba8fad3 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.md +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.md @@ -82,7 +82,7 @@ This allows to skip checking of async iife Examples of **correct** code for this rule with `{ ignoreIIFE: true }`: ```ts -(async function() { +await(async function() { await res(1); })(); @@ -98,7 +98,7 @@ Examples of **incorrect** code for this rule with `{ ignoreIIFE: true }`: ```ts (async function() { - Promise.resolve(); + await Promise.resolve(); })(); ``` diff --git a/packages/eslint-plugin/src/rules/no-floating-promises.ts b/packages/eslint-plugin/src/rules/no-floating-promises.ts index da755a5f0fa8..6ef628ae13e5 100644 --- a/packages/eslint-plugin/src/rules/no-floating-promises.ts +++ b/packages/eslint-plugin/src/rules/no-floating-promises.ts @@ -91,9 +91,7 @@ export default util.createRule({ }, }; - function isAsyncIife( - node: TSESTree.ExpressionStatement | TSESTree.ArrowFunctionExpression, - ): boolean { + function isAsyncIife(node: TSESTree.ExpressionStatement): boolean { if (node.expression.type !== AST_NODE_TYPES.CallExpression) { return false; } From f70505369eacaa3f7ec000d089cd1656c00d0f51 Mon Sep 17 00:00:00 2001 From: Anix Date: Sun, 29 Mar 2020 15:44:55 +0000 Subject: [PATCH 04/15] chore: docs and types update --- packages/eslint-plugin/docs/rules/no-floating-promises.md | 2 +- packages/typescript-estree/src/ts-estree/ts-estree.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.md b/packages/eslint-plugin/docs/rules/no-floating-promises.md index b8125ba8fad3..4936bdcaa350 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.md +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.md @@ -97,7 +97,7 @@ const foo = () => Examples of **incorrect** code for this rule with `{ ignoreIIFE: true }`: ```ts -(async function() { +await(async function() { await Promise.resolve(); })(); ``` diff --git a/packages/typescript-estree/src/ts-estree/ts-estree.ts b/packages/typescript-estree/src/ts-estree/ts-estree.ts index cbf7787c8055..29ef38662877 100644 --- a/packages/typescript-estree/src/ts-estree/ts-estree.ts +++ b/packages/typescript-estree/src/ts-estree/ts-estree.ts @@ -393,7 +393,8 @@ export type LeftHandSideExpression = | PrimaryExpression | TaggedTemplateExpression | TSNonNullExpression - | TSAsExpression; + | TSAsExpression + | ArrowFunctionExpression; export type Literal = | BooleanLiteral | NumberLiteral From 03b18c73c458a6e599efe9917d5923c59eadc462 Mon Sep 17 00:00:00 2001 From: Anix Date: Mon, 30 Mar 2020 00:00:23 +0530 Subject: [PATCH 05/15] chore: formatting and docs refactore Co-Authored-By: Brad Zacher --- .../docs/rules/no-floating-promises.md | 2 +- .../tests/rules/no-floating-promises.test.ts | 86 ++++++++++--------- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.md b/packages/eslint-plugin/docs/rules/no-floating-promises.md index 4936bdcaa350..b8125ba8fad3 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.md +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.md @@ -97,7 +97,7 @@ const foo = () => Examples of **incorrect** code for this rule with `{ ignoreIIFE: true }`: ```ts -await(async function() { +(async function() { await Promise.resolve(); })(); ``` diff --git a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts index bc62770a7ae8..902339e33a25 100644 --- a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts @@ -339,18 +339,18 @@ async function test() { // ignoreIIFE { code: ` - (async () => { - await something(); - })(); + (async () => { + await something(); + })(); `, options: [{ ignoreIIFE: true }], }, { code: ` - (async () => { - something(); - })(); - `, + (async () => { + something(); + })(); + `, options: [{ ignoreIIFE: true }], }, { @@ -359,26 +359,28 @@ async function test() { }, { code: ` - function foo(){ - (async function bar() { - })() - } - `, + function foo(){ + (async function bar() {})() + } + `, options: [{ ignoreIIFE: true }], }, { - code: `const foo = () => new Promise((res) => { + code: ` + const foo = () => new Promise((res) => { (async function(){ await res(1) - })() + })() }) - `, + `, options: [{ ignoreIIFE: true }], }, { - code: `(async function(){ + code: ` + (async function(){ await res(1) - })()`, + })() + `, options: [{ ignoreIIFE: true }], }, ], @@ -774,9 +776,9 @@ async function test() { }, { code: ` - (async () => { - await something(); - })(); + (async () => { + await something(); + })(); `, errors: [ { @@ -787,10 +789,10 @@ async function test() { }, { code: ` - (async () => { - something(); - })(); - `, + (async () => { + something(); + })(); + `, errors: [ { line: 2, @@ -809,11 +811,10 @@ async function test() { }, { code: ` - function foo(){ - (async function bar() { - })() - } - `, + function foo(){ + (async function bar() {})() + } + `, errors: [ { line: 3, @@ -822,11 +823,12 @@ async function test() { ], }, { - code: `const foo = () => new Promise((res) => { - (async function(){ + code: ` + const foo = () => new Promise((res) => { + (async function(){ await res(1) })() - }) + }) `, errors: [ { @@ -836,9 +838,11 @@ async function test() { ], }, { - code: `(async function(){ - await res(1) - })()`, + code: ` + (async function(){ + await res(1) + })() + `, errors: [ { line: 1, @@ -847,9 +851,11 @@ async function test() { ], }, { - code: `(async function(){ + code: ` + (async function(){ Promise.resolve(); - })()`, + })() + `, options: [{ ignoreIIFE: true }], errors: [ { @@ -859,13 +865,15 @@ async function test() { ], }, { - code: `(async function(){ + code: ` + (async function(){ const promiseIntersection: Promise & number; promiseIntersection; promiseIntersection.then(() => {}) promiseIntersection.catch(); promiseIntersection.finally(); - })()`, + })() + `, options: [{ ignoreIIFE: true }], errors: [ { From 69864051ef5799c820770ec520984d7a24aeb919 Mon Sep 17 00:00:00 2001 From: Anix Date: Mon, 30 Mar 2020 00:01:03 +0530 Subject: [PATCH 06/15] chore: docs formatting Co-Authored-By: Brad Zacher --- packages/eslint-plugin/docs/rules/no-floating-promises.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.md b/packages/eslint-plugin/docs/rules/no-floating-promises.md index b8125ba8fad3..37013ca49998 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.md +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.md @@ -82,7 +82,7 @@ This allows to skip checking of async iife Examples of **correct** code for this rule with `{ ignoreIIFE: true }`: ```ts -await(async function() { +await (async function() { await res(1); })(); From 325d9d89a5c970463ae8311d2c33bf4cb4a903ee Mon Sep 17 00:00:00 2001 From: Anix Date: Wed, 1 Apr 2020 12:41:26 +0000 Subject: [PATCH 07/15] chore: test refactore and typecheck fix --- .../src/rules/no-floating-promises.ts | 5 +++-- .../tests/rules/no-floating-promises.test.ts | 14 +++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-floating-promises.ts b/packages/eslint-plugin/src/rules/no-floating-promises.ts index 6ef628ae13e5..09d70bac42c0 100644 --- a/packages/eslint-plugin/src/rules/no-floating-promises.ts +++ b/packages/eslint-plugin/src/rules/no-floating-promises.ts @@ -97,9 +97,10 @@ export default util.createRule({ } return ( - node.expression.callee.type === + node.expression.type === AST_NODE_TYPES.CallExpression && + (node.expression.callee.type === AST_NODE_TYPES.ArrowFunctionExpression || - node.expression.callee.type === AST_NODE_TYPES.FunctionExpression + node.expression.callee.type === AST_NODE_TYPES.FunctionExpression) ); } diff --git a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts index 902339e33a25..0330e784226a 100644 --- a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts @@ -832,7 +832,7 @@ async function test() { `, errors: [ { - line: 2, + line: 3, messageId: 'floating', }, ], @@ -845,7 +845,7 @@ async function test() { `, errors: [ { - line: 1, + line: 2, messageId: 'floating', }, ], @@ -859,7 +859,7 @@ async function test() { options: [{ ignoreIIFE: true }], errors: [ { - line: 2, + line: 3, messageId: 'floating', }, ], @@ -876,10 +876,6 @@ async function test() { `, options: [{ ignoreIIFE: true }], errors: [ - { - line: 3, - messageId: 'floating', - }, { line: 4, messageId: 'floating', @@ -892,6 +888,10 @@ async function test() { line: 6, messageId: 'floating', }, + { + line: 7, + messageId: 'floating', + }, ], }, ], From 4b697172c49997a1260a98e91d21a528f0293a16 Mon Sep 17 00:00:00 2001 From: Anix Date: Wed, 1 Apr 2020 13:22:08 +0000 Subject: [PATCH 08/15] docs: docs refactore --- packages/eslint-plugin/docs/rules/no-floating-promises.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.md b/packages/eslint-plugin/docs/rules/no-floating-promises.md index 37013ca49998..b8125ba8fad3 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.md +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.md @@ -82,7 +82,7 @@ This allows to skip checking of async iife Examples of **correct** code for this rule with `{ ignoreIIFE: true }`: ```ts -await (async function() { +await(async function() { await res(1); })(); From 2c8315faf1e8500aa08f36bed0a99043e108cb46 Mon Sep 17 00:00:00 2001 From: Anix Date: Sat, 4 Apr 2020 07:02:17 +0000 Subject: [PATCH 09/15] chore: linting fixes --- .../tests/rules/no-floating-promises.test.ts | 59 ++++++++++--------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts index 0330e784226a..28635a090b1a 100644 --- a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts @@ -334,8 +334,7 @@ async function test() { returnsPromise()?.finally(() => {}); return returnsPromise(); } - - `, + `, // ignoreIIFE { code: ` @@ -354,32 +353,33 @@ async function test() { options: [{ ignoreIIFE: true }], }, { - code: '(async function foo(){})();', + code: '(async function foo() {})();', options: [{ ignoreIIFE: true }], }, { code: ` - function foo(){ - (async function bar() {})() + function foo() { + (async function bar() {})(); } `, options: [{ ignoreIIFE: true }], }, { code: ` - const foo = () => new Promise((res) => { - (async function(){ - await res(1) - })() - }) + const foo = () => + new Promise(res => { + (async function() { + await res(1); + })(); + }); `, options: [{ ignoreIIFE: true }], }, { code: ` - (async function(){ - await res(1) - })() + (async function() { + await res(1); + })(); `, options: [{ ignoreIIFE: true }], }, @@ -801,7 +801,7 @@ async function test() { ], }, { - code: '(async function foo(){})();', + code: '(async function foo() {})();', errors: [ { line: 1, @@ -811,8 +811,8 @@ async function test() { }, { code: ` - function foo(){ - (async function bar() {})() + function foo() { + (async function bar() {})(); } `, errors: [ @@ -824,11 +824,12 @@ async function test() { }, { code: ` - const foo = () => new Promise((res) => { - (async function(){ - await res(1) - })() - }) + const foo = () => + new Promise(res => { + (async function() { + await res(1); + })(); + }); `, errors: [ { @@ -839,9 +840,9 @@ async function test() { }, { code: ` - (async function(){ - await res(1) - })() + (async function() { + await res(1); + })(); `, errors: [ { @@ -852,9 +853,9 @@ async function test() { }, { code: ` - (async function(){ + (async function() { Promise.resolve(); - })() + })(); `, options: [{ ignoreIIFE: true }], errors: [ @@ -866,13 +867,13 @@ async function test() { }, { code: ` - (async function(){ + (async function() { const promiseIntersection: Promise & number; promiseIntersection; - promiseIntersection.then(() => {}) + promiseIntersection.then(() => {}); promiseIntersection.catch(); promiseIntersection.finally(); - })() + })(); `, options: [{ ignoreIIFE: true }], errors: [ From 920ebddf199eb196735a1acc754ee7559529404c Mon Sep 17 00:00:00 2001 From: Anix Date: Sat, 4 Apr 2020 07:14:26 +0000 Subject: [PATCH 10/15] chore: test fix --- packages/eslint-plugin/tests/rules/no-floating-promises.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts index 28635a090b1a..f6601a0613a1 100644 --- a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts @@ -833,7 +833,7 @@ async function test() { `, errors: [ { - line: 3, + line: 4, messageId: 'floating', }, ], From a26861bf5cb2d836eb1cec3bd6284cc1c151380d Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 19 Apr 2020 22:09:19 -0700 Subject: [PATCH 11/15] Update no-floating-promises.md --- .../docs/rules/no-floating-promises.md | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.md b/packages/eslint-plugin/docs/rules/no-floating-promises.md index b8125ba8fad3..d94f2b4cbf4f 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.md +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.md @@ -62,7 +62,8 @@ const defaults = { ### `ignoreVoid` -This allows to easily suppress false-positives with void operator. +This allows you to stop the rule reporting promises consumed with void operator. +This can be a good way to explicitly mark a promise as intentially not awaited. Examples of **correct** code for this rule with `{ ignoreVoid: true }`: @@ -77,35 +78,23 @@ void Promise.reject('value'); ### `ignoreIIFE` -This allows to skip checking of async iife +This allows you to skip checking of async iife Examples of **correct** code for this rule with `{ ignoreIIFE: true }`: ```ts -await(async function() { +await (async function() { await res(1); })(); -const foo = () => - new Promise(res => { - (async function() { - await res(1); - })(); - }); -``` - -Examples of **incorrect** code for this rule with `{ ignoreIIFE: true }`: - -```ts (async function() { - await Promise.resolve(); + await res(1); })(); ``` ## When Not To Use It -If you do not use Promise-like values in your codebase or want to allow them to -remain unhandled. +If you do not use Promise-like values in your codebase, or want to allow them to remain unhandled. ## Related to From 6548b1cedee2a289b5ed3685b92b24c2f6d6b4e6 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 19 Apr 2020 22:12:06 -0700 Subject: [PATCH 12/15] Update no-floating-promises.md --- .../docs/rules/no-floating-promises.md | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.md b/packages/eslint-plugin/docs/rules/no-floating-promises.md index d94f2b4cbf4f..134ef43507bf 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.md +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.md @@ -11,36 +11,36 @@ either calling `.then()` with two arguments or `.catch()` with one argument. Examples of **incorrect** code for this rule: ```ts -const promise = new Promise((resolve, reject) => resolve('value')); +const promise = new Promise((resolve, reject) => resolve("value")); promise; async function returnsPromise() { - return 'value'; + return "value"; } returnsPromise().then(() => {}); -Promise.reject('value').catch(); +Promise.reject("value").catch(); -Promise.reject('value').finally(); +Promise.reject("value").finally(); ``` Examples of **correct** code for this rule: ```ts -const promise = new Promise((resolve, reject) => resolve('value')); +const promise = new Promise((resolve, reject) => resolve("value")); await promise; async function returnsPromise() { - return 'value'; + return "value"; } returnsPromise().then( () => {}, - () => {}, + () => {} ); -Promise.reject('value').catch(() => {}); +Promise.reject("value").catch(() => {}); -Promise.reject('value').finally(() => {}); +Promise.reject("value").finally(() => {}); ``` ## Options @@ -69,11 +69,11 @@ Examples of **correct** code for this rule with `{ ignoreVoid: true }`: ```ts async function returnsPromise() { - return 'value'; + return "value"; } void returnsPromise(); -void Promise.reject('value'); +void Promise.reject("value"); ``` ### `ignoreIIFE` @@ -83,11 +83,11 @@ This allows you to skip checking of async iife Examples of **correct** code for this rule with `{ ignoreIIFE: true }`: ```ts -await (async function() { +await(async function () { await res(1); })(); -(async function() { +(async function () { await res(1); })(); ``` From 2c2155ea36cfca30ebf31480d88b7d7eeb2462de Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 19 Apr 2020 22:13:11 -0700 Subject: [PATCH 13/15] Update no-floating-promises.md --- .../docs/rules/no-floating-promises.md | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.md b/packages/eslint-plugin/docs/rules/no-floating-promises.md index 134ef43507bf..d2aa977884c0 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.md +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.md @@ -11,36 +11,36 @@ either calling `.then()` with two arguments or `.catch()` with one argument. Examples of **incorrect** code for this rule: ```ts -const promise = new Promise((resolve, reject) => resolve("value")); +const promise = new Promise((resolve, reject) => resolve('value')); promise; async function returnsPromise() { - return "value"; + return 'value'; } returnsPromise().then(() => {}); -Promise.reject("value").catch(); +Promise.reject('value').catch(); -Promise.reject("value").finally(); +Promise.reject('value').finally(); ``` Examples of **correct** code for this rule: ```ts -const promise = new Promise((resolve, reject) => resolve("value")); +const promise = new Promise((resolve, reject) => resolve('value')); await promise; async function returnsPromise() { - return "value"; + return 'value'; } returnsPromise().then( () => {}, - () => {} + () => {}, ); -Promise.reject("value").catch(() => {}); +Promise.reject('value').catch(() => {}); -Promise.reject("value").finally(() => {}); +Promise.reject('value').finally(() => {}); ``` ## Options @@ -69,11 +69,11 @@ Examples of **correct** code for this rule with `{ ignoreVoid: true }`: ```ts async function returnsPromise() { - return "value"; + return 'value'; } void returnsPromise(); -void Promise.reject("value"); +void Promise.reject('value'); ``` ### `ignoreIIFE` From b741c77d9dfdc0d92979bfb69f54abeec6b6ad67 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 19 Apr 2020 22:20:41 -0700 Subject: [PATCH 14/15] Update no-floating-promises.md --- packages/eslint-plugin/docs/rules/no-floating-promises.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.md b/packages/eslint-plugin/docs/rules/no-floating-promises.md index d2aa977884c0..1e6ba6f8238e 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.md +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.md @@ -83,11 +83,11 @@ This allows you to skip checking of async iife Examples of **correct** code for this rule with `{ ignoreIIFE: true }`: ```ts -await(async function () { +await(async function() { await res(1); })(); -(async function () { +(async function() { await res(1); })(); ``` From 05ed5f668a67eaa8795846900438405643690256 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 19 Apr 2020 22:29:37 -0700 Subject: [PATCH 15/15] Update no-floating-promises.md --- packages/eslint-plugin/docs/rules/no-floating-promises.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.md b/packages/eslint-plugin/docs/rules/no-floating-promises.md index 1e6ba6f8238e..4ea2e6addc99 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.md +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.md @@ -63,7 +63,7 @@ const defaults = { ### `ignoreVoid` This allows you to stop the rule reporting promises consumed with void operator. -This can be a good way to explicitly mark a promise as intentially not awaited. +This can be a good way to explicitly mark a promise as intentionally not awaited. Examples of **correct** code for this rule with `{ ignoreVoid: true }`: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy