123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _experimentalUtils = require("@typescript-eslint/experimental-utils");
- var _utils = require("./utils");
- const isThenOrCatchCall = node => node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(node.callee.property) && ['then', 'catch'].includes((0, _utils.getAccessorValue)(node.callee.property));
- const isExpectCallPresentInFunction = body => {
- if (body.type === _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
- return body.body.find(line => {
- if (line.type === _experimentalUtils.AST_NODE_TYPES.ExpressionStatement) {
- return isFullExpectCall(line.expression);
- }
- if (line.type === _experimentalUtils.AST_NODE_TYPES.ReturnStatement && line.argument) {
- return isFullExpectCall(line.argument);
- }
- return false;
- });
- }
- return isFullExpectCall(body);
- };
- const isFullExpectCall = expression => expression.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && (0, _utils.isExpectMember)(expression.callee);
- const reportReturnRequired = (context, node) => {
- context.report({
- loc: {
- end: {
- column: node.loc.end.column,
- line: node.loc.end.line
- },
- start: node.loc.start
- },
- messageId: 'returnPromise',
- node
- });
- };
- const isPromiseReturnedLater = (node, testFunctionBody) => {
- let promiseName;
- if (node.type === _experimentalUtils.AST_NODE_TYPES.ExpressionStatement && node.expression.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && node.expression.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(node.expression.callee.object)) {
- promiseName = (0, _utils.getAccessorValue)(node.expression.callee.object);
- } else if (node.type === _experimentalUtils.AST_NODE_TYPES.VariableDeclarator && node.id.type === _experimentalUtils.AST_NODE_TYPES.Identifier) {
- promiseName = node.id.name;
- }
- const lastLineInTestFunc = testFunctionBody[testFunctionBody.length - 1];
- return lastLineInTestFunc.type === _experimentalUtils.AST_NODE_TYPES.ReturnStatement && lastLineInTestFunc.argument && ('name' in lastLineInTestFunc.argument && lastLineInTestFunc.argument.name === promiseName || !promiseName);
- };
- const isTestFunc = node => node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && (0, _utils.isSupportedAccessor)(node.callee) && [_utils.TestCaseName.it, _utils.TestCaseName.test].includes((0, _utils.getAccessorValue)(node.callee));
- const findTestFunction = node => {
- while (node) {
- if ((0, _utils.isFunction)(node) && node.parent && isTestFunc(node.parent)) {
- return node;
- }
- node = node.parent;
- }
- return null;
- };
- const isParentThenOrPromiseReturned = (node, testFunctionBody) => node.type === _experimentalUtils.AST_NODE_TYPES.ReturnStatement || isPromiseReturnedLater(node, testFunctionBody);
- const verifyExpectWithReturn = (promiseCallbacks, node, context, testFunctionBody) => {
- promiseCallbacks.some(promiseCallback => {
- if (promiseCallback && (0, _utils.isFunction)(promiseCallback)) {
- if (isExpectCallPresentInFunction(promiseCallback.body) && node.parent.parent && !isParentThenOrPromiseReturned(node.parent.parent, testFunctionBody)) {
- reportReturnRequired(context, node.parent.parent);
- return true;
- }
- }
- return false;
- });
- };
- const isHavingAsyncCallBackParam = testFunction => testFunction.params[0] && testFunction.params[0].type === _experimentalUtils.AST_NODE_TYPES.Identifier;
- var _default = (0, _utils.createRule)({
- name: __filename,
- meta: {
- docs: {
- category: 'Best Practices',
- description: 'Enforce having return statement when testing with promises',
- recommended: 'error'
- },
- messages: {
- returnPromise: 'Promise should be returned to test its fulfillment or rejection'
- },
- type: 'suggestion',
- schema: []
- },
- defaultOptions: [],
- create(context) {
- return {
- CallExpression(node) {
- if (!isThenOrCatchCall(node) || node.parent && node.parent.type === _experimentalUtils.AST_NODE_TYPES.AwaitExpression) {
- return;
- }
- const testFunction = findTestFunction(node);
- if (testFunction && !isHavingAsyncCallBackParam(testFunction)) {
- const {
- body
- } = testFunction;
- if (body.type !== _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
- return;
- }
- const testFunctionBody = body.body;
- const [fulfillmentCallback, rejectionCallback] = node.arguments; // then block can have two args, fulfillment & rejection
- // then block can have one args, fulfillment
- // catch block can have one args, rejection
- // ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
- verifyExpectWithReturn([fulfillmentCallback, rejectionCallback], node.callee, context, testFunctionBody);
- }
- }
- };
- }
- });
- exports.default = _default;
|