no-standalone-expect.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 getBlockType = statement => {
  9. const func = statement.parent;
  10. /* istanbul ignore if */
  11. if (!func) {
  12. throw new Error(`Unexpected BlockStatement. No parent defined. - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
  13. } // functionDeclaration: function func() {}
  14. if (func.type === _experimentalUtils.AST_NODE_TYPES.FunctionDeclaration) {
  15. return 'function';
  16. }
  17. if ((0, _utils.isFunction)(func) && func.parent) {
  18. const expr = func.parent; // arrow function or function expr
  19. if (expr.type === _experimentalUtils.AST_NODE_TYPES.VariableDeclarator) {
  20. return 'function';
  21. } // if it's not a variable, it will be callExpr, we only care about describe
  22. if (expr.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && (0, _utils.isDescribeCall)(expr)) {
  23. return 'describe';
  24. }
  25. }
  26. return null;
  27. };
  28. var _default = (0, _utils.createRule)({
  29. name: __filename,
  30. meta: {
  31. docs: {
  32. category: 'Best Practices',
  33. description: 'Disallow using `expect` outside of `it` or `test` blocks',
  34. recommended: 'error'
  35. },
  36. messages: {
  37. unexpectedExpect: 'Expect must be inside of a test block.'
  38. },
  39. type: 'suggestion',
  40. schema: [{
  41. properties: {
  42. additionalTestBlockFunctions: {
  43. type: 'array',
  44. items: {
  45. type: 'string'
  46. }
  47. }
  48. },
  49. additionalProperties: false
  50. }]
  51. },
  52. defaultOptions: [{
  53. additionalTestBlockFunctions: []
  54. }],
  55. create(context, [{
  56. additionalTestBlockFunctions = []
  57. }]) {
  58. const callStack = [];
  59. const isCustomTestBlockFunction = node => additionalTestBlockFunctions.includes((0, _utils.getNodeName)(node) || '');
  60. const isTestBlock = node => (0, _utils.isTestCaseCall)(node) || isCustomTestBlockFunction(node);
  61. return {
  62. CallExpression(node) {
  63. if ((0, _utils.isExpectCall)(node)) {
  64. const parent = callStack[callStack.length - 1];
  65. if (!parent || parent === _utils.DescribeAlias.describe) {
  66. context.report({
  67. node,
  68. messageId: 'unexpectedExpect'
  69. });
  70. }
  71. return;
  72. }
  73. if (isTestBlock(node)) {
  74. callStack.push('test');
  75. }
  76. if (node.callee.type === _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression) {
  77. callStack.push('template');
  78. }
  79. },
  80. 'CallExpression:exit'(node) {
  81. const top = callStack[callStack.length - 1];
  82. if (top === 'test' && isTestBlock(node) && node.callee.type !== _experimentalUtils.AST_NODE_TYPES.MemberExpression || top === 'template' && node.callee.type === _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression) {
  83. callStack.pop();
  84. }
  85. },
  86. BlockStatement(statement) {
  87. const blockType = getBlockType(statement);
  88. if (blockType) {
  89. callStack.push(blockType);
  90. }
  91. },
  92. 'BlockStatement:exit'(statement) {
  93. if (callStack[callStack.length - 1] === getBlockType(statement)) {
  94. callStack.pop();
  95. }
  96. },
  97. ArrowFunctionExpression(node) {
  98. var _node$parent;
  99. if (((_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.type) !== _experimentalUtils.AST_NODE_TYPES.CallExpression) {
  100. callStack.push('arrow');
  101. }
  102. },
  103. 'ArrowFunctionExpression:exit'() {
  104. if (callStack[callStack.length - 1] === 'arrow') {
  105. callStack.pop();
  106. }
  107. }
  108. };
  109. }
  110. });
  111. exports.default = _default;