source: frontend/node_modules/eslint-plugin-jest/lib/rules/require-hook.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.9 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _experimentalUtils = require("@typescript-eslint/experimental-utils");
9
10var _utils = require("./utils");
11
12const isJestFnCall = node => {
13 var _getNodeName;
14
15 if ((0, _utils.isDescribeCall)(node) || (0, _utils.isTestCaseCall)(node) || (0, _utils.isHook)(node)) {
16 return true;
17 }
18
19 return !!((_getNodeName = (0, _utils.getNodeName)(node)) !== null && _getNodeName !== void 0 && _getNodeName.startsWith('jest.'));
20};
21
22const isNullOrUndefined = node => {
23 return node.type === _experimentalUtils.AST_NODE_TYPES.Literal && node.value === null || (0, _utils.isIdentifier)(node, 'undefined');
24};
25
26const shouldBeInHook = (node, allowedFunctionCalls = []) => {
27 switch (node.type) {
28 case _experimentalUtils.AST_NODE_TYPES.ExpressionStatement:
29 return shouldBeInHook(node.expression, allowedFunctionCalls);
30
31 case _experimentalUtils.AST_NODE_TYPES.CallExpression:
32 return !(isJestFnCall(node) || allowedFunctionCalls.includes((0, _utils.getNodeName)(node)));
33
34 case _experimentalUtils.AST_NODE_TYPES.VariableDeclaration:
35 {
36 if (node.kind === 'const') {
37 return false;
38 }
39
40 return node.declarations.some(({
41 init
42 }) => init !== null && !isNullOrUndefined(init));
43 }
44
45 default:
46 return false;
47 }
48};
49
50var _default = (0, _utils.createRule)({
51 name: __filename,
52 meta: {
53 docs: {
54 category: 'Best Practices',
55 description: 'Require setup and teardown code to be within a hook',
56 recommended: false
57 },
58 messages: {
59 useHook: 'This should be done within a hook'
60 },
61 type: 'suggestion',
62 schema: [{
63 type: 'object',
64 properties: {
65 allowedFunctionCalls: {
66 type: 'array',
67 items: {
68 type: 'string'
69 }
70 }
71 },
72 additionalProperties: false
73 }]
74 },
75 defaultOptions: [{
76 allowedFunctionCalls: []
77 }],
78
79 create(context) {
80 var _context$options$;
81
82 const {
83 allowedFunctionCalls
84 } = (_context$options$ = context.options[0]) !== null && _context$options$ !== void 0 ? _context$options$ : {};
85
86 const checkBlockBody = body => {
87 for (const statement of body) {
88 if (shouldBeInHook(statement, allowedFunctionCalls)) {
89 context.report({
90 node: statement,
91 messageId: 'useHook'
92 });
93 }
94 }
95 };
96
97 return {
98 Program(program) {
99 checkBlockBody(program.body);
100 },
101
102 CallExpression(node) {
103 if (!(0, _utils.isDescribeCall)(node) || node.arguments.length < 2) {
104 return;
105 }
106
107 const [, testFn] = node.arguments;
108
109 if (!(0, _utils.isFunction)(testFn) || testFn.body.type !== _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
110 return;
111 }
112
113 checkBlockBody(testFn.body.body);
114 }
115
116 };
117 }
118
119});
120
121exports.default = _default;
Note: See TracBrowser for help on using the repository browser.