max-nested-describe.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. var _default = (0, _utils.createRule)({
  9. name: __filename,
  10. meta: {
  11. docs: {
  12. category: 'Best Practices',
  13. description: 'Enforces a maximum depth to nested describe calls',
  14. recommended: false
  15. },
  16. messages: {
  17. exceededMaxDepth: 'Too many nested describe calls ({{ depth }}). Maximum allowed is {{ max }}.'
  18. },
  19. type: 'suggestion',
  20. schema: [{
  21. type: 'object',
  22. properties: {
  23. max: {
  24. type: 'integer',
  25. minimum: 0
  26. }
  27. },
  28. additionalProperties: false
  29. }]
  30. },
  31. defaultOptions: [{
  32. max: 5
  33. }],
  34. create(context, [{
  35. max
  36. }]) {
  37. const describeCallbackStack = [];
  38. function pushDescribeCallback(node) {
  39. const {
  40. parent
  41. } = node;
  42. if ((parent === null || parent === void 0 ? void 0 : parent.type) !== _experimentalUtils.AST_NODE_TYPES.CallExpression || !(0, _utils.isDescribeCall)(parent)) {
  43. return;
  44. }
  45. describeCallbackStack.push(0);
  46. if (describeCallbackStack.length > max) {
  47. context.report({
  48. node: parent,
  49. messageId: 'exceededMaxDepth',
  50. data: {
  51. depth: describeCallbackStack.length,
  52. max
  53. }
  54. });
  55. }
  56. }
  57. function popDescribeCallback(node) {
  58. const {
  59. parent
  60. } = node;
  61. if ((parent === null || parent === void 0 ? void 0 : parent.type) === _experimentalUtils.AST_NODE_TYPES.CallExpression && (0, _utils.isDescribeCall)(parent)) {
  62. describeCallbackStack.pop();
  63. }
  64. }
  65. return {
  66. FunctionExpression: pushDescribeCallback,
  67. 'FunctionExpression:exit': popDescribeCallback,
  68. ArrowFunctionExpression: pushDescribeCallback,
  69. 'ArrowFunctionExpression:exit': popDescribeCallback
  70. };
  71. }
  72. });
  73. exports.default = _default;