no-duplicate-hooks.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("./utils");
  7. const newHookContext = () => ({
  8. beforeAll: 0,
  9. beforeEach: 0,
  10. afterAll: 0,
  11. afterEach: 0
  12. });
  13. var _default = (0, _utils.createRule)({
  14. name: __filename,
  15. meta: {
  16. docs: {
  17. category: 'Best Practices',
  18. description: 'Disallow duplicate setup and teardown hooks',
  19. recommended: false
  20. },
  21. messages: {
  22. noDuplicateHook: 'Duplicate {{hook}} in describe block'
  23. },
  24. schema: [],
  25. type: 'suggestion'
  26. },
  27. defaultOptions: [],
  28. create(context) {
  29. const hookContexts = [newHookContext()];
  30. return {
  31. CallExpression(node) {
  32. if ((0, _utils.isDescribeCall)(node)) {
  33. hookContexts.push(newHookContext());
  34. }
  35. if ((0, _utils.isHook)(node)) {
  36. const currentLayer = hookContexts[hookContexts.length - 1];
  37. currentLayer[node.callee.name] += 1;
  38. if (currentLayer[node.callee.name] > 1) {
  39. context.report({
  40. messageId: 'noDuplicateHook',
  41. data: {
  42. hook: node.callee.name
  43. },
  44. node
  45. });
  46. }
  47. }
  48. },
  49. 'CallExpression:exit'(node) {
  50. if ((0, _utils.isDescribeCall)(node)) {
  51. hookContexts.pop();
  52. }
  53. }
  54. };
  55. }
  56. });
  57. exports.default = _default;