no-promise-in-callback.js 1007 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. docs: {
  12. url: getDocsUrl('no-promise-in-callback')
  13. }
  14. },
  15. create: function(context) {
  16. return {
  17. CallExpression: function(node) {
  18. if (!isPromise(node)) return
  19. // if i'm returning the promise, it's probably not really a callback
  20. // function, and I should be okay....
  21. if (node.parent.type === 'ReturnStatement') return
  22. // what about if the parent is an ArrowFunctionExpression
  23. // would that imply an implicit return?
  24. if (context.getAncestors().some(isInsideCallback)) {
  25. context.report({
  26. node: node.callee,
  27. message: 'Avoid using promises inside of callbacks.'
  28. })
  29. }
  30. }
  31. }
  32. }
  33. }