Skip to content

⬅️ Back to Table of Contents

📄 promiseUtils.ts

📊 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): | {

  onFulfilled?: TSESTree.Expression | undefined;
  onRejected?: TSESTree.Expression | undefined;
  object: TSESTree.Expression;
}

| undefined`

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;
}
  • JSDoc:

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

  • Parameters:

  • node: TSESTree.CallExpression
  • context: RuleContext<string, unknown[]>
  • Return Type: | { onFulfilled?: TSESTree.Expression | undefined; onRejected?: TSESTree.Expression | undefined; object: TSESTree.Expression; } | undefined
  • Calls:
  • getStaticMemberAccessValue (from ./misc)

`parseCatchCall(node: TSESTree.CallExpression, context: RuleContext): | {

  onRejected?: TSESTree.Expression | undefined;
  object: TSESTree.Expression;
}

| undefined`

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;
}
  • JSDoc:

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

  • Parameters:

  • node: TSESTree.CallExpression
  • context: RuleContext<string, unknown[]>
  • Return Type: | { onRejected?: TSESTree.Expression | undefined; object: TSESTree.Expression; } | undefined
  • Calls:
  • getStaticMemberAccessValue (from ./misc)

`parseFinallyCall(node: TSESTree.CallExpression, context: RuleContext): | {

  object: TSESTree.Expression;
  onFinally?: TSESTree.Expression | undefined;
}

| undefined`

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;
}
  • JSDoc:

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

  • Parameters:

  • node: TSESTree.CallExpression
  • context: RuleContext<string, unknown[]>
  • Return Type: | { object: TSESTree.Expression; onFinally?: TSESTree.Expression | undefined; } | undefined
  • Calls:
  • getStaticMemberAccessValue (from ./misc)