no-callback-in-promise.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Rule: no-callback-in-promise
  3. * Avoid calling back inside of a promise
  4. */
  5. 'use strict'
  6. const getDocsUrl = require('./lib/get-docs-url')
  7. const hasPromiseCallback = require('./lib/has-promise-callback')
  8. const isInsidePromise = require('./lib/is-inside-promise')
  9. const isCallback = require('./lib/is-callback')
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. url: getDocsUrl('no-callback-in-promise')
  14. }
  15. },
  16. create: function(context) {
  17. return {
  18. CallExpression: function(node) {
  19. const options = context.options[0] || {}
  20. const exceptions = options.exceptions || []
  21. if (!isCallback(node, exceptions)) {
  22. // in general we send you packing if you're not a callback
  23. // but we also need to watch out for whatever.then(cb)
  24. if (hasPromiseCallback(node)) {
  25. const name =
  26. node.arguments && node.arguments[0] && node.arguments[0].name
  27. if (
  28. name === 'callback' ||
  29. name === 'cb' ||
  30. name === 'next' ||
  31. name === 'done'
  32. ) {
  33. context.report({
  34. node: node.arguments[0],
  35. message: 'Avoid calling back inside of a promise.'
  36. })
  37. }
  38. }
  39. return
  40. }
  41. if (context.getAncestors().some(isInsidePromise)) {
  42. context.report({
  43. node,
  44. message: 'Avoid calling back inside of a promise.'
  45. })
  46. }
  47. }
  48. }
  49. }
  50. }