is-promise.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Library: isPromise
  3. * Makes sure that an Expression node is part of a promise.
  4. */
  5. 'use strict'
  6. const PROMISE_STATICS = require('./promise-statics')
  7. function isPromise(expression) {
  8. return (
  9. // hello.then()
  10. (expression.type === 'CallExpression' &&
  11. expression.callee.type === 'MemberExpression' &&
  12. expression.callee.property.name === 'then') ||
  13. // hello.catch()
  14. (expression.type === 'CallExpression' &&
  15. expression.callee.type === 'MemberExpression' &&
  16. expression.callee.property.name === 'catch') ||
  17. // hello.finally()
  18. (expression.type === 'CallExpression' &&
  19. expression.callee.type === 'MemberExpression' &&
  20. expression.callee.property.name === 'finally') ||
  21. // somePromise.ANYTHING()
  22. (expression.type === 'CallExpression' &&
  23. expression.callee.type === 'MemberExpression' &&
  24. isPromise(expression.callee.object)) ||
  25. // Promise.STATIC_METHOD()
  26. (expression.type === 'CallExpression' &&
  27. expression.callee.type === 'MemberExpression' &&
  28. expression.callee.object.type === 'Identifier' &&
  29. expression.callee.object.name === 'Promise' &&
  30. PROMISE_STATICS[expression.callee.property.name])
  31. )
  32. }
  33. module.exports = isPromise