no-promise-in-callback.js 1016 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Rule: no-promise-in-callback
  3. * Discourage using promises inside of callbacks.
  4. */
  5. 'use strict'
  6. const getDocsUrl = require('./lib/get-docs-url')
  7. const isPromise = require('./lib/is-promise')
  8. const isInsideCallback = require('./lib/is-inside-callback')
  9. module.exports = {
  10. meta: {
  11. type: 'suggestion',
  12. docs: {
  13. url: getDocsUrl('no-promise-in-callback'),
  14. },
  15. },
  16. create(context) {
  17. return {
  18. CallExpression(node) {
  19. if (!isPromise(node)) return
  20. // if i'm returning the promise, it's probably not really a callback
  21. // function, and I should be okay....
  22. if (node.parent.type === 'ReturnStatement') return
  23. // what about if the parent is an ArrowFunctionExpression
  24. // would that imply an implicit return?
  25. if (context.getAncestors().some(isInsideCallback)) {
  26. context.report({
  27. node: node.callee,
  28. message: 'Avoid using promises inside of callbacks.',
  29. })
  30. }
  31. },
  32. }
  33. },
  34. }