Skip to content

Commit

Permalink
fix: ignores iife for no-floating-promise (fixes typescript-eslint#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed Mar 26, 2020
1 parent 7d963fd commit a104a85
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
18 changes: 18 additions & 0 deletions packages/eslint-plugin/src/rules/no-floating-promises.ts
Expand Up @@ -12,6 +12,11 @@ type Options = [

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

const possibleIifeCalleType = new Set([
'FunctionExpression',
'ArrowFunctionExpression',
]);

export default util.createRule<Options, MessageId>({
name: 'no-floating-promises',
meta: {
Expand Down Expand Up @@ -54,6 +59,10 @@ export default util.createRule<Options, MessageId>({
ExpressionStatement(node): void {
const { expression } = parserServices.esTreeNodeToTSNodeMap.get(node);

if (isIife(node)) {
return;
}

if (isUnhandledPromise(checker, expression)) {
if (options.ignoreVoid) {
context.report({
Expand All @@ -80,6 +89,15 @@ export default util.createRule<Options, MessageId>({
},
};

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,
Expand Down
28 changes: 24 additions & 4 deletions packages/eslint-plugin/tests/rules/no-floating-promises.test.ts
Expand Up @@ -251,6 +251,30 @@ async function test() {
return returnsPromise();
}
`,
`
(async () => {
await something();
})();
`,
`
(async () => {
something();
})();
`,
'(async function foo(){})();',
`
function foo(){
(async function bar() {
})()
}
`,
`
const foo = () => new Promise((res) => {
(async function(){
await res(1)
})()
})
`,
],

invalid: [
Expand Down Expand Up @@ -344,10 +368,6 @@ async function test() {
}
`,
errors: [
{
line: 3,
messageId: 'floating',
},
{
line: 4,
messageId: 'floating',
Expand Down

0 comments on commit a104a85

Please sign in to comment.