@@ -92,16 +92,171 @@ module.exports = function (babel) {
9292 ( binding . path . parent &&
9393 t . isImportDeclaration ( binding . path . parent ) )
9494 ) {
95- // Get the arguments as a string
96- const argsString = arg . arguments
97- . map ( ( argNode ) =>
98- path . hub . file . code . slice ( argNode . start , argNode . end )
99- )
100- . join ( ', ' ) ;
101-
102- path . node . arguments [ 0 ] = t . stringLiteral (
103- `${ callee . name } (${ argsString } )`
104- ) ;
95+ // Check arguments for runtime values
96+ const args = arg . arguments . map ( ( argNode ) => {
97+ if ( t . isIdentifier ( argNode ) ) {
98+ const argBinding = path . scope . getBinding ( argNode . name ) ;
99+ if ( argBinding ) {
100+ return {
101+ value : argNode . name ,
102+ isRuntime : true ,
103+ } ;
104+ }
105+ } else if ( t . isBinaryExpression ( argNode ) ) {
106+ // For binary expressions, we need to handle the entire expression as one unit
107+ const hasRuntimeValue = ( node ) => {
108+ if ( t . isIdentifier ( node ) ) {
109+ return path . scope . getBinding ( node . name ) != null ;
110+ }
111+ if ( t . isBinaryExpression ( node ) ) {
112+ return (
113+ hasRuntimeValue ( node . left ) ||
114+ hasRuntimeValue ( node . right )
115+ ) ;
116+ }
117+ return false ;
118+ } ;
119+
120+ const isRuntime = hasRuntimeValue ( argNode ) ;
121+ return {
122+ value : path . hub . file . code . slice (
123+ argNode . start ,
124+ argNode . end
125+ ) ,
126+ isRuntime,
127+ isBinaryExpression : true ,
128+ } ;
129+ } else if ( t . isObjectExpression ( argNode ) ) {
130+ const properties = argNode . properties
131+ . map ( ( prop ) => {
132+ if ( t . isObjectProperty ( prop ) ) {
133+ if ( t . isIdentifier ( prop . value ) ) {
134+ const objectBinding = path . scope . getBinding (
135+ prop . value . name
136+ ) ;
137+ if ( objectBinding ) {
138+ return {
139+ key : prop . key . name ,
140+ value : prop . value . name ,
141+ isRuntime : true ,
142+ } ;
143+ }
144+ }
145+ return {
146+ key : prop . key . name ,
147+ value : path . hub . file . code . slice (
148+ prop . value . start ,
149+ prop . value . end
150+ ) ,
151+ isRuntime : false ,
152+ } ;
153+ }
154+ return null ;
155+ } )
156+ . filter ( Boolean ) ;
157+
158+ const hasRuntimeProps = properties . some ( ( p ) => p . isRuntime ) ;
159+ if ( hasRuntimeProps ) {
160+ return {
161+ properties,
162+ isObject : true ,
163+ isRuntime : true ,
164+ } ;
165+ }
166+ }
167+ return {
168+ value : path . hub . file . code . slice ( argNode . start , argNode . end ) ,
169+ isRuntime : false ,
170+ } ;
171+ } ) ;
172+
173+ if ( args . some ( ( runtimeArg ) => runtimeArg . isRuntime ) ) {
174+ // Create template elements for each argument
175+ const quasis = [ ] ;
176+ const expressions = [ ] ;
177+
178+ // Add the function name and opening parenthesis
179+ quasis . push (
180+ t . templateElement (
181+ { raw : `${ callee . name } (` , cooked : `${ callee . name } (` } ,
182+ false
183+ )
184+ ) ;
185+
186+ // Add each argument
187+ args . forEach ( ( objectArg , index ) => {
188+ if ( objectArg . isObject ) {
189+ const prev = quasis . pop ( ) ;
190+ let objStr = prev . value . raw + '{ ' ;
191+
192+ arg . properties . forEach ( ( prop , propIndex ) => {
193+ if ( prop . isRuntime ) {
194+ objStr += `${ prop . key } : ` ;
195+ quasis . push (
196+ t . templateElement (
197+ { raw : objStr , cooked : objStr } ,
198+ false
199+ )
200+ ) ;
201+ expressions . push ( t . identifier ( prop . value ) ) ;
202+ objStr =
203+ propIndex < arg . properties . length - 1 ? ', ' : '' ;
204+ } else {
205+ objStr += `${ prop . key } : ${ prop . value } ${ propIndex < arg . properties . length - 1 ? ', ' : '' } ` ;
206+ }
207+ } ) ;
208+
209+ objStr += ' }' + ( index === args . length - 1 ? ')' : ', ' ) ;
210+ quasis . push (
211+ t . templateElement (
212+ { raw : objStr , cooked : objStr } ,
213+ index === args . length - 1
214+ )
215+ ) ;
216+ } else if ( arg . isRuntime ) {
217+ if ( arg . isBinaryExpression ) {
218+ expressions . push ( t . identifier ( arg . value ) ) ;
219+ } else {
220+ expressions . push ( t . identifier ( arg . value ) ) ;
221+ }
222+ const separator = index === args . length - 1 ? ')' : ', ' ;
223+ quasis . push (
224+ t . templateElement (
225+ {
226+ raw : separator ,
227+ cooked : separator ,
228+ } ,
229+ index === args . length - 1
230+ )
231+ ) ;
232+ } else {
233+ const prev = quasis . pop ( ) ;
234+ const separator = index === args . length - 1 ? ')' : ', ' ;
235+ const newRaw = prev . value . raw + arg . value + separator ;
236+ const newCooked =
237+ prev . value . cooked + arg . value + separator ;
238+ quasis . push (
239+ t . templateElement (
240+ { raw : newRaw , cooked : newCooked } ,
241+ index === args . length - 1
242+ )
243+ ) ;
244+ }
245+ } ) ;
246+
247+ path . node . arguments [ 0 ] = t . templateLiteral (
248+ quasis ,
249+ expressions
250+ ) ;
251+ } else {
252+ // If no runtime values, use string literal
253+ const argsString = args
254+ . map ( ( stringArg ) => stringArg . value )
255+ . join ( ', ' ) ;
256+ path . node . arguments [ 0 ] = t . stringLiteral (
257+ `${ callee . name } (${ argsString } )`
258+ ) ;
259+ }
105260 return ;
106261 }
107262
@@ -205,8 +360,8 @@ module.exports = function (babel) {
205360 ) ;
206361
207362 // Add each argument
208- args . forEach ( ( arg , index ) => {
209- if ( arg . isBinaryExpression ) {
363+ args . forEach ( ( expressionArg , index ) => {
364+ if ( expressionArg . isBinaryExpression ) {
210365 const { left, operator, right } = arg . value ;
211366 if ( left . isRuntime ) {
212367 expressions . push ( t . identifier ( left . value ) ) ;
0 commit comments