no-conditional-expect.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("@typescript-eslint/utils");
  7. var _utils2 = require("./utils");
  8. const isCatchCall = node => node.callee.type === _utils.AST_NODE_TYPES.MemberExpression && (0, _utils2.isSupportedAccessor)(node.callee.property, 'catch');
  9. var _default = (0, _utils2.createRule)({
  10. name: __filename,
  11. meta: {
  12. docs: {
  13. description: 'Prevent calling `expect` conditionally',
  14. category: 'Best Practices',
  15. recommended: 'error'
  16. },
  17. messages: {
  18. conditionalExpect: 'Avoid calling `expect` conditionally`'
  19. },
  20. type: 'problem',
  21. schema: []
  22. },
  23. defaultOptions: [],
  24. create(context) {
  25. let conditionalDepth = 0;
  26. let inTestCase = false;
  27. let inPromiseCatch = false;
  28. const increaseConditionalDepth = () => inTestCase && conditionalDepth++;
  29. const decreaseConditionalDepth = () => inTestCase && conditionalDepth--;
  30. return {
  31. FunctionDeclaration(node) {
  32. const declaredVariables = context.getDeclaredVariables(node);
  33. const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context);
  34. if (testCallExpressions.length > 0) {
  35. inTestCase = true;
  36. }
  37. },
  38. CallExpression(node) {
  39. var _parseJestFnCall;
  40. const {
  41. type: jestFnCallType
  42. } = (_parseJestFnCall = (0, _utils2.parseJestFnCall)(node, context)) !== null && _parseJestFnCall !== void 0 ? _parseJestFnCall : {};
  43. if (jestFnCallType === 'test') {
  44. inTestCase = true;
  45. }
  46. if (isCatchCall(node)) {
  47. inPromiseCatch = true;
  48. }
  49. if (inTestCase && jestFnCallType === 'expect' && conditionalDepth > 0) {
  50. context.report({
  51. messageId: 'conditionalExpect',
  52. node
  53. });
  54. }
  55. if (inPromiseCatch && jestFnCallType === 'expect') {
  56. context.report({
  57. messageId: 'conditionalExpect',
  58. node
  59. });
  60. }
  61. },
  62. 'CallExpression:exit'(node) {
  63. if ((0, _utils2.isTypeOfJestFnCall)(node, context, ['test'])) {
  64. inTestCase = false;
  65. }
  66. if (isCatchCall(node)) {
  67. inPromiseCatch = false;
  68. }
  69. },
  70. CatchClause: increaseConditionalDepth,
  71. 'CatchClause:exit': decreaseConditionalDepth,
  72. IfStatement: increaseConditionalDepth,
  73. 'IfStatement:exit': decreaseConditionalDepth,
  74. SwitchStatement: increaseConditionalDepth,
  75. 'SwitchStatement:exit': decreaseConditionalDepth,
  76. ConditionalExpression: increaseConditionalDepth,
  77. 'ConditionalExpression:exit': decreaseConditionalDepth,
  78. LogicalExpression: increaseConditionalDepth,
  79. 'LogicalExpression:exit': decreaseConditionalDepth
  80. };
  81. }
  82. });
  83. exports.default = _default;