@@ -34,111 +34,62 @@ export function getImportPathTransformer({ pattern, template }) {
34
34
export const importPathTransformer = ( ) => ( {
35
35
before : [
36
36
transformImportPath ( {
37
- pattern : / ^ ( @ f i r e b a s e .* ) - e x p ( .* ) $ / ,
37
+ pattern : / ^ ( @ f i r e b a s e .* ) - e x p ( .* ) $ / g ,
38
38
template : [ 1 , 2 ]
39
39
} )
40
40
] ,
41
41
after : [ ] ,
42
42
afterDeclarations : [
43
43
transformImportPath ( {
44
- pattern : / ^ ( @ f i r e b a s e .* ) - e x p ( .* ) $ / ,
44
+ pattern : / ^ ( @ f i r e b a s e .* ) - e x p ( .* ) $ / g ,
45
45
template : [ 1 , 2 ]
46
46
} )
47
47
]
48
48
} ) ;
49
49
50
50
function transformImportPath ( { pattern, template } ) {
51
51
return context => file => {
52
- return visitNodeAndChildren (
53
- file ,
54
- context ,
55
- { pattern, template } ,
56
- transformNode
57
- ) ;
52
+ return visitNodeAndChildren ( file , context , { pattern, template } ) ;
58
53
} ;
59
54
}
60
55
61
- function visitNodeAndChildren (
62
- node ,
63
- context ,
64
- { pattern, template } ,
65
- nodeTransformer
66
- ) {
56
+ function visitNodeAndChildren ( node , context , { pattern, template } ) {
67
57
return ts . visitEachChild (
68
- nodeTransformer ( node , { pattern, template } ) ,
58
+ visitNode ( node , { pattern, template } ) ,
69
59
childNode =>
70
- visitNodeAndChildren (
71
- childNode ,
72
- context ,
73
- { pattern, template } ,
74
- nodeTransformer
75
- ) ,
60
+ visitNodeAndChildren ( childNode , context , { pattern, template } ) ,
76
61
context
77
62
) ;
78
63
}
79
64
80
- function replacePath ( pathString , pattern , template ) {
81
- const pathStringWithoutQuotes = pathString . substr ( 1 , pathString . length - 2 ) ;
82
-
83
- const captures = pattern . exec ( pathStringWithoutQuotes ) ;
84
-
85
- if ( captures ) {
86
- const newNameFragments = [ ] ;
87
- for ( const fragment of template ) {
88
- if ( typeof fragment === 'number' ) {
89
- newNameFragments . push ( captures [ fragment ] ) ;
90
- } else if ( typeof fragment === 'string' ) {
91
- newNameFragments . push ( fragment ) ;
92
- } else {
93
- throw Error ( `unrecognized fragment: ${ fragment } ` ) ;
94
- }
95
- }
96
- return newNameFragments . join ( '' ) ;
97
- }
98
-
99
- return null ;
100
- }
101
-
102
- function transformNode ( node , { pattern, template } ) {
65
+ function visitNode ( node , { pattern, template } ) {
66
+ let importPath ;
103
67
if (
104
68
( ts . isImportDeclaration ( node ) || ts . isExportDeclaration ( node ) ) &&
105
69
node . moduleSpecifier
106
70
) {
107
71
const importPathWithQuotes = node . moduleSpecifier . getText ( ) ;
108
- const newName = replacePath ( importPathWithQuotes , pattern , template ) ;
72
+ importPath = importPathWithQuotes . substr (
73
+ 1 ,
74
+ importPathWithQuotes . length - 2
75
+ ) ;
76
+
77
+ const captures = pattern . exec ( importPath ) ;
109
78
110
- if ( newName ) {
111
- if ( ts . isImportDeclaration ( node ) ) {
112
- return ts . factory . updateImportDeclaration (
113
- node ,
114
- node . decorators ,
115
- node . modifiers ,
116
- node . importClause ,
117
- ts . factory . createStringLiteral ( newName ) // moduleSpecifier
118
- ) ;
119
- } else if ( ts . isExportDeclaration ( node ) ) {
120
- return ts . factory . updateExportDeclaration (
121
- node ,
122
- node . decorators ,
123
- node . modifiers ,
124
- node . isTypeOnly ,
125
- node . exportClause ,
126
- ts . factory . createStringLiteral ( newName ) // moduleSpecifier
127
- ) ;
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
+ }
128
89
}
129
- // Just in case.
130
- return node ;
131
- }
132
- } else if ( ts . isModuleDeclaration ( node ) && node . name ) {
133
- const importPathWithQuotes = node . name . getText ( ) ;
134
- const newName = replacePath ( importPathWithQuotes , pattern , template ) ;
135
- if ( newName ) {
136
- const newNode = ts . factory . updateModuleDeclaration (
137
- node ,
138
- node . decorators ,
139
- node . modifiers ,
140
- ts . factory . createStringLiteral ( newName ) // name
141
- ) ;
90
+ const newName = newNameFragments . join ( '' ) ;
91
+ const newNode = ts . getMutableClone ( node ) ;
92
+ newNode . moduleSpecifier = ts . createLiteral ( newName ) ;
142
93
return newNode ;
143
94
}
144
95
}
0 commit comments