1
- import type { API , Collection , FileInfo , JSCodeshift , Options } from 'jscodeshift' ;
1
+ import type { API , ASTPath , Collection , FileInfo , JSCodeshift , JSXElement , Options } from 'jscodeshift' ;
2
2
3
3
const config = require ( './codemodConfig.json' ) ;
4
4
@@ -45,6 +45,32 @@ function addWebComponentsReactImport(j: JSCodeshift, root: Collection, importNam
45
45
}
46
46
}
47
47
48
+ function extractValueFromProp (
49
+ j : JSCodeshift ,
50
+ el : ASTPath < JSXElement > ,
51
+ componentName : string ,
52
+ propName : string
53
+ ) : string | null {
54
+ const prop = j ( el ) . find ( j . JSXAttribute , { name : { name : propName } } ) ;
55
+
56
+ if ( prop . size ( ) ) {
57
+ const s = prop . get ( ) ;
58
+ const stringLiteral = prop . find ( j . StringLiteral ) ;
59
+ const numericLiteral = prop . find ( j . NumericLiteral ) ;
60
+ prop . remove ( ) ;
61
+
62
+ if ( stringLiteral . size ( ) > 0 ) {
63
+ return stringLiteral . get ( ) . value . value ;
64
+ } else if ( numericLiteral . size ( ) > 0 ) {
65
+ return numericLiteral . get ( ) . value . value ;
66
+ } else {
67
+ console . warn ( `Unable to read value for prop '${ propName } ' (${ componentName } ). Please check the code manually.` ) ;
68
+ return null ;
69
+ }
70
+ }
71
+ return null ;
72
+ }
73
+
48
74
export default function transform ( file : FileInfo , api : API , options ?: Options ) : string | undefined {
49
75
const j = api . jscodeshift ;
50
76
const root = j ( file . source ) ;
@@ -82,52 +108,21 @@ export default function transform(file: FileInfo, api: API, options?: Options):
82
108
83
109
if ( componentName === 'Carousel' ) {
84
110
jsxElements . forEach ( ( el ) => {
85
- const itemsPerPageS = j ( el ) . find ( j . JSXAttribute , { name : { name : 'itemsPerPageS' } } ) ;
86
- const itemsPerPageM = j ( el ) . find ( j . JSXAttribute , { name : { name : 'itemsPerPageM' } } ) ;
87
- const itemsPerPageL = j ( el ) . find ( j . JSXAttribute , { name : { name : 'itemsPerPageL' } } ) ;
88
-
89
- const sizeValues : string [ ] = [ ] ;
90
-
91
- if ( itemsPerPageS . size ( ) ) {
92
- const s = itemsPerPageS . get ( ) ;
93
- const stringLiteral = itemsPerPageS . find ( j . StringLiteral ) ;
94
- const numericLiteral = itemsPerPageS . find ( j . NumericLiteral ) ;
95
-
96
- if ( stringLiteral . size ( ) > 0 ) {
97
- sizeValues . push ( `S${ stringLiteral . get ( ) . value . value } ` ) ;
98
- } else if ( numericLiteral . size ( ) > 0 ) {
99
- sizeValues . push ( `S${ numericLiteral . get ( ) . value . value } ` ) ;
100
- } else {
101
- console . warn ( `Unable to read value for prop 'itemsPerPageS' (Carousel). Please check the code manually.` ) ;
102
- }
103
- }
104
-
105
- if ( itemsPerPageM . size ( ) ) {
106
- const stringLiteral = itemsPerPageM . find ( j . StringLiteral ) ;
107
- const numericLiteral = itemsPerPageM . find ( j . NumericLiteral ) ;
108
- if ( stringLiteral . size ( ) > 0 ) {
109
- sizeValues . push ( `M${ stringLiteral . get ( ) . value . value } ` ) ;
110
- } else if ( numericLiteral . size ( ) > 0 ) {
111
- sizeValues . push ( `M${ numericLiteral . get ( ) . value . value } ` ) ;
112
- } else {
113
- console . warn ( `Unable to read value for prop 'itemsPerPageM' (Carousel). Please check the code manually.` ) ;
114
- }
115
- }
116
-
117
- if ( itemsPerPageL . size ( ) ) {
118
- const stringLiteral = itemsPerPageL . find ( j . StringLiteral ) ;
119
- const numericLiteral = itemsPerPageL . find ( j . NumericLiteral ) ;
120
- if ( stringLiteral . size ( ) > 0 ) {
121
- sizeValues . push ( `L${ stringLiteral . get ( ) . value . value } ` ) ;
122
- } else if ( numericLiteral . size ( ) > 0 ) {
123
- sizeValues . push ( `L${ numericLiteral . get ( ) . value . value } ` ) ;
124
- } else {
125
- console . warn ( `Unable to read value for prop 'itemsPerPageL' (Carousel). Please check the code manually.` ) ;
126
- }
127
- }
111
+ const sizeValues : string [ ] = [
112
+ [ 'S' , 'itemsPerPageS' ] ,
113
+ [ 'M' , 'itemsPerPageM' ] ,
114
+ [ 'L' , 'itemsPerPageL' ]
115
+ ]
116
+ . map ( ( [ key , prop ] ) => {
117
+ const val = extractValueFromProp ( j , el , componentName , prop ) ;
118
+ if ( val != null ) {
119
+ return `${ key } ${ val } ` ;
120
+ }
121
+ return '' ;
122
+ } )
123
+ . filter ( ( val ) => val . length > 0 ) ;
128
124
129
125
if ( sizeValues . length > 0 ) {
130
- [ itemsPerPageS , itemsPerPageM , itemsPerPageL ] . forEach ( ( e ) => e . remove ( ) ) ;
131
126
j ( el )
132
127
. find ( j . JSXOpeningElement )
133
128
. get ( )
@@ -139,6 +134,88 @@ export default function transform(file: FileInfo, api: API, options?: Options):
139
134
} ) ;
140
135
}
141
136
137
+ if ( componentName === 'Form' ) {
138
+ jsxElements . forEach ( ( el ) => {
139
+ const labelSpan : string [ ] = [
140
+ [ 'S' , 'labelSpanS' ] ,
141
+ [ 'M' , 'labelSpanM' ] ,
142
+ [ 'L' , 'labelSpanL' ] ,
143
+ [ 'XL' , 'labelSpanXL' ]
144
+ ]
145
+ . map ( ( [ key , prop ] ) => {
146
+ const val = extractValueFromProp ( j , el , componentName , prop ) ;
147
+ if ( val != null ) {
148
+ return `${ key } ${ val } ` ;
149
+ }
150
+ return '' ;
151
+ } )
152
+ . filter ( ( val ) => val . length > 0 ) ;
153
+
154
+ if ( labelSpan . length > 0 ) {
155
+ j ( el )
156
+ . find ( j . JSXOpeningElement )
157
+ . get ( )
158
+ . value . attributes . push ( j . jsxAttribute ( j . jsxIdentifier ( 'labelSpan' ) , j . stringLiteral ( labelSpan . join ( ' ' ) ) ) ) ;
159
+ isDirty = true ;
160
+ }
161
+
162
+ const layout : string [ ] = [
163
+ [ 'S' , 'columnsS' ] ,
164
+ [ 'M' , 'columnsM' ] ,
165
+ [ 'L' , 'columnsL' ] ,
166
+ [ 'XL' , 'columnsXL' ]
167
+ ]
168
+ . map ( ( [ key , prop ] ) => {
169
+ const val = extractValueFromProp ( j , el , componentName , prop ) ;
170
+ if ( val != null ) {
171
+ return `${ key } ${ val } ` ;
172
+ }
173
+ return '' ;
174
+ } )
175
+ . filter ( ( val ) => val . length > 0 ) ;
176
+
177
+ if ( layout . length > 0 ) {
178
+ j ( el )
179
+ . find ( j . JSXOpeningElement )
180
+ . get ( )
181
+ . value . attributes . push ( j . jsxAttribute ( j . jsxIdentifier ( 'layout' ) , j . stringLiteral ( layout . join ( ' ' ) ) ) ) ;
182
+ isDirty = true ;
183
+ }
184
+ } ) ;
185
+ }
186
+
187
+ if ( componentName === 'FormItem' ) {
188
+ jsxElements . forEach ( ( el ) => {
189
+ const label = j ( el ) . find ( j . JSXAttribute , { name : { name : 'label' } } ) ;
190
+ if ( label . size ( ) ) {
191
+ const labelNode = label . get ( ) ;
192
+ let value : string | undefined ;
193
+ if ( labelNode . value . value . type === 'StringLiteral' ) {
194
+ value = labelNode . value . value . value ;
195
+ }
196
+ if (
197
+ labelNode . value . value . type === 'JSXExpressionContainer' &&
198
+ labelNode . value . value . expression . type === 'StringLiteral'
199
+ ) {
200
+ value = labelNode . value . value . expression . value ;
201
+ }
202
+
203
+ if ( value ) {
204
+ addWebComponentsReactImport ( j , root , 'Label' ) ;
205
+ const labelComponent = j . jsxElement (
206
+ j . jsxOpeningElement ( j . jsxIdentifier ( 'Label' ) , [ ] , false ) ,
207
+ j . jsxClosingElement ( j . jsxIdentifier ( 'Label' ) ) ,
208
+ [ j . jsxText ( value ) ]
209
+ ) ;
210
+ label . replaceWith (
211
+ j . jsxAttribute ( j . jsxIdentifier ( 'labelContent' ) , j . jsxExpressionContainer ( labelComponent ) )
212
+ ) ;
213
+ isDirty = true ;
214
+ }
215
+ }
216
+ } ) ;
217
+ }
218
+
142
219
if ( componentName === 'Icon' ) {
143
220
jsxElements . forEach ( ( el ) => {
144
221
const interactive = j ( el ) . find ( j . JSXAttribute , { name : { name : 'interactive' } } ) ;
0 commit comments