-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathno-dom-import.ts
110 lines (101 loc) · 3.28 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
104
105
106
107
108
109
110
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { getDocsUrl } from '../utils';
import { isLiteral, isIdentifier } 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 ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: 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(context, [framework]) {
function report(
node: TSESTree.ImportDeclaration | TSESTree.Identifier,
moduleName: string
) {
if (framework) {
const isRequire = isIdentifier(node) && node.name === 'require';
const correctModuleName = moduleName.replace('dom', framework);
context.report({
node,
messageId: 'noDomImportFramework',
data: {
module: correctModuleName,
},
fix(fixer) {
if (isRequire) {
const callExpression = node.parent as TSESTree.CallExpression;
const name = callExpression.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 importDeclaration = node as TSESTree.ImportDeclaration;
const name = importDeclaration.source;
return fixer.replaceText(
name,
name.raw.replace(moduleName, correctModuleName)
);
}
},
});
} else {
context.report({
node,
messageId: 'noDomImport',
});
}
}
return {
ImportDeclaration(node) {
const value = node.source.value;
const domModuleName = DOM_TESTING_LIBRARY_MODULES.find(
(module) => module === value
);
if (domModuleName) {
report(node, domModuleName);
}
},
[`CallExpression > Identifier[name="require"]`](
node: TSESTree.Identifier
) {
const callExpression = node.parent as TSESTree.CallExpression;
const { arguments: args } = callExpression;
const literalNodeDomModuleName = args.find(
(args) =>
isLiteral(args) &&
typeof args.value === 'string' &&
DOM_TESTING_LIBRARY_MODULES.includes(args.value)
) as TSESTree.Literal;
if (literalNodeDomModuleName) {
report(node, literalNodeDomModuleName.value as string);
}
},
};
},
});