source: frontend/node_modules/eslint-plugin-jest/lib/rules/prefer-equality-matcher.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: 3.3 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
28var _default = (0, _utils.createRule)({
29 name: __filename,
30 meta: {
31 docs: {
32 category: 'Best Practices',
33 description: 'Suggest using the built-in equality matchers',
34 recommended: false,
35 suggestion: true
36 },
37 messages: {
38 useEqualityMatcher: 'Prefer using one of the equality matchers instead',
39 suggestEqualityMatcher: 'Use `{{ equalityMatcher }}`'
40 },
41 hasSuggestions: true,
42 type: 'suggestion',
43 schema: []
44 },
45 defaultOptions: [],
46
47 create(context) {
48 return {
49 CallExpression(node) {
50 if (!(0, _utils.isExpectCall)(node)) {
51 return;
52 }
53
54 const {
55 expect: {
56 arguments: [comparison],
57 range: [, expectCallEnd]
58 },
59 matcher,
60 modifier
61 } = (0, _utils.parseExpectCall)(node);
62
63 if (!matcher || (comparison === null || comparison === void 0 ? void 0 : comparison.type) !== _experimentalUtils.AST_NODE_TYPES.BinaryExpression || comparison.operator !== '===' && comparison.operator !== '!==' || !isBooleanEqualityMatcher(matcher)) {
64 return;
65 }
66
67 const matcherValue = (0, _utils.followTypeAssertionChain)(matcher.arguments[0]).value; // we need to negate the expectation if the current expected
68 // value is itself negated by the "not" modifier
69
70 const addNotModifier = (comparison.operator === '!==' ? !matcherValue : matcherValue) === !!modifier;
71
72 const buildFixer = equalityMatcher => fixer => {
73 const sourceCode = context.getSourceCode();
74 return [// replace the comparison argument with the left-hand side of the comparison
75 fixer.replaceText(comparison, sourceCode.getText(comparison.left)), // replace the current matcher & modifier with the preferred matcher
76 fixer.replaceTextRange([expectCallEnd, matcher.node.range[1]], addNotModifier ? `.${_utils.ModifierName.not}.${equalityMatcher}` : `.${equalityMatcher}`), // replace the matcher argument with the right-hand side of the comparison
77 fixer.replaceText(matcher.arguments[0], sourceCode.getText(comparison.right))];
78 };
79
80 context.report({
81 messageId: 'useEqualityMatcher',
82 suggest: ['toBe', 'toEqual', 'toStrictEqual'].map(equalityMatcher => ({
83 messageId: 'suggestEqualityMatcher',
84 data: {
85 equalityMatcher
86 },
87 fix: buildFixer(equalityMatcher)
88 })),
89 node: (modifier || matcher).node.property
90 });
91 }
92
93 };
94 }
95
96});
97
98exports.default = _default;
Note: See TracBrowser for help on using the repository browser.