param-names.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict'
  2. const getDocsUrl = require('./lib/get-docs-url')
  3. const {
  4. isPromiseConstructorWithInlineExecutor,
  5. } = require('./lib/is-promise-constructor')
  6. module.exports = {
  7. meta: {
  8. type: 'suggestion',
  9. docs: {
  10. url: getDocsUrl('param-names'),
  11. },
  12. schema: [
  13. {
  14. type: 'object',
  15. properties: {
  16. resolvePattern: { type: 'string' },
  17. rejectPattern: { type: 'string' },
  18. },
  19. additionalProperties: false,
  20. },
  21. ],
  22. },
  23. create(context) {
  24. const options = context.options[0] || {}
  25. const resolvePattern = new RegExp(
  26. options.resolvePattern || '^_?resolve$',
  27. 'u'
  28. )
  29. const rejectPattern = new RegExp(options.rejectPattern || '^_?reject$', 'u')
  30. return {
  31. NewExpression(node) {
  32. if (isPromiseConstructorWithInlineExecutor(node)) {
  33. const params = node.arguments[0].params
  34. if (!params || !params.length) {
  35. return
  36. }
  37. const resolveParamName = params[0] && params[0].name
  38. if (resolveParamName && !resolvePattern.test(resolveParamName)) {
  39. context.report({
  40. node: params[0],
  41. message:
  42. 'Promise constructor parameters must be named to match "{{ resolvePattern }}"',
  43. data: {
  44. resolvePattern: resolvePattern.source,
  45. },
  46. })
  47. }
  48. const rejectParamName = params[1] && params[1].name
  49. if (rejectParamName && !rejectPattern.test(rejectParamName)) {
  50. context.report({
  51. node: params[1],
  52. message:
  53. 'Promise constructor parameters must be named to match "{{ rejectPattern }}"',
  54. data: {
  55. rejectPattern: rejectPattern.source,
  56. },
  57. })
  58. }
  59. }
  60. },
  61. }
  62. },
  63. }