| 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 findCallbackArg = (node, isJestEach) => {
|
|---|
| 13 | if (isJestEach) {
|
|---|
| 14 | return node.arguments[1];
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | if ((0, _utils.isHook)(node) && node.arguments.length >= 1) {
|
|---|
| 18 | return node.arguments[0];
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | if ((0, _utils.isTestCaseCall)(node) && node.arguments.length >= 2) {
|
|---|
| 22 | return node.arguments[1];
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | return null;
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | var _default = (0, _utils.createRule)({
|
|---|
| 29 | name: __filename,
|
|---|
| 30 | meta: {
|
|---|
| 31 | docs: {
|
|---|
| 32 | category: 'Best Practices',
|
|---|
| 33 | description: 'Avoid using a callback in asynchronous tests and hooks',
|
|---|
| 34 | recommended: 'error',
|
|---|
| 35 | suggestion: true
|
|---|
| 36 | },
|
|---|
| 37 | messages: {
|
|---|
| 38 | noDoneCallback: 'Return a Promise instead of relying on callback parameter',
|
|---|
| 39 | suggestWrappingInPromise: 'Wrap in `new Promise({{ callback }} => ...`',
|
|---|
| 40 | useAwaitInsteadOfCallback: 'Use await instead of callback in async functions'
|
|---|
| 41 | },
|
|---|
| 42 | schema: [],
|
|---|
| 43 | type: 'suggestion',
|
|---|
| 44 | hasSuggestions: true
|
|---|
| 45 | },
|
|---|
| 46 | defaultOptions: [],
|
|---|
| 47 |
|
|---|
| 48 | create(context) {
|
|---|
| 49 | return {
|
|---|
| 50 | CallExpression(node) {
|
|---|
| 51 | var _getNodeName$endsWith, _getNodeName;
|
|---|
| 52 |
|
|---|
| 53 | // done is the second argument for it.each, not the first
|
|---|
| 54 | const isJestEach = (_getNodeName$endsWith = (_getNodeName = (0, _utils.getNodeName)(node.callee)) === null || _getNodeName === void 0 ? void 0 : _getNodeName.endsWith('.each')) !== null && _getNodeName$endsWith !== void 0 ? _getNodeName$endsWith : false;
|
|---|
| 55 |
|
|---|
| 56 | if (isJestEach && node.callee.type !== _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression) {
|
|---|
| 57 | // isJestEach but not a TaggedTemplateExpression, so this must be
|
|---|
| 58 | // the `jest.each([])()` syntax which this rule doesn't support due
|
|---|
| 59 | // to its complexity (see jest-community/eslint-plugin-jest#710)
|
|---|
| 60 | return;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | const callback = findCallbackArg(node, isJestEach);
|
|---|
| 64 | const callbackArgIndex = Number(isJestEach);
|
|---|
| 65 |
|
|---|
| 66 | if (!callback || !(0, _utils.isFunction)(callback) || callback.params.length !== 1 + callbackArgIndex) {
|
|---|
| 67 | return;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | const argument = callback.params[callbackArgIndex];
|
|---|
| 71 |
|
|---|
| 72 | if (argument.type !== _experimentalUtils.AST_NODE_TYPES.Identifier) {
|
|---|
| 73 | context.report({
|
|---|
| 74 | node: argument,
|
|---|
| 75 | messageId: 'noDoneCallback'
|
|---|
| 76 | });
|
|---|
| 77 | return;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | if (callback.async) {
|
|---|
| 81 | context.report({
|
|---|
| 82 | node: argument,
|
|---|
| 83 | messageId: 'useAwaitInsteadOfCallback'
|
|---|
| 84 | });
|
|---|
| 85 | return;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | context.report({
|
|---|
| 89 | node: argument,
|
|---|
| 90 | messageId: 'noDoneCallback',
|
|---|
| 91 | suggest: [{
|
|---|
| 92 | messageId: 'suggestWrappingInPromise',
|
|---|
| 93 | data: {
|
|---|
| 94 | callback: argument.name
|
|---|
| 95 | },
|
|---|
| 96 |
|
|---|
| 97 | fix(fixer) {
|
|---|
| 98 | const {
|
|---|
| 99 | body
|
|---|
| 100 | } = callback;
|
|---|
| 101 | const sourceCode = context.getSourceCode();
|
|---|
| 102 | const firstBodyToken = sourceCode.getFirstToken(body);
|
|---|
| 103 | const lastBodyToken = sourceCode.getLastToken(body);
|
|---|
| 104 | const tokenBeforeArgument = sourceCode.getTokenBefore(argument);
|
|---|
| 105 | const tokenAfterArgument = sourceCode.getTokenAfter(argument);
|
|---|
| 106 | /* istanbul ignore if */
|
|---|
| 107 |
|
|---|
| 108 | if (!firstBodyToken || !lastBodyToken || !tokenBeforeArgument || !tokenAfterArgument) {
|
|---|
| 109 | throw new Error(`Unexpected null when attempting to fix ${context.getFilename()} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | const argumentInParens = tokenBeforeArgument.value === '(' && tokenAfterArgument.value === ')';
|
|---|
| 113 | let argumentFix = fixer.replaceText(argument, '()');
|
|---|
| 114 |
|
|---|
| 115 | if (argumentInParens) {
|
|---|
| 116 | argumentFix = fixer.remove(argument);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | let newCallback = argument.name;
|
|---|
| 120 |
|
|---|
| 121 | if (argumentInParens) {
|
|---|
| 122 | newCallback = `(${newCallback})`;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | let beforeReplacement = `new Promise(${newCallback} => `;
|
|---|
| 126 | let afterReplacement = ')';
|
|---|
| 127 | let replaceBefore = true;
|
|---|
| 128 |
|
|---|
| 129 | if (body.type === _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
|
|---|
| 130 | const keyword = 'return';
|
|---|
| 131 | beforeReplacement = `${keyword} ${beforeReplacement}{`;
|
|---|
| 132 | afterReplacement += '}';
|
|---|
| 133 | replaceBefore = false;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | return [argumentFix, replaceBefore ? fixer.insertTextBefore(firstBodyToken, beforeReplacement) : fixer.insertTextAfter(firstBodyToken, beforeReplacement), fixer.insertTextAfter(lastBodyToken, afterReplacement)];
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | }]
|
|---|
| 140 | });
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | };
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | });
|
|---|
| 147 |
|
|---|
| 148 | exports.default = _default; |
|---|