valid-params.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict'
  2. const getDocsUrl = require('./lib/get-docs-url')
  3. const isPromise = require('./lib/is-promise')
  4. module.exports = {
  5. meta: {
  6. type: 'problem',
  7. docs: {
  8. description:
  9. 'Ensures the proper number of arguments are passed to Promise functions',
  10. url: getDocsUrl('valid-params'),
  11. },
  12. },
  13. create(context) {
  14. return {
  15. CallExpression(node) {
  16. if (!isPromise(node)) {
  17. return
  18. }
  19. const name = node.callee.property.name
  20. const numArgs = node.arguments.length
  21. // istanbul ignore next -- `isPromise` filters out others
  22. switch (name) {
  23. case 'resolve':
  24. case 'reject':
  25. if (numArgs > 1) {
  26. context.report({
  27. node,
  28. message:
  29. 'Promise.{{ name }}() requires 0 or 1 arguments, but received {{ numArgs }}',
  30. data: { name, numArgs },
  31. })
  32. }
  33. break
  34. case 'then':
  35. if (numArgs < 1 || numArgs > 2) {
  36. context.report({
  37. node,
  38. message:
  39. 'Promise.{{ name }}() requires 1 or 2 arguments, but received {{ numArgs }}',
  40. data: { name, numArgs },
  41. })
  42. }
  43. break
  44. case 'race':
  45. case 'all':
  46. case 'catch':
  47. case 'finally':
  48. if (numArgs !== 1) {
  49. context.report({
  50. node,
  51. message:
  52. 'Promise.{{ name }}() requires 1 argument, but received {{ numArgs }}',
  53. data: { name, numArgs },
  54. })
  55. }
  56. break
  57. default:
  58. // istanbul ignore next -- `isPromise` filters out others
  59. break
  60. }
  61. },
  62. }
  63. },
  64. }