forked from jest-community/eslint-plugin-jest
/
no-standalone-expect.ts
70 lines (67 loc) · 1.95 KB
/
no-standalone-expect.ts
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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { createRule, isDescribe, isExpectCall, isFunction } from './tsUtils';
const getBlockType = (stmt: TSESTree.BlockStatement) => {
const func = stmt.parent;
// functionDeclaration: function func() {}
if (func && func.type === AST_NODE_TYPES.FunctionDeclaration) {
return 'function';
} else if (func && isFunction(func) && func.parent) {
const expr = func.parent;
// arrowfunction or function expr
if (expr.type === AST_NODE_TYPES.VariableDeclarator) {
return 'function';
// if it's not a variable, it will be callExpr, we only care about describe
} else if (
expr.type === AST_NODE_TYPES.CallExpression &&
isDescribe(expr)
) {
return 'describe';
}
return 'callExpr';
}
return false;
};
export default 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',
schema: [],
},
defaultOptions: [],
create(context) {
const callStack: String[] = [];
return {
CallExpression(node) {
if (isExpectCall(node)) {
const parent = callStack[callStack.length - 1];
if (!parent || parent === 'describe') {
context.report({ node, messageId: 'unexpectedExpect' });
}
}
},
BlockStatement(stmt: TSESTree.BlockStatement) {
const blockType = getBlockType(stmt);
if (blockType) {
callStack.push(blockType);
}
},
'BlockStatement:exit'(stmt: TSESTree.BlockStatement) {
const blockType = getBlockType(stmt);
if (blockType && blockType === callStack[callStack.length - 1]) {
callStack.pop();
}
},
};
},
});