no-dom-import.js
3.55 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
"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-dom-import';
var DOM_TESTING_LIBRARY_MODULES = [
'dom-testing-library',
'@testing-library/dom',
];
exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
name: exports.RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow importing from DOM Testing Library',
category: 'Best Practices',
recommended: false,
},
messages: {
noDomImport: 'import from DOM Testing Library is restricted, import from corresponding Testing Library framework instead',
noDomImportFramework: 'import from DOM Testing Library is restricted, import from {{module}} instead',
},
fixable: 'code',
schema: [
{
type: 'string',
},
],
},
defaultOptions: [''],
create: function (context, _a) {
var _b;
var framework = _a[0];
function report(node, moduleName) {
if (framework) {
var isRequire_1 = node_utils_1.isIdentifier(node) && node.name === 'require';
var correctModuleName_1 = moduleName.replace('dom', framework);
context.report({
node: node,
messageId: 'noDomImportFramework',
data: {
module: correctModuleName_1,
},
fix: function (fixer) {
if (isRequire_1) {
var callExpression = node.parent;
var name_1 = callExpression.arguments[0];
return fixer.replaceText(name_1, name_1.raw.replace(moduleName, correctModuleName_1));
}
else {
var importDeclaration = node;
var name_2 = importDeclaration.source;
return fixer.replaceText(name_2, name_2.raw.replace(moduleName, correctModuleName_1));
}
},
});
}
else {
context.report({
node: node,
messageId: 'noDomImport',
});
}
}
return _b = {
ImportDeclaration: function (node) {
var value = node.source.value;
var domModuleName = DOM_TESTING_LIBRARY_MODULES.find(function (module) { return module === value; });
if (domModuleName) {
report(node, domModuleName);
}
}
},
_b["CallExpression > Identifier[name=\"require\"]"] = function (node) {
var callExpression = node.parent;
var args = callExpression.arguments;
var literalNodeDomModuleName = args.find(function (args) {
return node_utils_1.isLiteral(args) &&
typeof args.value === 'string' &&
DOM_TESTING_LIBRARY_MODULES.includes(args.value);
});
if (literalNodeDomModuleName) {
report(node, literalNodeDomModuleName.value);
}
},
_b;
},
});