no-manual-cleanup.js
4.77 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RULE_NAME = void 0;
var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
var utils_1 = require("../utils");
var node_utils_1 = require("../node-utils");
exports.RULE_NAME = 'no-manual-cleanup';
var CLEANUP_LIBRARY_REGEX = /(@testing-library\/(preact|react|svelte|vue))|@marko\/testing-library/;
exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
name: exports.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: function (context) {
var _a;
var defaultImportFromTestingLibrary;
var defaultRequireFromTestingLibrary;
function reportImportReferences(references) {
if (references && references.length > 0) {
references.forEach(function (reference) {
var utilsUsage = reference.identifier.parent;
if (node_utils_1.isMemberExpression(utilsUsage) &&
node_utils_1.isIdentifier(utilsUsage.property) &&
utilsUsage.property.name === 'cleanup') {
context.report({
node: utilsUsage.property,
messageId: 'noManualCleanup',
});
}
});
}
}
return _a = {
ImportDeclaration: function (node) {
var value = node.source.value;
var testingLibraryWithCleanup = value.match(CLEANUP_LIBRARY_REGEX);
if (!testingLibraryWithCleanup) {
return;
}
if (node_utils_1.isImportDefaultSpecifier(node.specifiers[0])) {
defaultImportFromTestingLibrary = node;
}
var cleanupSpecifier = node.specifiers.find(function (specifier) {
return node_utils_1.isImportSpecifier(specifier) &&
specifier.imported &&
specifier.imported.name === 'cleanup';
});
if (cleanupSpecifier) {
context.report({
node: cleanupSpecifier,
messageId: 'noManualCleanup',
});
}
}
},
_a["VariableDeclarator > CallExpression > Identifier[name=\"require\"]"] = function (node) {
var args = node.parent.arguments;
var literalNodeCleanupModuleName = args.find(function (args) {
return node_utils_1.isLiteral(args) &&
typeof args.value === 'string' &&
args.value.match(CLEANUP_LIBRARY_REGEX);
});
if (!literalNodeCleanupModuleName) {
return;
}
var declaratorNode = node.parent
.parent;
if (node_utils_1.isObjectPattern(declaratorNode.id)) {
var cleanupProperty = declaratorNode.id.properties.find(function (property) {
return node_utils_1.isProperty(property) &&
node_utils_1.isIdentifier(property.key) &&
property.key.name === 'cleanup';
});
if (cleanupProperty) {
context.report({
node: cleanupProperty,
messageId: 'noManualCleanup',
});
}
}
else {
defaultRequireFromTestingLibrary = declaratorNode.id;
}
},
_a['Program:exit'] = function () {
if (defaultImportFromTestingLibrary) {
var references = context.getDeclaredVariables(defaultImportFromTestingLibrary)[0].references;
reportImportReferences(references);
}
if (defaultRequireFromTestingLibrary) {
var references = context
.getDeclaredVariables(defaultRequireFromTestingLibrary.parent)[0]
.references.slice(1);
reportImportReferences(references);
}
},
_a;
},
});