@@ -3,12 +3,15 @@ import core, {Collection} from 'jscodeshift';
33export function hasImportDeclaration (
44 j : core . JSCodeshift ,
55 source : Collection < any > ,
6- sourcePath : string ,
6+ sourcePath : string | RegExp ,
77) {
88 return Boolean (
9- source
10- . find ( j . ImportDeclaration )
11- . filter ( ( path ) => path . node . source . value === sourcePath ) . length ,
9+ source . find ( j . ImportDeclaration ) . filter ( ( path ) => {
10+ const nodePath = path . node . source . value ;
11+ return typeof sourcePath === 'string'
12+ ? nodePath === sourcePath
13+ : sourcePath . test ( nodePath ?. toString ( ) ?? '' ) ;
14+ } ) . length ,
1215 ) ;
1316}
1417
@@ -85,11 +88,16 @@ export function getImportSpecifier(
8588 j : core . JSCodeshift ,
8689 source : Collection < any > ,
8790 specifier : string ,
88- sourcePath : string ,
91+ sourcePath : string | RegExp ,
8992) {
9093 return source
9194 . find ( j . ImportDeclaration )
92- . filter ( ( path ) => path . node . source . value === sourcePath )
95+ . filter ( ( path ) => {
96+ const nodePath = path . node . source . value ;
97+ return typeof sourcePath === 'string'
98+ ? nodePath === sourcePath
99+ : sourcePath . test ( nodePath ?. toString ( ) ?? '' ) ;
100+ } )
93101 . find ( j . ImportSpecifier )
94102 . filter ( ( path ) => path . value . imported . name === specifier ) ;
95103}
@@ -98,9 +106,10 @@ export function getImportSpecifierName(
98106 j : core . JSCodeshift ,
99107 source : Collection < any > ,
100108 specifier : string ,
101- sourcePath : string ,
109+ sourcePath : string | RegExp ,
102110) {
103111 const specifiers = getImportSpecifier ( j , source , specifier , sourcePath ) ;
112+ console . log ( { specifiers} ) ;
104113
105114 return specifiers . length > 0 ? specifiers . nodes ( ) [ 0 ] ! . local ! . name : null ;
106115}
@@ -109,7 +118,7 @@ export function hasImportSpecifier(
109118 j : core . JSCodeshift ,
110119 source : Collection < any > ,
111120 specifier : string ,
112- sourcePath : string ,
121+ sourcePath : string | RegExp ,
113122) {
114123 return Boolean ( getImportSpecifier ( j , source , specifier , sourcePath ) ?. length ) ;
115124}
@@ -139,8 +148,16 @@ export function renameImportSpecifier(
139148 source : Collection < any > ,
140149 specifier : string ,
141150 newSpecifier : string ,
142- sourcePath : string ,
151+ sourcePath : string | RegExp ,
143152) {
153+ console . log ( 'inside rename' ) ;
154+ console . log ( getImportSpecifier ( j , source , specifier , sourcePath ) ) ;
155+ // console.log(j.importSpecifier(j.identifier(newSpecifier)));
156+ // if (typeof sourcePath === 'string') {
157+ // console.log({sourcePath});
158+ // } else {
159+ // console.log('inside the else');
160+ // }
144161 getImportSpecifier ( j , source , specifier , sourcePath ) . replaceWith (
145162 j . importSpecifier ( j . identifier ( newSpecifier ) ) ,
146163 ) ;
@@ -150,7 +167,9 @@ export function removeImportSpecifier(
150167 j : core . JSCodeshift ,
151168 source : Collection < any > ,
152169 specifier : string ,
153- sourcePath : string ,
170+ sourcePath : string | RegExp ,
154171) {
172+ console . log ( 'inside remove' ) ;
173+ console . log ( getImportSpecifier ( j , source , specifier , sourcePath ) ) ;
155174 getImportSpecifier ( j , source , specifier , sourcePath ) . remove ( ) ;
156175}
0 commit comments