prefer-to-contain.js 3.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
'use strict';

const {
  getDocsUrl,
  expectCase,
  expectResolveCase,
  expectRejectCase,
  method,
  argument,
} = require('./util');

const isEqualityCheck = node =>
  method(node) &&
  (method(node).name === 'toBe' || method(node).name === 'toEqual');

const isArgumentValid = node =>
  argument(node).value === true || argument(node).value === false;

const hasOneArgument = node => node.arguments && node.arguments.length === 1;

const isValidEqualityCheck = node =>
  isEqualityCheck(node) &&
  hasOneArgument(node.parent.parent) &&
  isArgumentValid(node);

const isEqualityNegation = node =>
  method(node).name === 'not' && isValidEqualityCheck(node.parent);

const hasIncludesMethod = node =>
  node.arguments[0] &&
  node.arguments[0].callee &&
  node.arguments[0].callee.property &&
  node.arguments[0].callee.property.name === 'includes';

const isValidIncludesMethod = node =>
  hasIncludesMethod(node) && hasOneArgument(node.arguments[0]);

const getNegationFixes = (node, sourceCode, fixer) => {
  const negationPropertyDot = sourceCode.getFirstTokenBetween(
    node.parent.object,
    node.parent.property,
    token => token.value === '.'
  );
  const toContainFunc =
    isEqualityNegation(node) && argument(node.parent).value
      ? 'not.toContain'
      : 'toContain';

  //.includes function argument
  const [containArg] = node.arguments[0].arguments;
  return [
    fixer.remove(negationPropertyDot),
    fixer.remove(method(node)),
    fixer.replaceText(method(node.parent), toContainFunc),
    fixer.replaceText(argument(node.parent), sourceCode.getText(containArg)),
  ];
};

const getCommonFixes = (node, sourceCode, fixer) => {
  const [containArg] = node.arguments[0].arguments;
  const includesCaller = node.arguments[0].callee;

  const propertyDot = sourceCode.getFirstTokenBetween(
    includesCaller.object,
    includesCaller.property,
    token => token.value === '.'
  );

  const closingParenthesis = sourceCode.getTokenAfter(containArg);
  const openParenthesis = sourceCode.getTokenBefore(containArg);

  return [
    fixer.remove(containArg),
    fixer.remove(includesCaller.property),
    fixer.remove(propertyDot),
    fixer.remove(closingParenthesis),
    fixer.remove(openParenthesis),
  ];
};

module.exports = {
  meta: {
    docs: {
      url: getDocsUrl(__filename),
    },
    fixable: 'code',
  },
  create(context) {
    return {
      CallExpression(node) {
        if (
          !(expectResolveCase(node) || expectRejectCase(node)) &&
          expectCase(node) &&
          (isEqualityNegation(node) || isValidEqualityCheck(node)) &&
          isValidIncludesMethod(node)
        ) {
          context.report({
            fix(fixer) {
              const sourceCode = context.getSourceCode();

              let fixArr = getCommonFixes(node, sourceCode, fixer);
              if (isEqualityNegation(node)) {
                return getNegationFixes(node, sourceCode, fixer).concat(fixArr);
              }

              const toContainFunc = argument(node).value
                ? 'toContain'
                : 'not.toContain';

              //.includes function argument
              const [containArg] = node.arguments[0].arguments;

              fixArr.push(fixer.replaceText(method(node), toContainFunc));
              fixArr.push(
                fixer.replaceText(
                  argument(node),
                  sourceCode.getText(containArg)
                )
              );
              return fixArr;
            },
            message: 'Use toContain() instead',
            node: method(node),
          });
        }
      },
    };
  },
};