prefer-hooks-on-top.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("./utils");
  7. var _default = (0, _utils.createRule)({
  8. name: __filename,
  9. meta: {
  10. docs: {
  11. category: 'Best Practices',
  12. description: 'Suggest having hooks before any test cases',
  13. recommended: false
  14. },
  15. messages: {
  16. noHookOnTop: 'Hooks should come before test cases'
  17. },
  18. schema: [],
  19. type: 'suggestion'
  20. },
  21. defaultOptions: [],
  22. create(context) {
  23. const hooksContext = [false];
  24. return {
  25. CallExpression(node) {
  26. if (!(0, _utils.isHook)(node) && (0, _utils.isTestCaseCall)(node)) {
  27. hooksContext[hooksContext.length - 1] = true;
  28. }
  29. if (hooksContext[hooksContext.length - 1] && (0, _utils.isHook)(node)) {
  30. context.report({
  31. messageId: 'noHookOnTop',
  32. node
  33. });
  34. }
  35. hooksContext.push(false);
  36. },
  37. 'CallExpression:exit'() {
  38. hooksContext.pop();
  39. }
  40. };
  41. }
  42. });
  43. exports.default = _default;