@@ -17,9 +17,13 @@ import {
1717 objectExpression ,
1818 V8IntrinsicIdentifier ,
1919 ObjectProperty ,
20- VariableDeclarator
20+ VariableDeclarator ,
21+ MemberExpression ,
22+ isMemberExpression ,
23+ isNumericLiteral ,
24+ isStringLiteral ,
25+ Identifier
2126} from '@babel/types'
22- import { parse } from '@babel/parser'
2327import generate from '@babel/generator'
2428
2529import { getCoreExports } from './imports'
@@ -37,11 +41,36 @@ import { _babelInterop } from './util'
3741import { parseCode } from './parser'
3842import { createHumanLog } from '../core/errors'
3943
44+ export function getCalleeAndAccessorKey ( node : MemberExpression | Identifier ) {
45+ if ( ! isMemberExpression ( node ) ) {
46+ return {
47+ callee : node ,
48+ accessorKey : undefined
49+ }
50+ }
51+
52+ // @ts -expect-error
53+ const callee : MemberExpression | Identifier = node . object
54+
55+ let accessorKey = node . property
56+
57+ if ( ! node . computed ) {
58+ if ( isIdentifier ( node . property ) ) {
59+ accessorKey = stringLiteral ( node . property . name )
60+ }
61+ if ( isNumericLiteral ( node . property ) || isStringLiteral ( node . property ) ) {
62+ accessorKey = stringLiteral ( String ( node . property . value ) )
63+ }
64+ }
65+
66+ return { callee, accessorKey }
67+ }
68+
4069export function shouldBeWrappedUff ( path : NodePath ) {
4170 if (
4271 isCallExpression ( path . parent ) &&
4372 isIdentifier ( path . parent . callee ) &&
44- [ 'uff' ] . includes ( path . parent . callee . name )
73+ [ 'uff' , 'ufc' ] . includes ( path . parent . callee . name )
4574 ) {
4675 return false
4776 }
@@ -98,10 +127,6 @@ export function flytrapTransformUff(
98127 ? findIgnoredImports ( code , config . packageIgnores )
99128 : undefined
100129
101- // What gets transformed and captured is defined in the config.
102- // const shouldTransformFunctions = config?.captureDataFrom === "calls" ? false : true;
103- // const shouldTransformCalls = config?.captureDataFrom === 'functions' ? false : true;
104-
105130 try {
106131 _babelInterop ( babelTraverse ) ( ast , {
107132 ...( ! config ?. transformOptions ?. disableTransformation ?. includes ( 'arrow-function' ) && {
@@ -188,30 +213,40 @@ export function flytrapTransformUff(
188213 return
189214 }
190215
191- const functionCallName = extractFunctionCallName ( path . node )
216+ const fullFunctionCallName = _babelInterop ( generate ) ( {
217+ ...path . node ,
218+ arguments : [ ]
219+ } ) . code . replaceAll ( '()' , '' )
220+
221+ if ( fullFunctionCallName === 'this' || fullFunctionCallName . split ( '.' ) . at ( 0 ) === 'this' ) {
222+ return
223+ }
224+ const functionCallName = fullFunctionCallName . split ( '.' ) . at ( - 1 ) !
192225 const scopes = extractCurrentScope ( path )
193226 const functionCallId = extractFunctionCallId ( path , filePath , functionCallName , scopes )
194- const useFunctionName = isAwaitExpression ( path . parent )
195- ? 'useFlytrapCallAsync'
196- : 'useFlytrapCall'
197-
198- function transformCallee ( callee : V8IntrinsicIdentifier | Expression ) {
199- if ( callee . type === 'MemberExpression' ) {
200- return callee . object
201- }
202- return callee
227+ const useFunctionName = isAwaitExpression ( path . parent ) ? 'ufc' : 'ufc'
228+
229+ // @ts -ignore
230+ const { callee, accessorKey } = getCalleeAndAccessorKey ( path . node . callee )
231+
232+ if ( ! callee ) {
233+ throw new Error ( 'Callee is undefined. CODE: ' + generate ( path . node ) . code )
203234 }
204235
205236 const newNode = callExpression ( identifier ( useFunctionName ) , [
206- // @ts -ignore
207- transformCallee ( path . node . callee ) ,
237+ callee ,
208238 objectExpression ( [
209239 objectProperty ( identifier ( 'id' ) , stringLiteral ( functionCallId ) ) ,
210240 // @ts -ignore
211241 objectProperty ( identifier ( 'args' ) , arrayExpression ( path . node . arguments ) ) ,
212- objectProperty ( identifier ( 'name' ) , stringLiteral ( functionCallName ) )
242+ objectProperty (
243+ identifier ( 'name' ) ,
244+ // @ts -expect-error
245+ accessorKey ? accessorKey : stringLiteral ( functionCallName )
246+ )
213247 ] )
214248 ] )
249+
215250 path . replaceWith ( newNode )
216251 }
217252 } )
0 commit comments