@@ -6,16 +6,11 @@ function isImportPresent(j: JSCodeshift, ast: Node, path: string): boolean {
6
6
throw new Error ( `path parameter should be string, recieved ${ typeof path } ` ) ;
7
7
}
8
8
let importExists = false ;
9
- ast . find ( j . CallExpression ) . forEach (
10
- ( callExp : Node ) : void => {
11
- if (
12
- ( callExp . value as Node ) . callee . name === "require" &&
13
- ( callExp . value as Node ) . arguments [ 0 ] . value === path
14
- ) {
15
- importExists = true ;
16
- }
9
+ ast . find ( j . CallExpression ) . forEach ( ( callExp : Node ) : void => {
10
+ if ( ( callExp . value as Node ) . callee . name === "require" && ( callExp . value as Node ) . arguments [ 0 ] . value === path ) {
11
+ importExists = true ;
17
12
}
18
- ) ;
13
+ } ) ;
19
14
return importExists ;
20
15
}
21
16
@@ -93,13 +88,11 @@ function pathsToMemberExpression(j: JSCodeshift, paths: string[]): Node {
93
88
*/
94
89
95
90
function findPluginsByName ( j : JSCodeshift , node : Node , pluginNamesArray : string [ ] ) : Node {
96
- return node . find ( j . NewExpression ) . filter (
97
- ( path : Node ) : boolean => {
98
- return pluginNamesArray . some (
99
- ( plugin : string ) : boolean => memberExpressionToPathString ( path . get ( "callee" ) . value as Node ) === plugin
100
- ) ;
101
- }
102
- ) ;
91
+ return node . find ( j . NewExpression ) . filter ( ( path : Node ) : boolean => {
92
+ return pluginNamesArray . some (
93
+ ( plugin : string ) : boolean => memberExpressionToPathString ( path . get ( "callee" ) . value as Node ) === plugin
94
+ ) ;
95
+ } ) ;
103
96
}
104
97
105
98
/**
@@ -110,14 +103,12 @@ function findPluginsByName(j: JSCodeshift, node: Node, pluginNamesArray: string[
110
103
*/
111
104
112
105
function findPluginsArrayAndRemoveIfEmpty ( j : JSCodeshift , rootNode : Node ) : Node {
113
- return rootNode . find ( j . Identifier , { name : "plugins" } ) . forEach (
114
- ( node : Node ) : void => {
115
- const elements = safeTraverse ( node , [ "parent" , "value" , "value" , "elements" ] ) as Node [ ] ;
116
- if ( ! elements . length ) {
117
- j ( node . parent ) . remove ( ) ;
118
- }
106
+ return rootNode . find ( j . Identifier , { name : "plugins" } ) . forEach ( ( node : Node ) : void => {
107
+ const elements = safeTraverse ( node , [ "parent" , "value" , "value" , "elements" ] ) as Node [ ] ;
108
+ if ( ! elements . length ) {
109
+ j ( node . parent ) . remove ( ) ;
119
110
}
120
- ) ;
111
+ } ) ;
121
112
}
122
113
123
114
/**
@@ -250,15 +241,11 @@ function addOrUpdateConfigObject(
250
241
if ( propertyExists ) {
251
242
rootNode . properties
252
243
. filter ( ( path : Node ) : boolean => path . key . name === configProperty )
253
- . forEach (
254
- ( path : Node ) : void => {
255
- const newProperties = ( path . value as Node ) . properties . filter (
256
- ( p : Node ) : boolean => p . key . name !== key
257
- ) ;
258
- newProperties . push ( j . objectProperty ( j . identifier ( key ) , value ) ) ;
259
- ( path . value as Node ) . properties = newProperties ;
260
- }
261
- ) ;
244
+ . forEach ( ( path : Node ) : void => {
245
+ const newProperties = ( path . value as Node ) . properties . filter ( ( p : Node ) : boolean => p . key . name !== key ) ;
246
+ newProperties . push ( j . objectProperty ( j . identifier ( key ) , value ) ) ;
247
+ ( path . value as Node ) . properties = newProperties ;
248
+ } ) ;
262
249
} else {
263
250
rootNode . properties . push (
264
251
j . objectProperty (
@@ -285,17 +272,15 @@ function findAndRemovePluginByName(j: JSCodeshift, node: Node, pluginName: strin
285
272
286
273
findPluginsByName ( j , node , [ pluginName ] )
287
274
. filter ( ( path : Node ) : boolean => ! ! safeTraverse ( path , [ "parent" , "value" ] ) )
288
- . forEach (
289
- ( path : Node ) : void => {
290
- rootPath = safeTraverse ( path , [ "parent" , "parent" , "parent" , "value" ] ) as Node ;
291
- const arrayPath = path . parent . value as Node ;
292
- if ( arrayPath . elements && arrayPath . elements . length === 1 ) {
293
- j ( path . parent . parent ) . remove ( ) ;
294
- } else {
295
- j ( path ) . remove ( ) ;
296
- }
275
+ . forEach ( ( path : Node ) : void => {
276
+ rootPath = safeTraverse ( path , [ "parent" , "parent" , "parent" , "value" ] ) as Node ;
277
+ const arrayPath = path . parent . value as Node ;
278
+ if ( arrayPath . elements && arrayPath . elements . length === 1 ) {
279
+ j ( path . parent . parent ) . remove ( ) ;
280
+ } else {
281
+ j ( path ) . remove ( ) ;
297
282
}
298
- ) ;
283
+ } ) ;
299
284
300
285
return rootPath ;
301
286
}
@@ -326,45 +311,39 @@ function createOrUpdatePluginByName(j: JSCodeshift, rootNodePath: Node, pluginNa
326
311
327
312
// If plugin declaration already exist
328
313
if ( pluginInstancePath . size ( ) ) {
329
- pluginInstancePath . forEach (
330
- ( path : Node ) : void => {
331
- // There are options we want to pass as argument
332
- if ( optionsProps ) {
333
- const args : Node [ ] = ( path . value as Node ) . arguments ;
334
- if ( args . length ) {
335
- // Plugin is called with object as arguments
336
- // we will merge those objects
337
- const currentProps : Node = j ( path )
338
- . find ( j . ObjectExpression )
339
- . get ( "properties" ) ;
340
-
341
- optionsProps . forEach (
342
- ( opt : Node ) : void => {
343
- // Search for same keys in the existing object
344
- const existingProps = j ( currentProps )
345
- . find ( j . Identifier )
346
- . filter ( ( p : Node ) : boolean => opt . key . value === ( p . value as Node ) . name ) ;
347
-
348
- if ( existingProps . size ( ) ) {
349
- // Replacing values for the same key
350
- existingProps . forEach (
351
- ( p : Node ) : void => {
352
- j ( p . parent ) . replaceWith ( opt ) ;
353
- }
354
- ) ;
355
- } else {
356
- // Adding new key:values
357
- ( currentProps . value as Node [ ] ) . push ( opt ) ;
358
- }
359
- }
360
- ) ;
361
- } else {
362
- // Plugin is called without arguments
363
- args . push ( j . objectExpression ( optionsProps ) ) ;
364
- }
314
+ pluginInstancePath . forEach ( ( path : Node ) : void => {
315
+ // There are options we want to pass as argument
316
+ if ( optionsProps ) {
317
+ const args : Node [ ] = ( path . value as Node ) . arguments ;
318
+ if ( args . length ) {
319
+ // Plugin is called with object as arguments
320
+ // we will merge those objects
321
+ const currentProps : Node = j ( path )
322
+ . find ( j . ObjectExpression )
323
+ . get ( "properties" ) ;
324
+
325
+ optionsProps . forEach ( ( opt : Node ) : void => {
326
+ // Search for same keys in the existing object
327
+ const existingProps = j ( currentProps )
328
+ . find ( j . Identifier )
329
+ . filter ( ( p : Node ) : boolean => opt . key . value === ( p . value as Node ) . name ) ;
330
+
331
+ if ( existingProps . size ( ) ) {
332
+ // Replacing values for the same key
333
+ existingProps . forEach ( ( p : Node ) : void => {
334
+ j ( p . parent ) . replaceWith ( opt ) ;
335
+ } ) ;
336
+ } else {
337
+ // Adding new key:values
338
+ ( currentProps . value as Node [ ] ) . push ( opt ) ;
339
+ }
340
+ } ) ;
341
+ } else {
342
+ // Plugin is called without arguments
343
+ args . push ( j . objectExpression ( optionsProps ) ) ;
365
344
}
366
345
}
367
- ) ;
346
+ } ) ;
368
347
} else {
369
348
let argumentsArray : Node [ ] = [ ] ;
370
349
if ( optionsProps ) {
@@ -458,23 +437,19 @@ function addProperty(j: JSCodeshift, p: Node, key: string, value: valueType, act
458
437
if ( safeTraverseAndGetType ( p ) === "ArrayExpression" ) {
459
438
arrExp = ( p . value as Node ) . value as Node ;
460
439
}
461
- value . forEach (
462
- ( val : valueType ) : void => {
463
- addProperty ( j , arrExp , null , val ) ;
464
- }
465
- ) ;
440
+ value . forEach ( ( val : valueType ) : void => {
441
+ addProperty ( j , arrExp , null , val ) ;
442
+ } ) ;
466
443
valForNode = arrExp ;
467
444
} else if ( typeof value === "object" && ! ( value . __paths || value instanceof RegExp ) ) {
468
445
let objectExp : Node = j . objectExpression ( [ ] ) ;
469
446
if ( safeTraverseAndGetType ( p ) === "ObjectExpression" ) {
470
447
objectExp = ( p . value as Node ) . value as Node ;
471
448
}
472
449
// object -> loop through it
473
- Object . keys ( value ) . forEach (
474
- ( prop : string ) : void => {
475
- addProperty ( j , objectExp , prop , value [ prop ] ) ;
476
- }
477
- ) ;
450
+ Object . keys ( value ) . forEach ( ( prop : string ) : void => {
451
+ addProperty ( j , objectExp , prop , value [ prop ] ) ;
452
+ } ) ;
478
453
valForNode = objectExp ;
479
454
} else {
480
455
valForNode = createIdentifierOrLiteral ( j , value ) ;
@@ -527,11 +502,9 @@ function removeProperty(j: JSCodeshift, ast: Node, key: string, value: valueType
527
502
value : value . rules [ 0 ] . loader
528
503
}
529
504
} )
530
- . forEach (
531
- ( p : Node ) : void => {
532
- j ( p . parent ) . remove ( ) ;
533
- }
534
- ) ;
505
+ . forEach ( ( p : Node ) : void => {
506
+ j ( p . parent ) . remove ( ) ;
507
+ } ) ;
535
508
}
536
509
}
537
510
@@ -541,14 +514,12 @@ function removeProperty(j: JSCodeshift, ast: Node, key: string, value: valueType
541
514
. find ( j . Literal , {
542
515
value : value [ 0 ]
543
516
} )
544
- . forEach (
545
- ( p : Node ) : void => {
546
- const configKey = safeTraverse ( p , [ "parent" , "parent" , "node" , "key" , "name" ] ) ;
547
- if ( configKey === key ) {
548
- j ( p ) . remove ( ) ;
549
- }
517
+ . forEach ( ( p : Node ) : void => {
518
+ const configKey = safeTraverse ( p , [ "parent" , "parent" , "node" , "key" , "name" ] ) ;
519
+ if ( configKey === key ) {
520
+ j ( p ) . remove ( ) ;
550
521
}
551
- ) ;
522
+ } ) ;
552
523
}
553
524
554
525
// value => literal string / boolean / nested object
@@ -569,11 +540,9 @@ function removeProperty(j: JSCodeshift, ast: Node, key: string, value: valueType
569
540
type : "Identifier"
570
541
}
571
542
} )
572
- . forEach (
573
- ( p : Node ) : void => {
574
- j ( p ) . remove ( ) ;
575
- }
576
- ) ;
543
+ . forEach ( ( p : Node ) : void => {
544
+ j ( p ) . remove ( ) ;
545
+ } ) ;
577
546
}
578
547
579
548
/**
@@ -591,16 +560,14 @@ function removeProperty(j: JSCodeshift, ast: Node, key: string, value: valueType
591
560
// eslint-disable-next-line @typescript-eslint/no-unused-vars
592
561
function parseTopScope ( j : JSCodeshift , ast : Node , value : string [ ] , action : string ) : boolean | Node {
593
562
function createTopScopeProperty ( p : Node ) : boolean {
594
- value . forEach (
595
- ( n : string ) : void => {
596
- if (
597
- ! ( p . value as Node ) . body [ 0 ] . declarations ||
598
- n . indexOf ( ( p . value as Node ) . body [ 0 ] . declarations [ 0 ] . id . name ) <= 0
599
- ) {
600
- ( p . value as Node ) . body . splice ( - 1 , 0 , n ) ;
601
- }
563
+ value . forEach ( ( n : string ) : void => {
564
+ if (
565
+ ! ( p . value as Node ) . body [ 0 ] . declarations ||
566
+ n . indexOf ( ( p . value as Node ) . body [ 0 ] . declarations [ 0 ] . id . name ) <= 0
567
+ ) {
568
+ ( p . value as Node ) . body . splice ( - 1 , 0 , n ) ;
602
569
}
603
- ) ;
570
+ } ) ;
604
571
return false ; // TODO: debug later
605
572
}
606
573
if ( value ) {
@@ -658,17 +625,15 @@ function parseMerge(j: JSCodeshift, ast: Node, value: string[], action: string):
658
625
`Both parameters should be strings. recieved ${ typeof configIdentifier } , ${ typeof configPath } `
659
626
) ;
660
627
}
661
- ast . find ( j . Program ) . forEach (
662
- ( p : Node ) : void => {
663
- if ( ! isImportPresent ( j , ast , "webpack-merge" ) ) {
664
- ( p . value as Node ) . body . splice ( - 1 , 0 , `const merge = require('webpack-merge')` ) ;
665
- }
628
+ ast . find ( j . Program ) . forEach ( ( p : Node ) : void => {
629
+ if ( ! isImportPresent ( j , ast , "webpack-merge" ) ) {
630
+ ( p . value as Node ) . body . splice ( - 1 , 0 , `const merge = require('webpack-merge')` ) ;
631
+ }
666
632
667
- if ( ! isImportPresent ( j , ast , configPath ) ) {
668
- ( p . value as Node ) . body . splice ( - 1 , 0 , `const ${ configIdentifier } = require('${ configPath } ')` ) ;
669
- }
633
+ if ( ! isImportPresent ( j , ast , configPath ) ) {
634
+ ( p . value as Node ) . body . splice ( - 1 , 0 , `const ${ configIdentifier } = require('${ configPath } ')` ) ;
670
635
}
671
- ) ;
636
+ } ) ;
672
637
}
673
638
674
639
if ( value ) {
0 commit comments