valid-describe-callback.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _experimentalUtils = require("@typescript-eslint/experimental-utils");
  7. var _utils = require("./utils");
  8. const paramsLocation = params => {
  9. const [first] = params;
  10. const last = params[params.length - 1];
  11. return {
  12. start: first.loc.start,
  13. end: last.loc.end
  14. };
  15. };
  16. var _default = (0, _utils.createRule)({
  17. name: __filename,
  18. meta: {
  19. type: 'problem',
  20. docs: {
  21. category: 'Possible Errors',
  22. description: 'Enforce valid `describe()` callback',
  23. recommended: 'error'
  24. },
  25. messages: {
  26. nameAndCallback: 'Describe requires name and callback arguments',
  27. secondArgumentMustBeFunction: 'Second argument must be function',
  28. noAsyncDescribeCallback: 'No async describe callback',
  29. unexpectedDescribeArgument: 'Unexpected argument(s) in describe callback',
  30. unexpectedReturnInDescribe: 'Unexpected return statement in describe callback'
  31. },
  32. schema: []
  33. },
  34. defaultOptions: [],
  35. create(context) {
  36. return {
  37. CallExpression(node) {
  38. if (!(0, _utils.isDescribeCall)(node)) {
  39. return;
  40. }
  41. if (node.arguments.length < 1) {
  42. return context.report({
  43. messageId: 'nameAndCallback',
  44. loc: node.loc
  45. });
  46. }
  47. const [, callback] = node.arguments;
  48. if (!callback) {
  49. context.report({
  50. messageId: 'nameAndCallback',
  51. loc: paramsLocation(node.arguments)
  52. });
  53. return;
  54. }
  55. if (!(0, _utils.isFunction)(callback)) {
  56. context.report({
  57. messageId: 'secondArgumentMustBeFunction',
  58. loc: paramsLocation(node.arguments)
  59. });
  60. return;
  61. }
  62. if (callback.async) {
  63. context.report({
  64. messageId: 'noAsyncDescribeCallback',
  65. node: callback
  66. });
  67. }
  68. if (!(0, _utils.getNodeName)(node).endsWith('each') && callback.params.length) {
  69. context.report({
  70. messageId: 'unexpectedDescribeArgument',
  71. loc: paramsLocation(callback.params)
  72. });
  73. }
  74. if (callback.body.type === _experimentalUtils.AST_NODE_TYPES.CallExpression) {
  75. context.report({
  76. messageId: 'unexpectedReturnInDescribe',
  77. node: callback
  78. });
  79. }
  80. if (callback.body.type === _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
  81. callback.body.body.forEach(node => {
  82. if (node.type === _experimentalUtils.AST_NODE_TYPES.ReturnStatement) {
  83. context.report({
  84. messageId: 'unexpectedReturnInDescribe',
  85. node
  86. });
  87. }
  88. });
  89. }
  90. }
  91. };
  92. }
  93. });
  94. exports.default = _default;