Skip to content

⬅️ Back to Table of Contents

πŸ“„ promiseUtils

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 3
πŸ“¦ Imports 4

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/util/promiseUtils.ts

πŸ“¦ Imports

Name Source
TSESTree @typescript-eslint/utils
RuleContext @typescript-eslint/utils/ts-eslint
AST_NODE_TYPES @typescript-eslint/utils
getStaticMemberAccessValue ./misc

Functions

parseThenCall(node: TSESTree.CallExpression, context: RuleContext<string, unknown[]>): | { onFulfilled?: TSESTree.Expression | undefined; onReject…

Parses a syntactically possible Promise.then() call. Does not check the type of the callee.

Raw JSDoc
/**
 * Parses a syntactically possible `Promise.then()` call. Does not check the
 * type of the callee.
 */

Calls:

  • getStaticMemberAccessValue (from ./misc)
Code
export function parseThenCall(
  node: TSESTree.CallExpression,
  context: RuleContext<string, unknown[]>,
):
  | {
      onFulfilled?: TSESTree.Expression | undefined;
      onRejected?: TSESTree.Expression | undefined;
      object: TSESTree.Expression;
    }
  | undefined {
  if (node.callee.type === AST_NODE_TYPES.MemberExpression) {
    const methodName = getStaticMemberAccessValue(node.callee, context);
    if (methodName === 'then') {
      if (node.arguments.length >= 1) {
        if (node.arguments[0].type === AST_NODE_TYPES.SpreadElement) {
          return {
            object: node.callee.object,
          };
        }

        if (node.arguments.length >= 2) {
          if (node.arguments[1].type === AST_NODE_TYPES.SpreadElement) {
            return {
              object: node.callee.object,
              onFulfilled: node.arguments[0],
            };
          }

          return {
            object: node.callee.object,
            onFulfilled: node.arguments[0],
            onRejected: node.arguments[1],
          };
        }
        return {
          object: node.callee.object,
          onFulfilled: node.arguments[0],
        };
      }
      return {
        object: node.callee.object,
      };
    }
  }

  return undefined;
}

parseCatchCall(node: TSESTree.CallExpression, context: RuleContext<string, unknown[]>): | { onRejected?: TSESTree.Expression | undefined; object: T…

Parses a syntactically possible Promise.catch() call. Does not check the type of the callee.

Raw JSDoc
/**
 * Parses a syntactically possible `Promise.catch()` call. Does not check the
 * type of the callee.
 */

Calls:

  • getStaticMemberAccessValue (from ./misc)
Code
export function parseCatchCall(
  node: TSESTree.CallExpression,
  context: RuleContext<string, unknown[]>,
):
  | {
      onRejected?: TSESTree.Expression | undefined;
      object: TSESTree.Expression;
    }
  | undefined {
  if (node.callee.type === AST_NODE_TYPES.MemberExpression) {
    const methodName = getStaticMemberAccessValue(node.callee, context);
    if (methodName === 'catch') {
      if (node.arguments.length >= 1) {
        if (node.arguments[0].type === AST_NODE_TYPES.SpreadElement) {
          return {
            object: node.callee.object,
          };
        }

        return {
          object: node.callee.object,
          onRejected: node.arguments[0],
        };
      }
      return {
        object: node.callee.object,
      };
    }
  }

  return undefined;
}

parseFinallyCall(node: TSESTree.CallExpression, context: RuleContext<string, unknown[]>): | { object: TSESTree.Expression; onFinally?: TSESTree.Expre…

Parses a syntactically possible Promise.finally() call. Does not check the type of the callee.

Raw JSDoc
/**
 * Parses a syntactically possible `Promise.finally()` call. Does not check the
 * type of the callee.
 */

Calls:

  • getStaticMemberAccessValue (from ./misc)
Code
export function parseFinallyCall(
  node: TSESTree.CallExpression,
  context: RuleContext<string, unknown[]>,
):
  | {
      object: TSESTree.Expression;
      onFinally?: TSESTree.Expression | undefined;
    }
  | undefined {
  if (node.callee.type === AST_NODE_TYPES.MemberExpression) {
    const methodName = getStaticMemberAccessValue(node.callee, context);
    if (methodName === 'finally') {
      if (node.arguments.length >= 1) {
        if (node.arguments[0].type === AST_NODE_TYPES.SpreadElement) {
          return {
            object: node.callee.object,
          };
        }
        return {
          object: node.callee.object,
          onFinally: node.arguments[0],
        };
      }
      return {
        object: node.callee.object,
      };
    }
  }

  return undefined;
}

Generated by Syntax Scribe