@@ -2,7 +2,7 @@ import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
22import { getDocsUrl } from '../utils' ;
33import {
44 isImportDefaultSpecifier ,
5- isCallExpression ,
5+ isLiteral ,
66 isIdentifier ,
77 isObjectPattern ,
88 isProperty ,
@@ -88,40 +88,42 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
8888 } ) ;
8989 }
9090 } ,
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'
102116 ) ;
103117
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+ } ) ;
107123 }
108124
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 ;
125127 }
126128 } ,
127129 'Program:exit' ( ) {
0 commit comments