-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathno-manual-cleanup.ts
134 lines (118 loc) · 3.56 KB
/
no-manual-cleanup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { ASTUtils, TSESLint, TSESTree } from '@typescript-eslint/utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
getImportModuleName,
getVariableReferences,
ImportModuleNode,
isImportDeclaration,
isImportDefaultSpecifier,
isImportSpecifier,
isMemberExpression,
isObjectPattern,
isProperty,
} from '../node-utils';
import { getDeclaredVariables } from '../utils';
export const RULE_NAME = 'no-manual-cleanup';
export type MessageIds = 'noManualCleanup';
type Options = [];
const CLEANUP_LIBRARY_REGEXP =
/(@testing-library\/(preact|react|svelte|vue))|@marko\/testing-library/;
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow the use of `cleanup`',
recommendedConfig: {
dom: false,
angular: false,
react: 'error',
vue: 'error',
marko: false,
},
},
messages: {
noManualCleanup:
"`cleanup` is performed automatically by your test runner, you don't need manual cleanups.",
},
schema: [],
},
defaultOptions: [],
create(context, _, helpers) {
function reportImportReferences(references: TSESLint.Scope.Reference[]) {
for (const reference of references) {
const utilsUsage = reference.identifier.parent;
if (
utilsUsage &&
isMemberExpression(utilsUsage) &&
ASTUtils.isIdentifier(utilsUsage.property) &&
utilsUsage.property.name === 'cleanup'
) {
context.report({
node: utilsUsage.property,
messageId: 'noManualCleanup',
});
}
}
}
function reportCandidateModule(moduleNode: ImportModuleNode) {
if (isImportDeclaration(moduleNode)) {
// case: import utils from 'testing-library-module'
if (isImportDefaultSpecifier(moduleNode.specifiers[0])) {
const { references } = getDeclaredVariables(context, moduleNode)[0];
reportImportReferences(references);
}
// case: import { cleanup } from 'testing-library-module'
const cleanupSpecifier = moduleNode.specifiers.find(
(specifier) =>
isImportSpecifier(specifier) &&
ASTUtils.isIdentifier(specifier.imported) &&
specifier.imported.name === 'cleanup'
);
if (cleanupSpecifier) {
context.report({
node: cleanupSpecifier,
messageId: 'noManualCleanup',
});
}
} else {
const declaratorNode = moduleNode.parent as TSESTree.VariableDeclarator;
if (isObjectPattern(declaratorNode.id)) {
// case: const { cleanup } = require('testing-library-module')
const cleanupProperty = declaratorNode.id.properties.find(
(property) =>
isProperty(property) &&
ASTUtils.isIdentifier(property.key) &&
property.key.name === 'cleanup'
);
if (cleanupProperty) {
context.report({
node: cleanupProperty,
messageId: 'noManualCleanup',
});
}
} else {
// case: const utils = require('testing-library-module')
const references = getVariableReferences(context, declaratorNode);
reportImportReferences(references);
}
}
}
return {
'Program:exit'() {
const customModuleImportNode = helpers.getCustomModuleImportNode();
for (const testingLibraryImportNode of helpers.getAllTestingLibraryImportNodes()) {
const testingLibraryImportName = getImportModuleName(
testingLibraryImportNode
);
if (testingLibraryImportName?.match(CLEANUP_LIBRARY_REGEXP)) {
reportCandidateModule(testingLibraryImportNode);
}
}
if (customModuleImportNode) {
reportCandidateModule(customModuleImportNode);
}
},
};
},
});