@@ -12,6 +12,14 @@ const DOM_TESTING_LIBRARY_MODULES = [
12
12
'@testing-library/dom' ,
13
13
] ;
14
14
15
+ const correctModuleNameByFramework = {
16
+ angular : '@testing-library/angular' , // ATL is *always* called `@testing-library/angular`
17
+ marko : '@marko/testing-library' , // Marko TL is called `@marko/testing-library`
18
+ } ;
19
+ const getCorrectModuleName = ( moduleName : string , framework : string ) : string =>
20
+ correctModuleNameByFramework [ framework ] ||
21
+ moduleName . replace ( 'dom' , framework ) ;
22
+
15
23
export default createTestingLibraryRule < Options , MessageIds > ( {
16
24
name : RULE_NAME ,
17
25
meta : {
@@ -33,11 +41,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
33
41
'import from DOM Testing Library is restricted, import from {{module}} instead' ,
34
42
} ,
35
43
fixable : 'code' ,
36
- schema : [
37
- {
38
- type : 'string' ,
39
- } ,
40
- ] ,
44
+ schema : [ { type : 'string' } ] ,
41
45
} ,
42
46
defaultOptions : [ '' ] ,
43
47
@@ -46,42 +50,36 @@ export default createTestingLibraryRule<Options, MessageIds>({
46
50
node : TSESTree . CallExpression | TSESTree . ImportDeclaration ,
47
51
moduleName : string
48
52
) {
49
- if ( framework ) {
50
- // marko TL is called @marko /testing-library
51
- const correctModuleName =
52
- framework === 'marko'
53
- ? moduleName . replace ( 'dom-' , `@${ framework } /` )
54
- : moduleName . replace ( 'dom' , framework ) ;
55
- context . report ( {
56
- node,
57
- messageId : 'noDomImportFramework' ,
58
- data : {
59
- module : correctModuleName ,
60
- } ,
61
- fix ( fixer ) {
62
- if ( isCallExpression ( node ) ) {
63
- const name = node . arguments [ 0 ] as TSESTree . Literal ;
64
-
65
- // Replace the module name with the raw module name as we can't predict which punctuation the user is going to use
66
- return fixer . replaceText (
67
- name ,
68
- name . raw . replace ( moduleName , correctModuleName )
69
- ) ;
70
- } else {
71
- const name = node . source ;
72
- return fixer . replaceText (
73
- name ,
74
- name . raw . replace ( moduleName , correctModuleName )
75
- ) ;
76
- }
77
- } ,
78
- } ) ;
79
- } else {
80
- context . report ( {
53
+ if ( ! framework ) {
54
+ return context . report ( {
81
55
node,
82
56
messageId : 'noDomImport' ,
83
57
} ) ;
84
58
}
59
+
60
+ const correctModuleName = getCorrectModuleName ( moduleName , framework ) ;
61
+ context . report ( {
62
+ data : { module : correctModuleName } ,
63
+ fix ( fixer ) {
64
+ if ( isCallExpression ( node ) ) {
65
+ const name = node . arguments [ 0 ] as TSESTree . Literal ;
66
+
67
+ // Replace the module name with the raw module name as we can't predict which punctuation the user is going to use
68
+ return fixer . replaceText (
69
+ name ,
70
+ name . raw . replace ( moduleName , correctModuleName )
71
+ ) ;
72
+ } else {
73
+ const name = node . source ;
74
+ return fixer . replaceText (
75
+ name ,
76
+ name . raw . replace ( moduleName , correctModuleName )
77
+ ) ;
78
+ }
79
+ } ,
80
+ messageId : 'noDomImportFramework' ,
81
+ node,
82
+ } ) ;
85
83
}
86
84
87
85
return {
0 commit comments