@@ -12,48 +12,54 @@ export default function({ types: t }) {
1212 return ;
1313 }
1414 path . node . alreadyVisited = true ;
15-
15+
1616 const returnValue = path . node . argument ? path . node . argument : t . identifier ( 'undefined' ) ;
1717 const returnNode = callOnTrace ( 'return' , [ location ( path . node , state ) , returnValue ] ) ;
1818 path . node . argument = returnNode ;
1919 }
2020 }
2121
22- function callOnTrace ( methodName , args = [ ] ) {
23- return t . callExpression ( t . memberExpression ( t . identifier ( Trace . traceIdenifierName ) , t . identifier ( methodName ) ) ,
22+ function callOnTrace ( methodName , args = [ ] , shouldBeStatement = false ) {
23+ let call = t . callExpression ( t . memberExpression ( t . identifier ( Trace . traceIdenifierName ) , t . identifier ( methodName ) ) ,
2424 args ) ;
25+ if ( shouldBeStatement ) {
26+ call = t . expressionStatement ( call ) ;
27+ }
28+ return call ;
2529 }
26-
30+
2731 function location ( astNode , state ) {
2832 const filename = state . file . opts . filename ;
29-
30- const start = astNode . loc . start ;
33+
34+ const start = astNode . loc . start ;
3135 const end = astNode . loc . end ;
32-
36+
3337 const locationObject = {
34- filename,
35- startLine : start . line ,
36- startColumn : start . column ,
37- endLine : end . line ,
38+ filename,
39+ startLine : start . line ,
40+ startColumn : start . column ,
41+ endLine : end . line ,
3842 endColumn : end . column
3943 }
40-
44+
4145 const id = Trace . register ( locationObject ) ;
42-
46+
4347 return t . numericLiteral ( id ) ;
4448 }
4549
4650 function modifyFunction ( name , path , state ) {
4751 const body = path . get ( 'body' ) ;
48- body . unshiftContainer ( 'body' , t . expressionStatement ( callOnTrace ( 'enterFunction' , [ location ( path . node , state ) , t . stringLiteral ( name ) ] ) ) ) ;
52+ body . unshiftContainer ( 'body' , t . expressionStatement ( callOnTrace ( 'enterFunction' , [ location ( path . node , state ) , t
53+ . stringLiteral ( name )
54+ ] ) ) ) ;
4955 body . pushContainer ( 'body' , t . expressionStatement ( callOnTrace ( 'leave' , [ location ( path . node , state ) ] ) ) ) ;
5056 path . traverse ( returnVisitor , state ) ;
5157 }
5258
5359 function resolveName ( callee ) {
54- if ( callee . type === 'MemberExpression' ) {
60+ if ( callee . type === 'MemberExpression' ) {
5561 return resolveName ( callee . object ) + `.${ callee . property . name } ` ;
56- } else if ( callee . type === 'CallExpression' ) {
62+ } else if ( callee . type === 'CallExpression' ) {
5763 return resolveName ( callee . callee ) ;
5864 } else {
5965 return callee . name ;
@@ -73,16 +79,47 @@ export default function({ types: t }) {
7379 name : 'tracer' ,
7480 visitor : {
7581 Program ( path ) {
76- // wrapAST(path.node, {notify(){console.log(...arguments)}})
82+ // wrapAST(path.node, {notify(){console.log(...arguments)}})
7783 } ,
7884 CallExpression ( path ) {
7985 if ( path . node . alreadyVisited || path . isGenerated ( ) ) {
8086 return ;
8187 }
82- const name = nameFromCallExpression ( path ) ;
88+
89+
8390 path . node . alreadyVisited = true ;
84- const log = callOnTrace ( 'aboutToEnter' , [ location ( path . node , this ) , t . stringLiteral ( name ) ] ) ;
85- path . insertBefore ( log )
91+ let callee = path . get ( 'callee' ) ;
92+ let name ;
93+
94+ if ( t . isMemberExpression ( callee ) ) {
95+ callee . node . computed = true ;
96+ callee = path . get ( 'callee.property' ) ;
97+
98+ name = callee . node . name || 'anonymous function' ;
99+
100+ const aboutToEnter = callOnTrace ( 'aboutToEnter' ,
101+ [
102+ location ( callee . node , this ) ,
103+ t . stringLiteral ( name )
104+ ] ) ;
105+ callee . replaceWith ( t . stringLiteral ( name ) ) ;
106+ callee . insertBefore ( aboutToEnter ) ;
107+ return ;
108+ } else if ( t . isFunctionExpression ( callee ) ) {
109+ name = callee . node . id . name || 'anonymous function' ;
110+ } else { // identifier or anonymous
111+ name = callee . node . name || 'anonymous function' ;
112+ }
113+
114+ const aboutToEnter = callOnTrace ( 'aboutToEnter' ,
115+ [
116+ location ( callee . node , this ) ,
117+ t . stringLiteral ( name )
118+ ] ) ;
119+
120+ callee . insertBefore ( aboutToEnter ) ;
121+
122+
86123 } ,
87124 ArrowFunctionExpression ( path ) {
88125
@@ -101,42 +138,44 @@ export default function({ types: t }) {
101138 path . insertAfter ( callOnTrace ( 'endLoop' , [ location ( path . node , this ) ] ) )
102139 } ,
103140 ForStatement ( path ) {
104- path . get ( 'body' ) . unshiftContainer ( 'body' , callOnTrace ( 'nextLoopIteration' , [ location ( path . node , this ) , ...path . node . init . declarations . map ( declaration => declaration . id ) ] ) ) ;
141+ path . get ( 'body' ) . unshiftContainer ( 'body' , callOnTrace ( 'nextLoopIteration' , [ location ( path . node , this ) ,
142+ ...path . node . init . declarations . map ( declaration => declaration . id )
143+ ] ) ) ;
105144 } ,
106145 ForOfStatement ( path ) {
107-
146+
108147 } ,
109148 ForInStatement ( path ) {
110-
149+
111150 } ,
112- Conditional ( path ) {
113- if ( path . node . alreadyVisited ) {
151+ Conditional ( path ) {
152+ if ( path . node . alreadyVisited ) {
114153 return ;
115154 }
116-
155+
117156 path . node . alreadyVisited = true ;
118-
157+
119158 path . node . test = callOnTrace ( 'conditionTest' , [
120- location ( path . node . test , this ) ,
159+ location ( path . node . test , this ) ,
121160 path . node . test
122161 ] ) ;
123-
162+
124163 // do not log else ifs
125- if ( path . parent . type !== 'IfStatement' ) {
164+ if ( path . parent . type !== 'IfStatement' ) {
126165 path . insertBefore ( callOnTrace ( 'beginCondition' , [
127- location ( path . node , this ) ,
166+ location ( path . node , this ) ,
128167 t . stringLiteral ( path . type )
129168 ] ) ) ;
130169 path . insertAfter ( callOnTrace ( 'endCondition' , [ location ( path . node , this ) ] ) ) ;
131- }
170+ }
132171 } ,
133-
172+
134173 AssignmentExpression ( path ) {
135- if ( path . isGenerated ( ) ) {
174+ if ( path . isGenerated ( ) ) {
136175 return ;
137176 }
138177 path . node . right = callOnTrace ( 'assignment' , [
139- location ( path . node , this ) ,
178+ location ( path . node , this ) ,
140179 t . stringLiteral ( resolveName ( path . node . left ) ) , path . node . right
141180 ] ) ;
142181 }
0 commit comments