| 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 | const 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 | */
|
|---|
| 26 | const isBooleanEqualityMatcher = matcher => (0, _utils.isParsedEqualityMatcherCall)(matcher) && isBooleanLiteral((0, _utils.followTypeAssertionChain)(matcher.arguments[0]));
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Checks if the given `node` is a `CallExpression` representing the calling
|
|---|
| 30 | * of an `includes`-like method that can be 'fixed' (using `toContain`).
|
|---|
| 31 | *
|
|---|
| 32 | * @param {CallExpression} node
|
|---|
| 33 | *
|
|---|
| 34 | * @return {node is FixableIncludesCallExpression}
|
|---|
| 35 | */
|
|---|
| 36 | const isFixableIncludesCallExpression = node => node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(node.callee.property, 'includes') && (0, _utils.hasOnlyOneArgument)(node); // expect(array.includes(<value>)[not.]{toBe,toEqual}(<boolean>)
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | var _default = (0, _utils.createRule)({
|
|---|
| 40 | name: __filename,
|
|---|
| 41 | meta: {
|
|---|
| 42 | docs: {
|
|---|
| 43 | category: 'Best Practices',
|
|---|
| 44 | description: 'Suggest using `toContain()`',
|
|---|
| 45 | recommended: false
|
|---|
| 46 | },
|
|---|
| 47 | messages: {
|
|---|
| 48 | useToContain: 'Use toContain() instead'
|
|---|
| 49 | },
|
|---|
| 50 | fixable: 'code',
|
|---|
| 51 | type: 'suggestion',
|
|---|
| 52 | schema: []
|
|---|
| 53 | },
|
|---|
| 54 | defaultOptions: [],
|
|---|
| 55 |
|
|---|
| 56 | create(context) {
|
|---|
| 57 | return {
|
|---|
| 58 | CallExpression(node) {
|
|---|
| 59 | if (!(0, _utils.isExpectCall)(node)) {
|
|---|
| 60 | return;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | const {
|
|---|
| 64 | expect: {
|
|---|
| 65 | arguments: [includesCall],
|
|---|
| 66 | range: [, expectCallEnd]
|
|---|
| 67 | },
|
|---|
| 68 | matcher,
|
|---|
| 69 | modifier
|
|---|
| 70 | } = (0, _utils.parseExpectCall)(node);
|
|---|
| 71 |
|
|---|
| 72 | if (!matcher || !includesCall || modifier && modifier.name !== _utils.ModifierName.not || !isBooleanEqualityMatcher(matcher) || !isFixableIncludesCallExpression(includesCall)) {
|
|---|
| 73 | return;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | context.report({
|
|---|
| 77 | fix(fixer) {
|
|---|
| 78 | const sourceCode = context.getSourceCode(); // we need to negate the expectation if the current expected
|
|---|
| 79 | // value is itself negated by the "not" modifier
|
|---|
| 80 |
|
|---|
| 81 | const addNotModifier = (0, _utils.followTypeAssertionChain)(matcher.arguments[0]).value === !!modifier;
|
|---|
| 82 | return [// remove the "includes" call entirely
|
|---|
| 83 | fixer.removeRange([includesCall.callee.property.range[0] - 1, includesCall.range[1]]), // replace the current matcher with "toContain", adding "not" if needed
|
|---|
| 84 | fixer.replaceTextRange([expectCallEnd, matcher.node.range[1]], addNotModifier ? `.${_utils.ModifierName.not}.toContain` : '.toContain'), // replace the matcher argument with the value from the "includes"
|
|---|
| 85 | fixer.replaceText(matcher.arguments[0], sourceCode.getText(includesCall.arguments[0]))];
|
|---|
| 86 | },
|
|---|
| 87 |
|
|---|
| 88 | messageId: 'useToContain',
|
|---|
| 89 | node: (modifier || matcher).node.property
|
|---|
| 90 | });
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | };
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | });
|
|---|
| 97 |
|
|---|
| 98 | exports.default = _default; |
|---|