| 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 | var _default = (0, _utils.createRule)({
|
|---|
| 13 | name: __filename,
|
|---|
| 14 | meta: {
|
|---|
| 15 | docs: {
|
|---|
| 16 | category: 'Best Practices',
|
|---|
| 17 | description: 'Enforces a maximum depth to nested describe calls',
|
|---|
| 18 | recommended: false
|
|---|
| 19 | },
|
|---|
| 20 | messages: {
|
|---|
| 21 | exceededMaxDepth: 'Too many nested describe calls ({{ depth }}). Maximum allowed is {{ max }}.'
|
|---|
| 22 | },
|
|---|
| 23 | type: 'suggestion',
|
|---|
| 24 | schema: [{
|
|---|
| 25 | type: 'object',
|
|---|
| 26 | properties: {
|
|---|
| 27 | max: {
|
|---|
| 28 | type: 'integer',
|
|---|
| 29 | minimum: 0
|
|---|
| 30 | }
|
|---|
| 31 | },
|
|---|
| 32 | additionalProperties: false
|
|---|
| 33 | }]
|
|---|
| 34 | },
|
|---|
| 35 | defaultOptions: [{
|
|---|
| 36 | max: 5
|
|---|
| 37 | }],
|
|---|
| 38 |
|
|---|
| 39 | create(context, [{
|
|---|
| 40 | max
|
|---|
| 41 | }]) {
|
|---|
| 42 | const describeCallbackStack = [];
|
|---|
| 43 |
|
|---|
| 44 | function pushDescribeCallback(node) {
|
|---|
| 45 | const {
|
|---|
| 46 | parent
|
|---|
| 47 | } = node;
|
|---|
| 48 |
|
|---|
| 49 | if ((parent === null || parent === void 0 ? void 0 : parent.type) !== _experimentalUtils.AST_NODE_TYPES.CallExpression || !(0, _utils.isDescribeCall)(parent)) {
|
|---|
| 50 | return;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | describeCallbackStack.push(0);
|
|---|
| 54 |
|
|---|
| 55 | if (describeCallbackStack.length > max) {
|
|---|
| 56 | context.report({
|
|---|
| 57 | node: parent,
|
|---|
| 58 | messageId: 'exceededMaxDepth',
|
|---|
| 59 | data: {
|
|---|
| 60 | depth: describeCallbackStack.length,
|
|---|
| 61 | max
|
|---|
| 62 | }
|
|---|
| 63 | });
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | function popDescribeCallback(node) {
|
|---|
| 68 | const {
|
|---|
| 69 | parent
|
|---|
| 70 | } = node;
|
|---|
| 71 |
|
|---|
| 72 | if ((parent === null || parent === void 0 ? void 0 : parent.type) === _experimentalUtils.AST_NODE_TYPES.CallExpression && (0, _utils.isDescribeCall)(parent)) {
|
|---|
| 73 | describeCallbackStack.pop();
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | return {
|
|---|
| 78 | FunctionExpression: pushDescribeCallback,
|
|---|
| 79 | 'FunctionExpression:exit': popDescribeCallback,
|
|---|
| 80 | ArrowFunctionExpression: pushDescribeCallback,
|
|---|
| 81 | 'ArrowFunctionExpression:exit': popDescribeCallback
|
|---|
| 82 | };
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | });
|
|---|
| 86 |
|
|---|
| 87 | exports.default = _default; |
|---|