-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathno-manual-cleanup.ts
147 lines (129 loc) · 4.21 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
135
136
137
138
139
140
141
142
143
144
145
146
147
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { getDocsUrl } from '../utils';
import {
isImportDefaultSpecifier,
isLiteral,
isIdentifier,
isObjectPattern,
isProperty,
isMemberExpression,
isImportSpecifier,
} from '../node-utils';
export const RULE_NAME = 'no-manual-cleanup';
export type MessageIds = 'noManualCleanup';
type Options = [];
const CLEANUP_LIBRARY_REGEX = /(@testing-library\/(preact|react|svelte|vue))|@marko\/testing-library/;
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow the use of `cleanup`',
category: 'Best Practices',
recommended: false,
},
messages: {
noManualCleanup:
"`cleanup` is performed automatically by your test runner, you don't need manual cleanups.",
},
fixable: null,
schema: [],
},
defaultOptions: [],
create(context) {
let defaultImportFromTestingLibrary: TSESTree.ImportDeclaration;
let defaultRequireFromTestingLibrary:
| TSESTree.Identifier
| TSESTree.ArrayPattern;
// can't find the right type?
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function reportImportReferences(references: any[]) {
if (references && references.length > 0) {
references.forEach((reference) => {
const utilsUsage = reference.identifier.parent;
if (
isMemberExpression(utilsUsage) &&
isIdentifier(utilsUsage.property) &&
utilsUsage.property.name === 'cleanup'
) {
context.report({
node: utilsUsage.property,
messageId: 'noManualCleanup',
});
}
});
}
}
return {
ImportDeclaration(node) {
const value = node.source.value as string;
const testingLibraryWithCleanup = value.match(CLEANUP_LIBRARY_REGEX);
// Early return if the library doesn't support `cleanup`
if (!testingLibraryWithCleanup) {
return;
}
if (isImportDefaultSpecifier(node.specifiers[0])) {
defaultImportFromTestingLibrary = node;
}
const cleanupSpecifier = node.specifiers.find(
(specifier) =>
isImportSpecifier(specifier) &&
specifier.imported &&
specifier.imported.name === 'cleanup'
);
if (cleanupSpecifier) {
context.report({
node: cleanupSpecifier,
messageId: 'noManualCleanup',
});
}
},
[`VariableDeclarator > CallExpression > Identifier[name="require"]`](
node: TSESTree.Identifier
) {
const { arguments: args } = node.parent as TSESTree.CallExpression;
const literalNodeCleanupModuleName = args.find(
(args) =>
isLiteral(args) &&
typeof args.value === 'string' &&
args.value.match(CLEANUP_LIBRARY_REGEX)
);
if (!literalNodeCleanupModuleName) {
return;
}
const declaratorNode = node.parent
.parent as TSESTree.VariableDeclarator;
if (isObjectPattern(declaratorNode.id)) {
const cleanupProperty = declaratorNode.id.properties.find(
(property) =>
isProperty(property) &&
isIdentifier(property.key) &&
property.key.name === 'cleanup'
);
if (cleanupProperty) {
context.report({
node: cleanupProperty,
messageId: 'noManualCleanup',
});
}
} else {
defaultRequireFromTestingLibrary = declaratorNode.id;
}
},
'Program:exit'() {
if (defaultImportFromTestingLibrary) {
const references = context.getDeclaredVariables(
defaultImportFromTestingLibrary
)[0].references;
reportImportReferences(references);
}
if (defaultRequireFromTestingLibrary) {
const references = context
.getDeclaredVariables(defaultRequireFromTestingLibrary.parent)[0]
.references.slice(1);
reportImportReferences(references);
}
},
};
},
});