no-test-callback.js 2.27 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
'use strict';

const { getDocsUrl, isTestCase } = require('./util');

module.exports = {
  meta: {
    docs: {
      url: getDocsUrl(__filename),
    },
    fixable: 'code',
  },
  create(context) {
    return {
      CallExpression(node) {
        if (!isTestCase(node) || node.arguments.length !== 2) {
          return;
        }

        const [, callback] = node.arguments;

        if (
          !/^(Arrow)?FunctionExpression$/.test(callback.type) ||
          callback.params.length !== 1
        ) {
          return;
        }

        const [argument] = callback.params;
        context.report({
          node: argument,
          message: 'Illegal usage of test callback',
          fix(fixer) {
            const sourceCode = context.getSourceCode();
            const { body } = callback;
            const firstBodyToken = sourceCode.getFirstToken(body);
            const lastBodyToken = sourceCode.getLastToken(body);
            const tokenBeforeArgument = sourceCode.getTokenBefore(argument);
            const tokenAfterArgument = sourceCode.getTokenAfter(argument);
            const argumentInParens =
              tokenBeforeArgument.value === '(' &&
              tokenAfterArgument.value === ')';

            let argumentFix = fixer.replaceText(argument, '()');

            if (argumentInParens) {
              argumentFix = fixer.remove(argument);
            }

            let newCallback = argument.name;

            if (argumentInParens) {
              newCallback = `(${newCallback})`;
            }

            let beforeReplacement = `new Promise(${newCallback} => `;
            let afterReplacement = ')';
            let replaceBefore = true;

            if (body.type === 'BlockStatement') {
              const keyword = callback.async ? 'await' : 'return';

              beforeReplacement = `${keyword} ${beforeReplacement}{`;
              afterReplacement += '}';
              replaceBefore = false;
            }

            return [
              argumentFix,
              replaceBefore
                ? fixer.insertTextBefore(firstBodyToken, beforeReplacement)
                : fixer.insertTextAfter(firstBodyToken, beforeReplacement),
              fixer.insertTextAfter(lastBodyToken, afterReplacement),
            ];
          },
        });
      },
    };
  },
};