@@ -49,45 +49,88 @@ export const importPathTransformer = () => ({
49
49
50
50
function transformImportPath ( { pattern, template } ) {
51
51
return context => file => {
52
- return visitNodeAndChildren ( file , context , { pattern, template } ) ;
52
+ const firstPass = visitNodeAndChildren (
53
+ file ,
54
+ context ,
55
+ { pattern, template } ,
56
+ transformDeclareNode
57
+ ) ;
58
+ return visitNodeAndChildren (
59
+ firstPass ,
60
+ context ,
61
+ { pattern, template } ,
62
+ transformImportExportNode
63
+ ) ;
53
64
} ;
54
65
}
55
66
56
- function visitNodeAndChildren ( node , context , { pattern, template } ) {
67
+ function visitNodeAndChildren (
68
+ node ,
69
+ context ,
70
+ { pattern, template } ,
71
+ nodeTransformer
72
+ ) {
57
73
return ts . visitEachChild (
58
- visitNode ( node , { pattern, template } ) ,
74
+ nodeTransformer ( node , { pattern, template } ) ,
59
75
childNode =>
60
- visitNodeAndChildren ( childNode , context , { pattern, template } ) ,
76
+ visitNodeAndChildren (
77
+ childNode ,
78
+ context ,
79
+ { pattern, template } ,
80
+ nodeTransformer
81
+ ) ,
61
82
context
62
83
) ;
63
84
}
64
85
65
- function visitNode ( node , { pattern, template } ) {
66
- let importPath ;
86
+ function replacePath ( pathString , pattern , template ) {
87
+ const pathStringWithoutQuotes = pathString . substr ( 1 , pathString . length - 2 ) ;
88
+
89
+ const captures = pattern . exec ( pathStringWithoutQuotes ) ;
90
+
91
+ if ( captures ) {
92
+ const newNameFragments = [ ] ;
93
+ for ( const fragment of template ) {
94
+ if ( typeof fragment === 'number' ) {
95
+ newNameFragments . push ( captures [ fragment ] ) ;
96
+ } else if ( typeof fragment === 'string' ) {
97
+ newNameFragments . push ( fragment ) ;
98
+ } else {
99
+ throw Error ( `unrecognized fragment: ${ fragment } ` ) ;
100
+ }
101
+ }
102
+ return newNameFragments . join ( '' ) ;
103
+ }
104
+
105
+ return null ;
106
+ }
107
+
108
+ function transformDeclareNode ( node , { pattern, template } ) {
109
+ if ( ts . isModuleDeclaration ( node ) && node . name ) {
110
+ const importPathWithQuotes = node . name . getText ( ) ;
111
+
112
+ const newName = replacePath ( importPathWithQuotes , pattern , template ) ;
113
+ if ( newName ) {
114
+ const newNode = ts . getMutableClone ( node ) ;
115
+ newNode . name = ts . createLiteral ( newName ) ;
116
+ return newNode ;
117
+ }
118
+ return node ;
119
+ }
120
+
121
+ return node ;
122
+ }
123
+
124
+ function transformImportExportNode ( node , { pattern, template } ) {
67
125
if (
68
126
( ts . isImportDeclaration ( node ) || ts . isExportDeclaration ( node ) ) &&
69
127
node . moduleSpecifier
70
128
) {
71
129
const importPathWithQuotes = node . moduleSpecifier . getText ( ) ;
72
- importPath = importPathWithQuotes . substr (
73
- 1 ,
74
- importPathWithQuotes . length - 2
75
- ) ;
76
130
77
- const captures = pattern . exec ( importPath ) ;
131
+ const newName = replacePath ( importPathWithQuotes , pattern , template ) ;
78
132
79
- if ( captures ) {
80
- const newNameFragments = [ ] ;
81
- for ( const fragment of template ) {
82
- if ( typeof fragment === 'number' ) {
83
- newNameFragments . push ( captures [ fragment ] ) ;
84
- } else if ( typeof fragment === 'string' ) {
85
- newNameFragments . push ( fragment ) ;
86
- } else {
87
- throw Error ( `unrecognized fragment: ${ fragment } ` ) ;
88
- }
89
- }
90
- const newName = newNameFragments . join ( '' ) ;
133
+ if ( newName ) {
91
134
const newNode = ts . getMutableClone ( node ) ;
92
135
newNode . moduleSpecifier = ts . createLiteral ( newName ) ;
93
136
return newNode ;
0 commit comments