source: frontend/node_modules/eslint-plugin-jest/lib/rules/prefer-comparison-matcher.js

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

Fix frontend appearance

  • Property mode set to 100644
File size: 3.6 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 isBooleanLiteral = node => node.type === _experimentalUtils.AST_NODE_TYPES.Literal && typeof node.value === 'boolean';
13
14/**
15 * Checks if the given `ParsedExpectMatcher` is a call to one of the equality matchers,
16 * with a boolean literal as the sole argument.
17 *
18 * @example javascript
19 * toBe(true);
20 * toEqual(false);
21 *
22 * @param {ParsedExpectMatcher} matcher
23 *
24 * @return {matcher is ParsedBooleanEqualityMatcher}
25 */
26const isBooleanEqualityMatcher = matcher => (0, _utils.isParsedEqualityMatcherCall)(matcher) && isBooleanLiteral((0, _utils.followTypeAssertionChain)(matcher.arguments[0]));
27
28const isString = node => {
29 return (0, _utils.isStringNode)(node) || node.type === _experimentalUtils.AST_NODE_TYPES.TemplateLiteral;
30};
31
32const isComparingToString = expression => {
33 return isString(expression.left) || isString(expression.right);
34};
35
36const invertOperator = operator => {
37 switch (operator) {
38 case '>':
39 return '<=';
40
41 case '<':
42 return '>=';
43
44 case '>=':
45 return '<';
46
47 case '<=':
48 return '>';
49 }
50
51 return null;
52};
53
54const determineMatcher = (operator, negated) => {
55 const op = negated ? invertOperator(operator) : operator;
56
57 switch (op) {
58 case '>':
59 return 'toBeGreaterThan';
60
61 case '<':
62 return 'toBeLessThan';
63
64 case '>=':
65 return 'toBeGreaterThanOrEqual';
66
67 case '<=':
68 return 'toBeLessThanOrEqual';
69 }
70
71 return null;
72};
73
74var _default = (0, _utils.createRule)({
75 name: __filename,
76 meta: {
77 docs: {
78 category: 'Best Practices',
79 description: 'Suggest using the built-in comparison matchers',
80 recommended: false
81 },
82 messages: {
83 useToBeComparison: 'Prefer using `{{ preferredMatcher }}` instead'
84 },
85 fixable: 'code',
86 type: 'suggestion',
87 schema: []
88 },
89 defaultOptions: [],
90
91 create(context) {
92 return {
93 CallExpression(node) {
94 if (!(0, _utils.isExpectCall)(node)) {
95 return;
96 }
97
98 const {
99 expect: {
100 arguments: [comparison],
101 range: [, expectCallEnd]
102 },
103 matcher,
104 modifier
105 } = (0, _utils.parseExpectCall)(node);
106
107 if (!matcher || (comparison === null || comparison === void 0 ? void 0 : comparison.type) !== _experimentalUtils.AST_NODE_TYPES.BinaryExpression || isComparingToString(comparison) || !isBooleanEqualityMatcher(matcher)) {
108 return;
109 }
110
111 const preferredMatcher = determineMatcher(comparison.operator, (0, _utils.followTypeAssertionChain)(matcher.arguments[0]).value === !!modifier);
112
113 if (!preferredMatcher) {
114 return;
115 }
116
117 context.report({
118 fix(fixer) {
119 const sourceCode = context.getSourceCode();
120 return [// replace the comparison argument with the left-hand side of the comparison
121 fixer.replaceText(comparison, sourceCode.getText(comparison.left)), // replace the current matcher & modifier with the preferred matcher
122 fixer.replaceTextRange([expectCallEnd, matcher.node.range[1]], `.${preferredMatcher}`), // replace the matcher argument with the right-hand side of the comparison
123 fixer.replaceText(matcher.arguments[0], sourceCode.getText(comparison.right))];
124 },
125
126 messageId: 'useToBeComparison',
127 data: {
128 preferredMatcher
129 },
130 node: (modifier || matcher).node.property
131 });
132 }
133
134 };
135 }
136
137});
138
139exports.default = _default;
Note: See TracBrowser for help on using the repository browser.