github-pages-deploy-action/node_modules/eslint-plugin-jest/lib/rules/no-standalone-expect.js

145 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-03-07 11:45:40 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _experimentalUtils = require("@typescript-eslint/experimental-utils");
var _utils = require("./utils");
2020-06-27 02:01:06 +08:00
const getBlockType = statement => {
const func = statement.parent;
2020-03-07 11:45:40 +08:00
/* istanbul ignore if */
if (!func) {
throw new Error(`Unexpected BlockStatement. No parent defined. - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
} // functionDeclaration: function func() {}
if (func.type === _experimentalUtils.AST_NODE_TYPES.FunctionDeclaration) {
return 'function';
}
if ((0, _utils.isFunction)(func) && func.parent) {
2020-06-27 02:01:06 +08:00
const expr = func.parent; // arrow function or function expr
2020-03-07 11:45:40 +08:00
if (expr.type === _experimentalUtils.AST_NODE_TYPES.VariableDeclarator) {
return 'function';
} // if it's not a variable, it will be callExpr, we only care about describe
if (expr.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && (0, _utils.isDescribe)(expr)) {
2020-06-27 02:01:06 +08:00
return 'describe';
2020-03-07 11:45:40 +08:00
}
}
return null;
};
2020-03-31 20:40:00 +08:00
const isEach = node => node.callee.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && node.callee.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && node.callee.callee.property.type === _experimentalUtils.AST_NODE_TYPES.Identifier && node.callee.callee.property.name === 'each' && node.callee.callee.object.type === _experimentalUtils.AST_NODE_TYPES.Identifier && _utils.TestCaseName.hasOwnProperty(node.callee.callee.object.name);
2020-03-07 11:45:40 +08:00
var _default = (0, _utils.createRule)({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Prevents expects that are outside of an it or test block.',
recommended: false
},
messages: {
unexpectedExpect: 'Expect must be inside of a test block.'
},
type: 'suggestion',
2020-06-27 02:01:06 +08:00
schema: [{
properties: {
additionalTestBlockFunctions: {
type: 'array',
items: {
type: 'string'
}
}
},
additionalProperties: false
}]
2020-03-07 11:45:40 +08:00
},
2020-06-27 02:01:06 +08:00
defaultOptions: [{
additionalTestBlockFunctions: []
}],
2020-03-07 11:45:40 +08:00
2020-06-27 02:01:06 +08:00
create(context, [{
additionalTestBlockFunctions = []
}]) {
2020-03-07 11:45:40 +08:00
const callStack = [];
2020-06-27 02:01:06 +08:00
const isCustomTestBlockFunction = node => additionalTestBlockFunctions.includes((0, _utils.getNodeName)(node) || '');
const isTestBlock = node => (0, _utils.isTestCase)(node) || isCustomTestBlockFunction(node);
2020-03-07 11:45:40 +08:00
return {
CallExpression(node) {
if ((0, _utils.isExpectCall)(node)) {
const parent = callStack[callStack.length - 1];
if (!parent || parent === _utils.DescribeAlias.describe) {
context.report({
node,
messageId: 'unexpectedExpect'
});
}
return;
}
2020-06-27 02:01:06 +08:00
if (isTestBlock(node)) {
callStack.push('test');
2020-03-07 11:45:40 +08:00
}
if (node.callee.type === _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression) {
callStack.push('template');
}
},
'CallExpression:exit'(node) {
const top = callStack[callStack.length - 1];
2020-06-27 02:01:06 +08:00
if (top === 'test' && (isEach(node) || isTestBlock(node) && node.callee.type !== _experimentalUtils.AST_NODE_TYPES.MemberExpression) || top === 'template' && node.callee.type === _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression) {
2020-03-07 11:45:40 +08:00
callStack.pop();
}
},
2020-06-27 02:01:06 +08:00
BlockStatement(statement) {
const blockType = getBlockType(statement);
2020-03-07 11:45:40 +08:00
if (blockType) {
callStack.push(blockType);
}
},
2020-06-27 02:01:06 +08:00
'BlockStatement:exit'(statement) {
if (callStack[callStack.length - 1] === getBlockType(statement)) {
2020-03-07 11:45:40 +08:00
callStack.pop();
}
},
ArrowFunctionExpression(node) {
2020-06-27 02:01:06 +08:00
var _node$parent;
if (((_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.type) !== _experimentalUtils.AST_NODE_TYPES.CallExpression) {
callStack.push('arrow');
2020-03-07 11:45:40 +08:00
}
},
'ArrowFunctionExpression:exit'() {
2020-06-27 02:01:06 +08:00
if (callStack[callStack.length - 1] === 'arrow') {
2020-03-07 11:45:40 +08:00
callStack.pop();
}
}
};
}
});
exports.default = _default;