no-callback-in-promise.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. type: 'suggestion',
  13. docs: {
  14. url: getDocsUrl('no-callback-in-promise'),
  15. },
  16. messages: {
  17. callback: 'Avoid calling back inside of a promise.',
  18. },
  19. },
  20. create(context) {
  21. return {
  22. CallExpression(node) {
  23. const options = context.options[0] || {}
  24. const exceptions = options.exceptions || []
  25. if (!isCallback(node, exceptions)) {
  26. // in general we send you packing if you're not a callback
  27. // but we also need to watch out for whatever.then(cb)
  28. if (hasPromiseCallback(node)) {
  29. const name =
  30. node.arguments && node.arguments[0] && node.arguments[0].name
  31. if (
  32. name === 'callback' ||
  33. name === 'cb' ||
  34. name === 'next' ||
  35. name === 'done'
  36. ) {
  37. context.report({
  38. node: node.arguments[0],
  39. messageId: 'callback',
  40. })
  41. }
  42. }
  43. return
  44. }
  45. if (context.getAncestors().some(isInsidePromise)) {
  46. context.report({
  47. node,
  48. messageId: 'callback',
  49. })
  50. }
  51. },
  52. }
  53. },
  54. }