2020-08-26 01:57:08 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.default = void 0;
|
2022-11-10 11:43:16 +01:00
|
|
|
var _utils = require("@typescript-eslint/utils");
|
|
|
|
var _utils2 = require("./utils");
|
|
|
|
const getBlockType = (statement, context) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
const func = statement.parent;
|
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
/* istanbul ignore if */
|
2020-08-26 01:57:08 +02:00
|
|
|
if (!func) {
|
|
|
|
throw new Error(`Unexpected BlockStatement. No parent defined. - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
|
2022-11-10 11:43:16 +01:00
|
|
|
}
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
// functionDeclaration: function func() {}
|
|
|
|
if (func.type === _utils.AST_NODE_TYPES.FunctionDeclaration) {
|
2020-08-26 01:57:08 +02:00
|
|
|
return 'function';
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
if ((0, _utils2.isFunction)(func) && func.parent) {
|
|
|
|
const expr = func.parent;
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
// arrow function or function expr
|
|
|
|
if (expr.type === _utils.AST_NODE_TYPES.VariableDeclarator) {
|
2020-08-26 01:57:08 +02:00
|
|
|
return 'function';
|
2022-11-10 11:43:16 +01:00
|
|
|
}
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
// if it's not a variable, it will be callExpr, we only care about describe
|
|
|
|
if (expr.type === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isTypeOfJestFnCall)(expr, context, ['describe'])) {
|
2020-08-26 01:57:08 +02:00
|
|
|
return 'describe';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2022-11-10 11:43:16 +01:00
|
|
|
var _default = (0, _utils2.createRule)({
|
2020-08-26 01:57:08 +02:00
|
|
|
name: __filename,
|
|
|
|
meta: {
|
|
|
|
docs: {
|
|
|
|
category: 'Best Practices',
|
|
|
|
description: 'Disallow using `expect` outside of `it` or `test` blocks',
|
|
|
|
recommended: 'error'
|
|
|
|
},
|
|
|
|
messages: {
|
|
|
|
unexpectedExpect: 'Expect must be inside of a test block.'
|
|
|
|
},
|
|
|
|
type: 'suggestion',
|
|
|
|
schema: [{
|
|
|
|
properties: {
|
|
|
|
additionalTestBlockFunctions: {
|
|
|
|
type: 'array',
|
|
|
|
items: {
|
|
|
|
type: 'string'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
additionalProperties: false
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
defaultOptions: [{
|
|
|
|
additionalTestBlockFunctions: []
|
|
|
|
}],
|
|
|
|
create(context, [{
|
|
|
|
additionalTestBlockFunctions = []
|
|
|
|
}]) {
|
|
|
|
const callStack = [];
|
2022-11-10 11:43:16 +01:00
|
|
|
const isCustomTestBlockFunction = node => additionalTestBlockFunctions.includes((0, _utils2.getNodeName)(node) || '');
|
2020-08-26 01:57:08 +02:00
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
2022-11-10 11:43:16 +01:00
|
|
|
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
|
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'expect') {
|
|
|
|
var _jestFnCall$head$node;
|
|
|
|
if (((_jestFnCall$head$node = jestFnCall.head.node.parent) === null || _jestFnCall$head$node === void 0 ? void 0 : _jestFnCall$head$node.type) === _utils.AST_NODE_TYPES.MemberExpression && jestFnCall.members.length === 1 && !['assertions', 'hasAssertions'].includes((0, _utils2.getAccessorValue)(jestFnCall.members[0]))) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-26 01:57:08 +02:00
|
|
|
const parent = callStack[callStack.length - 1];
|
2022-11-10 11:43:16 +01:00
|
|
|
if (!parent || parent === _utils2.DescribeAlias.describe) {
|
2020-08-26 01:57:08 +02:00
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: 'unexpectedExpect'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'test' || isCustomTestBlockFunction(node)) {
|
2020-08-26 01:57:08 +02:00
|
|
|
callStack.push('test');
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
if (node.callee.type === _utils.AST_NODE_TYPES.TaggedTemplateExpression) {
|
2020-08-26 01:57:08 +02:00
|
|
|
callStack.push('template');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'CallExpression:exit'(node) {
|
|
|
|
const top = callStack[callStack.length - 1];
|
2022-11-10 11:43:16 +01:00
|
|
|
if (top === 'test' && ((0, _utils2.isTypeOfJestFnCall)(node, context, ['test']) || isCustomTestBlockFunction(node)) && node.callee.type !== _utils.AST_NODE_TYPES.MemberExpression || top === 'template' && node.callee.type === _utils.AST_NODE_TYPES.TaggedTemplateExpression) {
|
2020-08-26 01:57:08 +02:00
|
|
|
callStack.pop();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
BlockStatement(statement) {
|
2022-11-10 11:43:16 +01:00
|
|
|
const blockType = getBlockType(statement, context);
|
2020-08-26 01:57:08 +02:00
|
|
|
if (blockType) {
|
|
|
|
callStack.push(blockType);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'BlockStatement:exit'(statement) {
|
2022-11-10 11:43:16 +01:00
|
|
|
if (callStack[callStack.length - 1] === getBlockType(statement, context)) {
|
2020-08-26 01:57:08 +02:00
|
|
|
callStack.pop();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ArrowFunctionExpression(node) {
|
|
|
|
var _node$parent;
|
2022-11-10 11:43:16 +01:00
|
|
|
if (((_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.type) !== _utils.AST_NODE_TYPES.CallExpression) {
|
2020-08-26 01:57:08 +02:00
|
|
|
callStack.push('arrow');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'ArrowFunctionExpression:exit'() {
|
|
|
|
if (callStack[callStack.length - 1] === 'arrow') {
|
|
|
|
callStack.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
exports.default = _default;
|