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