@@ -2,20 +2,18 @@ import core, { ASTPath, ImportDeclaration, ImportSpecifier } from 'jscodeshift';
22
33type Nullable < T > = T | null ;
44
5- const elevateComponentToDefault = ( pkg : string , innerElementName : string ) => (
6- j : core . JSCodeshift ,
7- root : any ,
8- ) => {
9- const existingAlias : Nullable < string > =
10- root
11- . find ( j . ImportDeclaration )
12- . filter (
13- ( path : ASTPath < ImportDeclaration > ) => path . node . source . value === pkg ,
14- )
15- . find ( j . ImportSpecifier )
16- . nodes ( )
17- . map (
18- ( specifier : ImportSpecifier ) : Nullable < string > => {
5+ const elevateComponentToDefault =
6+ ( pkg : string , innerElementName : string ) =>
7+ ( j : core . JSCodeshift , root : any ) => {
8+ const existingAlias : Nullable < string > =
9+ root
10+ . find ( j . ImportDeclaration )
11+ . filter (
12+ ( path : ASTPath < ImportDeclaration > ) => path . node . source . value === pkg ,
13+ )
14+ . find ( j . ImportSpecifier )
15+ . nodes ( )
16+ . map ( ( specifier : ImportSpecifier ) : Nullable < string > => {
1917 if ( innerElementName !== specifier . imported . name ) {
2018 return null ;
2119 }
@@ -25,42 +23,42 @@ const elevateComponentToDefault = (pkg: string, innerElementName: string) => (
2523 }
2624
2725 return null ;
28- } ,
29- )
30- . filter ( Boolean ) [ 0 ] || null ;
26+ } )
27+ . filter ( Boolean ) [ 0 ] || null ;
3128
32- root
33- . find ( j . ImportDeclaration )
34- . filter (
35- ( path : ASTPath < ImportDeclaration > ) => path . node . source . value === pkg ,
36- )
37- . forEach ( ( path : ASTPath < ImportDeclaration > ) => {
38- const defaultSpecifier = ( path . value . specifiers || [ ] ) . filter (
39- specifier => specifier . type === 'ImportDefaultSpecifier' ,
40- ) ;
29+ root
30+ . find ( j . ImportDeclaration )
31+ . filter (
32+ ( path : ASTPath < ImportDeclaration > ) => path . node . source . value === pkg ,
33+ )
34+ . forEach ( ( path : ASTPath < ImportDeclaration > ) => {
35+ const defaultSpecifier = ( path . value . specifiers || [ ] ) . filter (
36+ specifier => specifier . type === 'ImportDefaultSpecifier' ,
37+ ) ;
4138
42- let otherSpecifier = ( path . value . specifiers || [ ] ) . filter (
43- specifier => specifier . type === 'ImportSpecifier' ,
44- ) ;
39+ const otherSpecifier = ( path . value . specifiers || [ ] ) . filter (
40+ specifier => specifier . type === 'ImportSpecifier' ,
41+ ) ;
4542
46- if ( defaultSpecifier . length > 0 ) {
47- return ;
48- }
43+ if ( defaultSpecifier . length > 0 ) {
44+ return ;
45+ }
4946
50- const ds = otherSpecifier . find ( s =>
51- [ innerElementName , existingAlias ] . includes ( s . local ?. name || null ) ,
52- ) ;
53- const ni = otherSpecifier . filter (
54- s => ! [ innerElementName , existingAlias ] . includes ( s . local ?. name || null ) ,
55- ) ;
56- const declaration = j . importDeclaration (
57- [ j . importDefaultSpecifier ( ds && ds . local ) , ...ni ] ,
58- j . literal ( pkg ) ,
59- ) ;
47+ const ds = otherSpecifier . find ( s =>
48+ [ innerElementName , existingAlias ] . includes ( s . local ?. name || null ) ,
49+ ) ;
50+ const ni = otherSpecifier . filter (
51+ s =>
52+ ! [ innerElementName , existingAlias ] . includes ( s . local ?. name || null ) ,
53+ ) ;
54+ const declaration = j . importDeclaration (
55+ [ j . importDefaultSpecifier ( ds && ds . local ) , ...ni ] ,
56+ j . literal ( pkg ) ,
57+ ) ;
6058
61- j ( path ) . replaceWith ( declaration ) ;
62- } ) ;
63- } ;
59+ j ( path ) . replaceWith ( declaration ) ;
60+ } ) ;
61+ } ;
6462
6563const elevateStatelessToDefault = elevateComponentToDefault (
6664 '@atlaskit/breadcrumbs' ,
0 commit comments