@@ -12,6 +12,14 @@ const DOM_TESTING_LIBRARY_MODULES = [
1212 '@testing-library/dom' ,
1313] ;
1414
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+
1523export default createTestingLibraryRule < Options , MessageIds > ( {
1624 name : RULE_NAME ,
1725 meta : {
@@ -33,11 +41,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
3341 'import from DOM Testing Library is restricted, import from {{module}} instead' ,
3442 } ,
3543 fixable : 'code' ,
36- schema : [
37- {
38- type : 'string' ,
39- } ,
40- ] ,
44+ schema : [ { type : 'string' } ] ,
4145 } ,
4246 defaultOptions : [ '' ] ,
4347
@@ -46,42 +50,36 @@ export default createTestingLibraryRule<Options, MessageIds>({
4650 node : TSESTree . CallExpression | TSESTree . ImportDeclaration ,
4751 moduleName : string
4852 ) {
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 ( {
8155 node,
8256 messageId : 'noDomImport' ,
8357 } ) ;
8458 }
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+ } ) ;
8583 }
8684
8785 return {
0 commit comments