@@ -6,16 +6,11 @@ function isImportPresent(j: JSCodeshift, ast: Node, path: string): boolean {
66 throw new Error ( `path parameter should be string, recieved ${ typeof path } ` ) ;
77 }
88 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 ;
1712 }
18- ) ;
13+ } ) ;
1914 return importExists ;
2015}
2116
@@ -93,13 +88,11 @@ function pathsToMemberExpression(j: JSCodeshift, paths: string[]): Node {
9388 */
9489
9590function 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+ } ) ;
10396}
10497
10598/**
@@ -110,14 +103,12 @@ function findPluginsByName(j: JSCodeshift, node: Node, pluginNamesArray: string[
110103 */
111104
112105function 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 ( ) ;
119110 }
120- ) ;
111+ } ) ;
121112}
122113
123114/**
@@ -250,15 +241,11 @@ function addOrUpdateConfigObject(
250241 if ( propertyExists ) {
251242 rootNode . properties
252243 . 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+ } ) ;
262249 } else {
263250 rootNode . properties . push (
264251 j . objectProperty (
@@ -285,17 +272,15 @@ function findAndRemovePluginByName(j: JSCodeshift, node: Node, pluginName: strin
285272
286273 findPluginsByName ( j , node , [ pluginName ] )
287274 . 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 ( ) ;
297282 }
298- ) ;
283+ } ) ;
299284
300285 return rootPath ;
301286}
@@ -326,45 +311,39 @@ function createOrUpdatePluginByName(j: JSCodeshift, rootNodePath: Node, pluginNa
326311
327312 // If plugin declaration already exist
328313 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 ) ) ;
365344 }
366345 }
367- ) ;
346+ } ) ;
368347 } else {
369348 let argumentsArray : Node [ ] = [ ] ;
370349 if ( optionsProps ) {
@@ -458,23 +437,19 @@ function addProperty(j: JSCodeshift, p: Node, key: string, value: valueType, act
458437 if ( safeTraverseAndGetType ( p ) === "ArrayExpression" ) {
459438 arrExp = ( p . value as Node ) . value as Node ;
460439 }
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+ } ) ;
466443 valForNode = arrExp ;
467444 } else if ( typeof value === "object" && ! ( value . __paths || value instanceof RegExp ) ) {
468445 let objectExp : Node = j . objectExpression ( [ ] ) ;
469446 if ( safeTraverseAndGetType ( p ) === "ObjectExpression" ) {
470447 objectExp = ( p . value as Node ) . value as Node ;
471448 }
472449 // 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+ } ) ;
478453 valForNode = objectExp ;
479454 } else {
480455 valForNode = createIdentifierOrLiteral ( j , value ) ;
@@ -527,11 +502,9 @@ function removeProperty(j: JSCodeshift, ast: Node, key: string, value: valueType
527502 value : value . rules [ 0 ] . loader
528503 }
529504 } )
530- . forEach (
531- ( p : Node ) : void => {
532- j ( p . parent ) . remove ( ) ;
533- }
534- ) ;
505+ . forEach ( ( p : Node ) : void => {
506+ j ( p . parent ) . remove ( ) ;
507+ } ) ;
535508 }
536509 }
537510
@@ -541,14 +514,12 @@ function removeProperty(j: JSCodeshift, ast: Node, key: string, value: valueType
541514 . find ( j . Literal , {
542515 value : value [ 0 ]
543516 } )
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 ( ) ;
550521 }
551- ) ;
522+ } ) ;
552523 }
553524
554525 // value => literal string / boolean / nested object
@@ -569,11 +540,9 @@ function removeProperty(j: JSCodeshift, ast: Node, key: string, value: valueType
569540 type : "Identifier"
570541 }
571542 } )
572- . forEach (
573- ( p : Node ) : void => {
574- j ( p ) . remove ( ) ;
575- }
576- ) ;
543+ . forEach ( ( p : Node ) : void => {
544+ j ( p ) . remove ( ) ;
545+ } ) ;
577546}
578547
579548/**
@@ -591,16 +560,14 @@ function removeProperty(j: JSCodeshift, ast: Node, key: string, value: valueType
591560// eslint-disable-next-line @typescript-eslint/no-unused-vars
592561function parseTopScope ( j : JSCodeshift , ast : Node , value : string [ ] , action : string ) : boolean | Node {
593562 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 ) ;
602569 }
603- ) ;
570+ } ) ;
604571 return false ; // TODO: debug later
605572 }
606573 if ( value ) {
@@ -658,17 +625,15 @@ function parseMerge(j: JSCodeshift, ast: Node, value: string[], action: string):
658625 `Both parameters should be strings. recieved ${ typeof configIdentifier } , ${ typeof configPath } `
659626 ) ;
660627 }
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+ }
666632
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 } ')` ) ;
670635 }
671- ) ;
636+ } ) ;
672637 }
673638
674639 if ( value ) {
0 commit comments