source: frontend/node_modules/eslint-plugin-jest/lib/rules/no-if.js

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

Fix frontend appearance

  • Property mode set to 100644
File size: 2.7 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 testCaseNames = new Set([...Object.keys(_utils.TestCaseName), 'it.only', 'it.concurrent.only', 'it.skip', 'it.concurrent.skip', 'test.only', 'test.concurrent.only', 'test.skip', 'test.concurrent.skip', 'fit.concurrent']);
13
14const isTestFunctionExpression = node => node.parent !== undefined && node.parent.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && testCaseNames.has((0, _utils.getNodeName)(node.parent.callee));
15
16const conditionName = {
17 [_experimentalUtils.AST_NODE_TYPES.ConditionalExpression]: 'conditional',
18 [_experimentalUtils.AST_NODE_TYPES.SwitchStatement]: 'switch',
19 [_experimentalUtils.AST_NODE_TYPES.IfStatement]: 'if'
20};
21
22var _default = (0, _utils.createRule)({
23 name: __filename,
24 meta: {
25 docs: {
26 description: 'Disallow conditional logic',
27 category: 'Best Practices',
28 recommended: false
29 },
30 messages: {
31 conditionalInTest: 'Test should not contain {{ condition }} statements.'
32 },
33 schema: [],
34 type: 'suggestion'
35 },
36 defaultOptions: [],
37
38 create(context) {
39 const stack = [];
40
41 function validate(node) {
42 const lastElementInStack = stack[stack.length - 1];
43
44 if (stack.length === 0 || !lastElementInStack) {
45 return;
46 }
47
48 context.report({
49 data: {
50 condition: conditionName[node.type]
51 },
52 messageId: 'conditionalInTest',
53 node
54 });
55 }
56
57 return {
58 CallExpression(node) {
59 if ((0, _utils.isTestCaseCall)(node)) {
60 stack.push(true);
61
62 if ((0, _utils.getNodeName)(node).endsWith('each')) {
63 stack.push(true);
64 }
65 }
66 },
67
68 FunctionExpression(node) {
69 stack.push(isTestFunctionExpression(node));
70 },
71
72 FunctionDeclaration(node) {
73 const declaredVariables = context.getDeclaredVariables(node);
74 const testCallExpressions = (0, _utils.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
75 stack.push(testCallExpressions.length > 0);
76 },
77
78 ArrowFunctionExpression(node) {
79 stack.push(isTestFunctionExpression(node));
80 },
81
82 IfStatement: validate,
83 SwitchStatement: validate,
84 ConditionalExpression: validate,
85
86 'CallExpression:exit'() {
87 stack.pop();
88 },
89
90 'FunctionExpression:exit'() {
91 stack.pop();
92 },
93
94 'FunctionDeclaration:exit'() {
95 stack.pop();
96 },
97
98 'ArrowFunctionExpression:exit'() {
99 stack.pop();
100 }
101
102 };
103 }
104
105});
106
107exports.default = _default;
Note: See TracBrowser for help on using the repository browser.