@@ -2,7 +2,7 @@ import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
2
2
import { getDocsUrl } from '../utils' ;
3
3
import {
4
4
isImportDefaultSpecifier ,
5
- isCallExpression ,
5
+ isLiteral ,
6
6
isIdentifier ,
7
7
isObjectPattern ,
8
8
isProperty ,
@@ -88,40 +88,42 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
88
88
} ) ;
89
89
}
90
90
} ,
91
- VariableDeclarator ( node ) {
92
- if (
93
- isCallExpression ( node . init ) &&
94
- isIdentifier ( node . init . callee ) &&
95
- node . init . callee . name === 'require'
96
- ) {
97
- const requiredModule = node . init . arguments [ 0 ] as TSESTree . Literal ;
98
- const requiredModuleValue = requiredModule . value as string ;
99
-
100
- const testingLibraryWithCleanup = requiredModuleValue . match (
101
- CLEANUP_LIBRARY_REGEX
91
+ [ `VariableDeclarator > CallExpression > Identifier[name="require"]` ] (
92
+ node : TSESTree . Identifier
93
+ ) {
94
+ const { arguments : args } = node . parent as TSESTree . CallExpression ;
95
+
96
+ const literalNodeCleanupModuleName = args . find (
97
+ args =>
98
+ isLiteral ( args ) &&
99
+ typeof args . value === 'string' &&
100
+ args . value . match ( CLEANUP_LIBRARY_REGEX )
101
+ ) ;
102
+
103
+ if ( ! literalNodeCleanupModuleName ) {
104
+ return ;
105
+ }
106
+
107
+ const declaratorNode = node . parent
108
+ . parent as TSESTree . VariableDeclarator ;
109
+
110
+ if ( isObjectPattern ( declaratorNode . id ) ) {
111
+ const cleanupProperty = declaratorNode . id . properties . find (
112
+ property =>
113
+ isProperty ( property ) &&
114
+ isIdentifier ( property . key ) &&
115
+ property . key . name === 'cleanup'
102
116
) ;
103
117
104
- // Early return if the library doesn't support `cleanup`
105
- if ( ! testingLibraryWithCleanup ) {
106
- return ;
118
+ if ( cleanupProperty ) {
119
+ context . report ( {
120
+ node : cleanupProperty ,
121
+ messageId : 'noManualCleanup' ,
122
+ } ) ;
107
123
}
108
124
109
- if ( isObjectPattern ( node . id ) ) {
110
- const cleanupProperty = node . id . properties . find (
111
- property =>
112
- isProperty ( property ) &&
113
- isIdentifier ( property . key ) &&
114
- property . key . name === 'cleanup'
115
- ) ;
116
- if ( cleanupProperty ) {
117
- context . report ( {
118
- node : cleanupProperty ,
119
- messageId : 'noManualCleanup' ,
120
- } ) ;
121
- }
122
- } else {
123
- defaultRequireFromTestingLibrary = node . id ;
124
- }
125
+ } else {
126
+ defaultRequireFromTestingLibrary = declaratorNode . id ;
125
127
}
126
128
} ,
127
129
'Program:exit' ( ) {
0 commit comments