-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathno-dom-import.ts
103 lines (92 loc) · 2.75 KB
/
no-dom-import.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
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import { isCallExpression } from '../node-utils';
export const RULE_NAME = 'no-dom-import';
export type MessageIds = 'noDomImport' | 'noDomImportFramework';
type Options = [string];
const DOM_TESTING_LIBRARY_MODULES = [
'dom-testing-library',
'@testing-library/dom',
];
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow importing from DOM Testing Library',
recommendedConfig: {
dom: false,
angular: ['error', 'angular'],
react: ['error', 'react'],
vue: ['error', 'vue'],
},
},
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(context, [framework], helpers) {
function report(
node: TSESTree.CallExpression | TSESTree.ImportDeclaration,
moduleName: string
) {
if (framework) {
const correctModuleName = moduleName.replace('dom', framework);
context.report({
node,
messageId: 'noDomImportFramework',
data: {
module: correctModuleName,
},
fix(fixer) {
if (isCallExpression(node)) {
const name = node.arguments[0] as TSESTree.Literal;
// Replace the module name with the raw module name as we can't predict which punctuation the user is going to use
return fixer.replaceText(
name,
name.raw.replace(moduleName, correctModuleName)
);
} else {
const name = node.source;
return fixer.replaceText(
name,
name.raw.replace(moduleName, correctModuleName)
);
}
},
});
} else {
context.report({
node,
messageId: 'noDomImport',
});
}
}
return {
'Program:exit'() {
const importName = helpers.getTestingLibraryImportName();
const importNode = helpers.getTestingLibraryImportNode();
if (!importNode) {
return;
}
const domModuleName = DOM_TESTING_LIBRARY_MODULES.find(
(module) => module === importName
);
if (!domModuleName) {
return;
}
report(importNode, domModuleName);
},
};
},
});