Skip to content

Commit 28e23a9

Browse files
fix(no-manual-cleanup): check require imports properly (#128)
Closes #125
1 parent a789ae9 commit 28e23a9

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

lib/rules/no-manual-cleanup.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
22
import { getDocsUrl } from '../utils';
33
import {
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'() {

tests/lib/rules/no-manual-cleanup.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ ruleTester.run(RULE_NAME, rule, {
4444
// For test coverage
4545
code: `const utils = render("something")`,
4646
},
47+
{
48+
code: `const utils = require(moduleName)`,
49+
},
4750
],
4851
invalid: [
4952
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({

0 commit comments

Comments
 (0)