no-callback-literal.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Ensures that the callback pattern is followed properly
  3. * with an Error object (or undefined or null) in the first position.
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Helpers
  8. // ------------------------------------------------------------------------------
  9. /**
  10. * Determine if a node has a possiblity to be an Error object
  11. * @param {ASTNode} node ASTNode to check
  12. * @returns {boolean} True if there is a chance it contains an Error obj
  13. */
  14. function couldBeError (node) {
  15. switch (node.type) {
  16. case 'Identifier':
  17. case 'CallExpression':
  18. case 'NewExpression':
  19. case 'MemberExpression':
  20. case 'TaggedTemplateExpression':
  21. case 'YieldExpression':
  22. return true // possibly an error object.
  23. case 'AssignmentExpression':
  24. return couldBeError(node.right)
  25. case 'SequenceExpression':
  26. var exprs = node.expressions
  27. return exprs.length !== 0 && couldBeError(exprs[exprs.length - 1])
  28. case 'LogicalExpression':
  29. return couldBeError(node.left) || couldBeError(node.right)
  30. case 'ConditionalExpression':
  31. return couldBeError(node.consequent) || couldBeError(node.alternate)
  32. default:
  33. return node.value === null
  34. }
  35. }
  36. // ------------------------------------------------------------------------------
  37. // Rule Definition
  38. // ------------------------------------------------------------------------------
  39. module.exports = {
  40. meta: {
  41. docs: {
  42. url: 'https://github.com/xjamundx/eslint-plugin-standard#rules-explanations'
  43. }
  44. },
  45. create: function (context) {
  46. var callbackNames = context.options[0] || ['callback', 'cb']
  47. function isCallback (name) {
  48. return callbackNames.indexOf(name) > -1
  49. }
  50. return {
  51. CallExpression: function (node) {
  52. var errorArg = node.arguments[0]
  53. var calleeName = node.callee.name
  54. if (errorArg && !couldBeError(errorArg) && isCallback(calleeName)) {
  55. context.report(node, 'Unexpected literal in error position of callback.')
  56. }
  57. }
  58. }
  59. }
  60. }