@@ -14,31 +14,36 @@ import {
14
14
} from './utils' ;
15
15
16
16
const getBlockType = (
17
- stmt : TSESTree . BlockStatement ,
18
- ) : 'function' | DescribeAlias . describe | null => {
19
- const func = stmt . parent ;
17
+ statement : TSESTree . BlockStatement ,
18
+ ) : 'function' | ' describe' | null => {
19
+ const func = statement . parent ;
20
20
21
21
/* istanbul ignore if */
22
22
if ( ! func ) {
23
23
throw new Error (
24
24
`Unexpected BlockStatement. No parent defined. - please file a github issue at https://github.com/jest-community/eslint-plugin-jest` ,
25
25
) ;
26
26
}
27
+
27
28
// functionDeclaration: function func() {}
28
29
if ( func . type === AST_NODE_TYPES . FunctionDeclaration ) {
29
30
return 'function' ;
30
31
}
32
+
31
33
if ( isFunction ( func ) && func . parent ) {
32
34
const expr = func . parent ;
33
- // arrowfunction or function expr
35
+
36
+ // arrow function or function expr
34
37
if ( expr . type === AST_NODE_TYPES . VariableDeclarator ) {
35
38
return 'function' ;
36
39
}
40
+
37
41
// if it's not a variable, it will be callExpr, we only care about describe
38
42
if ( expr . type === AST_NODE_TYPES . CallExpression && isDescribe ( expr ) ) {
39
- return DescribeAlias . describe ;
43
+ return ' describe' ;
40
44
}
41
45
}
46
+
42
47
return null ;
43
48
} ;
44
49
@@ -50,12 +55,7 @@ const isEach = (node: TSESTree.CallExpression): boolean =>
50
55
node . callee . callee . object . type === AST_NODE_TYPES . Identifier &&
51
56
TestCaseName . hasOwnProperty ( node . callee . callee . object . name ) ;
52
57
53
- type callStackEntry =
54
- | TestCaseName . test
55
- | 'function'
56
- | DescribeAlias . describe
57
- | 'arrowFunc'
58
- | 'template' ;
58
+ type BlockType = 'test' | 'function' | 'describe' | 'arrow' | 'template' ;
59
59
60
60
export default createRule <
61
61
[ { additionalTestBlockFunctions : string [ ] } ] ,
@@ -86,25 +86,32 @@ export default createRule<
86
86
} ,
87
87
defaultOptions : [ { additionalTestBlockFunctions : [ ] } ] ,
88
88
create ( context , [ { additionalTestBlockFunctions = [ ] } ] ) {
89
- const callStack : callStackEntry [ ] = [ ] ;
89
+ const callStack : BlockType [ ] = [ ] ;
90
90
91
91
const isCustomTestBlockFunction = (
92
92
node : TSESTree . CallExpression ,
93
93
) : boolean =>
94
94
additionalTestBlockFunctions . includes ( getNodeName ( node ) || '' ) ;
95
95
96
+ const isTestBlock = ( node : TSESTree . CallExpression ) : boolean =>
97
+ isTestCase ( node ) || isCustomTestBlockFunction ( node ) ;
98
+
96
99
return {
97
100
CallExpression ( node ) {
98
101
if ( isExpectCall ( node ) ) {
99
102
const parent = callStack [ callStack . length - 1 ] ;
103
+
100
104
if ( ! parent || parent === DescribeAlias . describe ) {
101
105
context . report ( { node, messageId : 'unexpectedExpect' } ) ;
102
106
}
107
+
103
108
return ;
104
109
}
105
- if ( isTestCase ( node ) || isCustomTestBlockFunction ( node ) ) {
106
- callStack . push ( TestCaseName . test ) ;
110
+
111
+ if ( isTestBlock ( node ) ) {
112
+ callStack . push ( 'test' ) ;
107
113
}
114
+
108
115
if ( node . callee . type === AST_NODE_TYPES . TaggedTemplateExpression ) {
109
116
callStack . push ( 'template' ) ;
110
117
}
@@ -113,35 +120,37 @@ export default createRule<
113
120
const top = callStack [ callStack . length - 1 ] ;
114
121
115
122
if (
116
- ( ( ( ( isTestCase ( node ) || isCustomTestBlockFunction ( node ) ) &&
117
- node . callee . type !== AST_NODE_TYPES . MemberExpression ) ||
118
- isEach ( node ) ) &&
119
- top === TestCaseName . test ) ||
120
- ( node . callee . type === AST_NODE_TYPES . TaggedTemplateExpression &&
121
- top === 'template' )
123
+ ( top === 'test' &&
124
+ ( isEach ( node ) ||
125
+ ( isTestBlock ( node ) &&
126
+ node . callee . type !== AST_NODE_TYPES . MemberExpression ) ) ) ||
127
+ ( top === 'template' &&
128
+ node . callee . type === AST_NODE_TYPES . TaggedTemplateExpression )
122
129
) {
123
130
callStack . pop ( ) ;
124
131
}
125
132
} ,
126
- BlockStatement ( stmt ) {
127
- const blockType = getBlockType ( stmt ) ;
133
+
134
+ BlockStatement ( statement ) {
135
+ const blockType = getBlockType ( statement ) ;
136
+
128
137
if ( blockType ) {
129
138
callStack . push ( blockType ) ;
130
139
}
131
140
} ,
132
- 'BlockStatement:exit' ( stmt : TSESTree . BlockStatement ) {
133
- const blockType = getBlockType ( stmt ) ;
134
- if ( blockType && blockType === callStack [ callStack . length - 1 ] ) {
141
+ 'BlockStatement:exit' ( statement : TSESTree . BlockStatement ) {
142
+ if ( callStack [ callStack . length - 1 ] === getBlockType ( statement ) ) {
135
143
callStack . pop ( ) ;
136
144
}
137
145
} ,
146
+
138
147
ArrowFunctionExpression ( node ) {
139
- if ( node . parent && node . parent . type !== AST_NODE_TYPES . CallExpression ) {
140
- callStack . push ( 'arrowFunc ' ) ;
148
+ if ( node . parent ? .type !== AST_NODE_TYPES . CallExpression ) {
149
+ callStack . push ( 'arrow ' ) ;
141
150
}
142
151
} ,
143
152
'ArrowFunctionExpression:exit' ( ) {
144
- if ( callStack [ callStack . length - 1 ] === 'arrowFunc ' ) {
153
+ if ( callStack [ callStack . length - 1 ] === 'arrow ' ) {
145
154
callStack . pop ( ) ;
146
155
}
147
156
} ,
0 commit comments