@@ -67,7 +67,13 @@ export function migrateFile(
67
67
} ) ;
68
68
}
69
69
70
- migrateTestingModuleImports ( node , commonHttpTestingIdentifiers , addedImports , changeTracker ) ;
70
+ migrateTestingModuleImports (
71
+ node ,
72
+ commonHttpIdentifiers ,
73
+ commonHttpTestingIdentifiers ,
74
+ addedImports ,
75
+ changeTracker ,
76
+ ) ;
71
77
} ) ;
72
78
73
79
// Imports are for the whole file
@@ -90,6 +96,13 @@ export function migrateFile(
90
96
] ) ;
91
97
changeTracker . replaceNode ( commonHttpImports , newImports ) ;
92
98
}
99
+ // If there are no imports for common/http, and we need to add some
100
+ else if ( addedImports . get ( COMMON_HTTP ) ?. size ) {
101
+ // Then we add a new import statement for common/http
102
+ addedImports . get ( COMMON_HTTP ) ?. forEach ( ( entry ) => {
103
+ changeTracker . addImport ( sourceFile , entry , COMMON_HTTP ) ;
104
+ } ) ;
105
+ }
93
106
94
107
// Remove the HttpModules imports from common/http/testing
95
108
const commonHttpTestingImports = getNamedImports ( sourceFile , COMMON_HTTP_TESTING ) ;
@@ -156,22 +169,23 @@ function migrateDecorator(
156
169
const addedProviders = new Set < ts . CallExpression > ( ) ;
157
170
158
171
// Handle the different imported Http modules
172
+ const commonHttpAddedImports = addedImports . get ( COMMON_HTTP ) ;
159
173
if ( importedModules . client ) {
160
- addedImports . get ( COMMON_HTTP ) ?. add ( WITH_INTERCEPTORS_FROM_DI ) ;
174
+ commonHttpAddedImports ?. add ( WITH_INTERCEPTORS_FROM_DI ) ;
161
175
addedProviders . add ( createCallExpression ( WITH_INTERCEPTORS_FROM_DI ) ) ;
162
176
}
163
177
if ( importedModules . clientJsonp ) {
164
- addedImports . get ( COMMON_HTTP ) ?. add ( WITH_JSONP_SUPPORT ) ;
178
+ commonHttpAddedImports ?. add ( WITH_JSONP_SUPPORT ) ;
165
179
addedProviders . add ( createCallExpression ( WITH_JSONP_SUPPORT ) ) ;
166
180
}
167
181
if ( importedModules . xsrf ) {
168
182
// HttpClientXsrfModule is the only module with Class methods.
169
183
// They correspond to different provider functions
170
184
if ( importedModules . xsrfOptions === 'disable' ) {
171
- addedImports . get ( COMMON_HTTP ) ?. add ( WITH_NOXSRF_PROTECTION ) ;
185
+ commonHttpAddedImports ?. add ( WITH_NOXSRF_PROTECTION ) ;
172
186
addedProviders . add ( createCallExpression ( WITH_NOXSRF_PROTECTION ) ) ;
173
187
} else {
174
- addedImports . get ( COMMON_HTTP ) ?. add ( WITH_XSRF_CONFIGURATION ) ;
188
+ commonHttpAddedImports ?. add ( WITH_XSRF_CONFIGURATION ) ;
175
189
addedProviders . add (
176
190
createCallExpression (
177
191
WITH_XSRF_CONFIGURATION ,
@@ -192,20 +206,23 @@ function migrateDecorator(
192
206
] ) ;
193
207
194
208
// Adding the new providers
195
- addedImports . get ( COMMON_HTTP ) ?. add ( PROVIDE_HTTP_CLIENT ) ;
209
+ commonHttpAddedImports ?. add ( PROVIDE_HTTP_CLIENT ) ;
196
210
const providers = getProvidersFromLiteralExpr ( metadata ) ;
197
211
const provideHttpExpr = createCallExpression ( PROVIDE_HTTP_CLIENT , [ ...addedProviders ] ) ;
198
212
199
213
let newProviders : ts . ArrayLiteralExpression ;
200
214
if ( ! providers ) {
201
- // No existing providers, we add an property to the literal
215
+ // No existing providers, we add a property to the literal
202
216
newProviders = ts . factory . createArrayLiteralExpression ( [ provideHttpExpr ] ) ;
203
217
} else {
204
218
// We add the provider to the existing provider array
205
- newProviders = ts . factory . createArrayLiteralExpression ( [
206
- ...providers . elements ,
207
- provideHttpExpr ,
208
- ] ) ;
219
+ newProviders = ts . factory . updateArrayLiteralExpression (
220
+ providers ,
221
+ ts . factory . createNodeArray (
222
+ [ ...providers . elements , provideHttpExpr ] ,
223
+ providers . elements . hasTrailingComma ,
224
+ ) ,
225
+ ) ;
209
226
}
210
227
211
228
// Replacing the existing decorator with the new one (with the new imports and providers)
@@ -219,6 +236,7 @@ function migrateDecorator(
219
236
220
237
function migrateTestingModuleImports (
221
238
node : ts . Node ,
239
+ commonHttpIdentifiers : Set < string > ,
222
240
commonHttpTestingIdentifiers : Set < string > ,
223
241
addedImports : Map < string , Set < string > > ,
224
242
changeTracker : ChangeTracker ,
@@ -248,51 +266,98 @@ function migrateTestingModuleImports(
248
266
return ;
249
267
}
250
268
251
- // Does the imports array contain the HttpClientTestingModule?
252
- const httpClientTesting = importsArray . elements . find (
253
- ( elt ) => elt . getText ( ) === HTTP_CLIENT_TESTING_MODULE ,
254
- ) ;
255
- if ( ! httpClientTesting || ! commonHttpTestingIdentifiers . has ( HTTP_CLIENT_TESTING_MODULE ) ) {
256
- return ;
257
- }
269
+ const commonHttpAddedImports = addedImports . get ( COMMON_HTTP ) ;
258
270
259
- addedImports . get ( COMMON_HTTP_TESTING ) ?. add ( PROVIDE_HTTP_CLIENT_TESTING ) ;
271
+ // Does the imports array contain the HttpClientModule?
272
+ const httpClient = importsArray . elements . find ( ( elt ) => elt . getText ( ) === HTTP_CLIENT_MODULE ) ;
273
+ if ( httpClient && commonHttpIdentifiers . has ( HTTP_CLIENT_MODULE ) ) {
274
+ // We add the imports for provideHttpClient(withInterceptorsFromDi())
275
+ commonHttpAddedImports ?. add ( PROVIDE_HTTP_CLIENT ) ;
276
+ commonHttpAddedImports ?. add ( WITH_INTERCEPTORS_FROM_DI ) ;
260
277
261
- const newImports = ts . factory . createArrayLiteralExpression ( [
262
- ...importsArray . elements . filter ( ( item ) => item !== httpClientTesting ) ,
263
- ] ) ;
278
+ const newImports = ts . factory . createArrayLiteralExpression ( [
279
+ ...importsArray . elements . filter ( ( item ) => item !== httpClient ) ,
280
+ ] ) ;
264
281
265
- const provideHttpClient = createCallExpression ( PROVIDE_HTTP_CLIENT , [
266
- createCallExpression ( WITH_INTERCEPTORS_FROM_DI ) ,
267
- ] ) ;
268
- const provideHttpClientTesting = createCallExpression ( PROVIDE_HTTP_CLIENT_TESTING ) ;
282
+ const provideHttpClient = createCallExpression ( PROVIDE_HTTP_CLIENT , [
283
+ createCallExpression ( WITH_INTERCEPTORS_FROM_DI ) ,
284
+ ] ) ;
269
285
270
- // Adding the new providers
271
- const providers = getProvidersFromLiteralExpr ( configureTestingModuleArgs ) ;
286
+ // Adding the new provider
287
+ const providers = getProvidersFromLiteralExpr ( configureTestingModuleArgs ) ;
272
288
273
- let newProviders : ts . ArrayLiteralExpression ;
274
- if ( ! providers ) {
275
- // No existing providers, we add an property to the literal
276
- newProviders = ts . factory . createArrayLiteralExpression ( [
277
- provideHttpClient ,
278
- provideHttpClientTesting ,
289
+ let newProviders : ts . ArrayLiteralExpression ;
290
+ if ( ! providers ) {
291
+ // No existing providers, we add a property to the literal
292
+ newProviders = ts . factory . createArrayLiteralExpression ( [ provideHttpClient ] ) ;
293
+ } else {
294
+ // We add the provider to the existing provider array
295
+ newProviders = ts . factory . updateArrayLiteralExpression (
296
+ providers ,
297
+ ts . factory . createNodeArray (
298
+ [ ...providers . elements , provideHttpClient ] ,
299
+ providers . elements . hasTrailingComma ,
300
+ ) ,
301
+ ) ;
302
+ }
303
+
304
+ // Replacing the existing configuration with the new one (with the new imports and providers)
305
+ const newTestingModuleArgs = updateTestBedConfiguration (
306
+ configureTestingModuleArgs ,
307
+ newImports ,
308
+ newProviders ,
309
+ ) ;
310
+ changeTracker . replaceNode ( configureTestingModuleArgs , newTestingModuleArgs ) ;
311
+ }
312
+
313
+ // Does the imports array contain the HttpClientTestingModule?
314
+ const httpClientTesting = importsArray . elements . find (
315
+ ( elt ) => elt . getText ( ) === HTTP_CLIENT_TESTING_MODULE ,
316
+ ) ;
317
+ if ( httpClientTesting && commonHttpTestingIdentifiers . has ( HTTP_CLIENT_TESTING_MODULE ) ) {
318
+ // We add the imports for provideHttpClient(withInterceptorsFromDi()) and provideHttpClientTesting()
319
+ commonHttpAddedImports ?. add ( PROVIDE_HTTP_CLIENT ) ;
320
+ commonHttpAddedImports ?. add ( WITH_INTERCEPTORS_FROM_DI ) ;
321
+ addedImports . get ( COMMON_HTTP_TESTING ) ?. add ( PROVIDE_HTTP_CLIENT_TESTING ) ;
322
+
323
+ const newImports = ts . factory . createArrayLiteralExpression ( [
324
+ ...importsArray . elements . filter ( ( item ) => item !== httpClientTesting ) ,
279
325
] ) ;
280
- } else {
281
- // We add the provider to the existing provider array
282
- newProviders = ts . factory . createArrayLiteralExpression ( [
283
- ...providers . elements ,
284
- provideHttpClient ,
285
- provideHttpClientTesting ,
326
+
327
+ const provideHttpClient = createCallExpression ( PROVIDE_HTTP_CLIENT , [
328
+ createCallExpression ( WITH_INTERCEPTORS_FROM_DI ) ,
286
329
] ) ;
287
- }
330
+ const provideHttpClientTesting = createCallExpression ( PROVIDE_HTTP_CLIENT_TESTING ) ;
331
+
332
+ // Adding the new providers
333
+ const providers = getProvidersFromLiteralExpr ( configureTestingModuleArgs ) ;
334
+
335
+ let newProviders : ts . ArrayLiteralExpression ;
336
+ if ( ! providers ) {
337
+ // No existing providers, we add a property to the literal
338
+ newProviders = ts . factory . createArrayLiteralExpression ( [
339
+ provideHttpClient ,
340
+ provideHttpClientTesting ,
341
+ ] ) ;
342
+ } else {
343
+ // We add the provider to the existing provider array
344
+ newProviders = ts . factory . updateArrayLiteralExpression (
345
+ providers ,
346
+ ts . factory . createNodeArray (
347
+ [ ...providers . elements , provideHttpClient , provideHttpClientTesting ] ,
348
+ providers . elements . hasTrailingComma ,
349
+ ) ,
350
+ ) ;
351
+ }
288
352
289
- // Replacing the existing decorator with the new one (with the new imports and providers)
290
- const newTestingModuleArgs = ts . factory . createObjectLiteralExpression ( [
291
- ...configureTestingModuleArgs . properties . filter ( ( p ) => p . getText ( ) === 'imports' ) ,
292
- ts . factory . createPropertyAssignment ( 'imports' , newImports ) ,
293
- ts . factory . createPropertyAssignment ( 'providers' , newProviders ) ,
294
- ] ) ;
295
- changeTracker . replaceNode ( configureTestingModuleArgs , newTestingModuleArgs ) ;
353
+ // Replacing the existing configuration with the new one (with the new imports and providers)
354
+ const newTestingModuleArgs = updateTestBedConfiguration (
355
+ configureTestingModuleArgs ,
356
+ newImports ,
357
+ newProviders ,
358
+ ) ;
359
+ changeTracker . replaceNode ( configureTestingModuleArgs , newTestingModuleArgs ) ;
360
+ }
296
361
}
297
362
298
363
function getImportsProp ( literal : ts . ObjectLiteralExpression ) {
@@ -387,3 +452,14 @@ function createCallExpression(functionName: string, args: ts.Expression[] = [])
387
452
args ,
388
453
) ;
389
454
}
455
+
456
+ function updateTestBedConfiguration (
457
+ configureTestingModuleArgs : ts . ObjectLiteralExpression ,
458
+ newImports : ts . ArrayLiteralExpression ,
459
+ newProviders : ts . ArrayLiteralExpression ,
460
+ ) : ts . ObjectLiteralExpression {
461
+ return ts . factory . updateObjectLiteralExpression ( configureTestingModuleArgs , [
462
+ ts . factory . createPropertyAssignment ( 'imports' , newImports ) ,
463
+ ts . factory . createPropertyAssignment ( 'providers' , newProviders ) ,
464
+ ] ) ;
465
+ }
0 commit comments