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 Apr 1, 2020
1 parent 188b689 commit ff3ce3d
Show file tree
Hide file tree
Showing 2 changed files with 18 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
Expand Up @@ -428,10 +428,6 @@ async function test() {
}
`,
errors: [
{
line: 3,
messageId: 'floating',
},
{
line: 4,
messageId: 'floating',
Expand Down

0 comments on commit ff3ce3d

Please sign in to comment.