@@ -14,30 +14,33 @@ 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
35
34
- // arrowfunction or function expr
36
+ // arrow function or function expr
35
37
if ( expr . type === AST_NODE_TYPES . VariableDeclarator ) {
36
38
return 'function' ;
37
39
}
40
+
38
41
// if it's not a variable, it will be callExpr, we only care about describe
39
42
if ( expr . type === AST_NODE_TYPES . CallExpression && isDescribe ( expr ) ) {
40
- return DescribeAlias . describe ;
43
+ return ' describe' ;
41
44
}
42
45
}
43
46
@@ -52,12 +55,7 @@ const isEach = (node: TSESTree.CallExpression): boolean =>
52
55
node . callee . callee . object . type === AST_NODE_TYPES . Identifier &&
53
56
TestCaseName . hasOwnProperty ( node . callee . callee . object . name ) ;
54
57
55
- type callStackEntry =
56
- | TestCaseName . test
57
- | 'function'
58
- | DescribeAlias . describe
59
- | 'arrowFunc'
60
- | 'template' ;
58
+ type BlockType = 'test' | 'function' | 'describe' | 'arrow' | 'template' ;
61
59
62
60
export default createRule <
63
61
[ { additionalTestBlockFunctions : string [ ] } ] ,
@@ -88,13 +86,16 @@ export default createRule<
88
86
} ,
89
87
defaultOptions : [ { additionalTestBlockFunctions : [ ] } ] ,
90
88
create ( context , [ { additionalTestBlockFunctions = [ ] } ] ) {
91
- const callStack : callStackEntry [ ] = [ ] ;
89
+ const callStack : BlockType [ ] = [ ] ;
92
90
93
91
const isCustomTestBlockFunction = (
94
92
node : TSESTree . CallExpression ,
95
93
) : boolean =>
96
94
additionalTestBlockFunctions . includes ( getNodeName ( node ) || '' ) ;
97
95
96
+ const isTestBlock = ( node : TSESTree . CallExpression ) : boolean =>
97
+ isTestCase ( node ) || isCustomTestBlockFunction ( node ) ;
98
+
98
99
return {
99
100
CallExpression ( node ) {
100
101
if ( isExpectCall ( node ) ) {
@@ -106,9 +107,11 @@ export default createRule<
106
107
107
108
return ;
108
109
}
109
- if ( isTestCase ( node ) || isCustomTestBlockFunction ( node ) ) {
110
- callStack . push ( TestCaseName . test ) ;
110
+
111
+ if ( isTestBlock ( node ) ) {
112
+ callStack . push ( 'test' ) ;
111
113
}
114
+
112
115
if ( node . callee . type === AST_NODE_TYPES . TaggedTemplateExpression ) {
113
116
callStack . push ( 'template' ) ;
114
117
}
@@ -117,37 +120,37 @@ export default createRule<
117
120
const top = callStack [ callStack . length - 1 ] ;
118
121
119
122
if (
120
- ( ( ( ( isTestCase ( node ) || isCustomTestBlockFunction ( node ) ) &&
121
- node . callee . type !== AST_NODE_TYPES . MemberExpression ) ||
122
- isEach ( node ) ) &&
123
- top === TestCaseName . test ) ||
124
- ( node . callee . type === AST_NODE_TYPES . TaggedTemplateExpression &&
125
- 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 )
126
129
) {
127
130
callStack . pop ( ) ;
128
131
}
129
132
} ,
130
- BlockStatement ( stmt ) {
131
- const blockType = getBlockType ( stmt ) ;
133
+
134
+ BlockStatement ( statement ) {
135
+ const blockType = getBlockType ( statement ) ;
132
136
133
137
if ( blockType ) {
134
138
callStack . push ( blockType ) ;
135
139
}
136
140
} ,
137
- 'BlockStatement:exit' ( stmt : TSESTree . BlockStatement ) {
138
- const blockType = getBlockType ( stmt ) ;
139
-
140
- if ( blockType && blockType === callStack [ callStack . length - 1 ] ) {
141
+ 'BlockStatement:exit' ( statement : TSESTree . BlockStatement ) {
142
+ if ( callStack [ callStack . length - 1 ] === getBlockType ( statement ) ) {
141
143
callStack . pop ( ) ;
142
144
}
143
145
} ,
146
+
144
147
ArrowFunctionExpression ( node ) {
145
- if ( node . parent && node . parent . type !== AST_NODE_TYPES . CallExpression ) {
146
- callStack . push ( 'arrowFunc ' ) ;
148
+ if ( node . parent ? .type !== AST_NODE_TYPES . CallExpression ) {
149
+ callStack . push ( 'arrow ' ) ;
147
150
}
148
151
} ,
149
152
'ArrowFunctionExpression:exit' ( ) {
150
- if ( callStack [ callStack . length - 1 ] === 'arrowFunc ' ) {
153
+ if ( callStack [ callStack . length - 1 ] === 'arrow ' ) {
151
154
callStack . pop ( ) ;
152
155
}
153
156
} ,
0 commit comments