@@ -42,15 +42,15 @@ class ASTChangeEvent {
4242 }
4343
4444 getNode ( id , astNode ) {
45-
45+
4646
4747 if ( astNode . type ) {
4848 const isSearchedNode = value => value && value . traceID !== undefined && value . traceID . nodeID === id . nodeID ;
49-
50- if ( isSearchedNode ( astNode ) ) {
49+
50+ if ( isSearchedNode ( astNode ) ) {
5151 return astNode ;
5252 }
53-
53+
5454 const keys = Object . keys ( astNode ) . filter ( key => ! excludedProperties . includes ( key ) ) ;
5555 for ( const key of keys ) {
5656 const value = astNode [ key ] ;
@@ -59,7 +59,7 @@ class ASTChangeEvent {
5959 if ( Array . isArray ( value ) ) {
6060 for ( const entry of value ) {
6161 const node = this . getNode ( id , entry )
62- if ( isSearchedNode ( node ) ) {
62+ if ( isSearchedNode ( node ) ) {
6363 return node ;
6464 }
6565 }
@@ -78,7 +78,7 @@ class ASTChangeEvent {
7878 case 'object' :
7979 // assume it is an astNode
8080 const node = this . getNode ( id , value )
81- if ( isSearchedNode ( node ) ) {
81+ if ( isSearchedNode ( node ) ) {
8282 return node ;
8383 }
8484 // fallthrough as we want to know if a node is replaced
@@ -93,7 +93,7 @@ class ASTChangeEvent {
9393 let astNode = this . getNode ( this . objectID , ast ) ;
9494 if ( this . arrayProperty ) {
9595 astNode = astNode [ this . arrayProperty ] ;
96- }
96+ }
9797 astNode [ this . propertyName ] = this . newValue ;
9898 }
9999
@@ -170,10 +170,11 @@ class Trace {
170170 constructor ( ) {
171171 this . _log = [ ] ;
172172 this . counter = 0 ;
173- }
174173
175- register ( name ) {
176- console . log ( name ) ;
174+ this . locations = [ ] ;
175+ this . filenames = [ ] ;
176+ this . fileRegistry = new Map ( ) ;
177+ this . astNodeRegistry = new Map ( ) ;
177178 }
178179
179180 log ( event ) {
@@ -190,6 +191,73 @@ class Trace {
190191 nodeID : this . counter ++
191192 }
192193 }
194+
195+ serialize ( ) {
196+ const serialized = { } ;
197+
198+ serialized . _log = JSON . stringify ( this . _log ) ;
199+ serialized . locations = JSON . stringify ( this . locations ) ;
200+ serialized . filenames = JSON . stringify ( this . filenames ) ;
201+ serialized . astNodeRegistry = JSON . stringify ( [ ...this . astNodeRegistry ] ) ;
202+
203+ return serialized ;
204+ }
205+
206+ static deserializedFrom ( obj ) {
207+ const trace = new Trace ( ) ;
208+
209+ trace . _log = JSON . parse ( obj . _log ) ;
210+ trace . locations = JSON . parse ( obj . locations ) ;
211+ trace . filenames = JSON . parse ( obj . filenames ) ;
212+ trace . astNodeRegistry = new Map ( JSON . parse ( obj . astNodeRegistry ) ) ;
213+
214+ return trace ;
215+ }
216+
217+ /* */
218+
219+ register ( astNode , state ) {
220+ if ( this . astNodeRegistry . has ( astNode ) ) {
221+ return this . astNodeRegistry . get ( astNode ) ;
222+ }
223+
224+ const filename = state . file . opts . filename ;
225+ let fileID ;
226+
227+ if ( this . fileRegistry . has ( filename ) ) {
228+ fileID = this . fileRegistry . get ( filename )
229+ } else {
230+ fileID = this . filenames . push ( filename ) - 1 ;
231+ this . fileRegistry . set ( filename , fileID ) ;
232+ }
233+
234+ const start = astNode . loc . start ;
235+ const end = astNode . loc . end ;
236+
237+ const location = [
238+ fileID ,
239+ start . line ,
240+ start . column ,
241+ end . line ,
242+ end . column
243+ ] ;
244+
245+ const id = this . locations . push ( location ) - 1 ;
246+ this . astNodeRegistry . set ( astNode , id ) ;
247+
248+ return id ;
249+ }
250+
251+ resolve ( locationID ) {
252+ const location = this . locations [ locationID ] ;
253+ return {
254+ filename : this . filenames [ location [ 0 ] ] ,
255+ startLine : location [ 1 ] ,
256+ startColumn : location [ 2 ] ,
257+ endLine : location [ 3 ] ,
258+ endColumn : location [ 4 ]
259+ }
260+ }
193261
194262 /* AST changes */
195263
@@ -239,12 +307,12 @@ class Trace {
239307 nextLoopIteration ( position , ...args ) {
240308 this . log ( new Event ( 'nextLoopIteration' , args . map ( clone ) , position ) ) ;
241309 }
242-
310+
243311 forIterator ( position , iterator ) {
244312 this . log ( new Event ( 'forIterator' , iterator , position ) ) ;
245313 return iterator ;
246314 }
247-
315+
248316 forKeys ( position , keys ) {
249317 this . log ( new Event ( 'forKeys' , keys , position ) ) ;
250318 return keys ;
@@ -335,56 +403,22 @@ class Trace {
335403}
336404
337405Trace . traceIdenifierName = '__currentTrace__' ;
338- Trace . locations = [ ] ;
339- Trace . fileRegistry = new Map ( ) ;
340- Trace . astNodeRegistry = new Map ( ) ;
341-
342- Trace . register = function ( astNode , state ) {
343- if ( Trace . astNodeRegistry . has ( astNode ) ) {
344- return Trace . astNodeRegistry . get ( astNode ) ;
345- }
346-
347- const filename = state . file . opts . filename ;
348-
349- const start = astNode . loc . start ;
350- const end = astNode . loc . end ;
351-
352- const locationObject = {
353- filename,
354- startLine : start . line ,
355- startColumn : start . column ,
356- endLine : end . line ,
357- endColumn : end . column
358- }
359-
360- const id = Trace . locations . push ( locationObject ) - 1 ;
361- Trace . astNodeRegistry . set ( astNode , id ) ;
362-
363- return id ;
364- }
365-
366- Trace . resolve = ( number , locations ) => locations [ number ] ;
367406
368407Trace . on = async function ( source , pluginsUrls ) {
369- const data = await loadPlugin ( source , pluginsUrls )
408+ const data = await loadPlugin ( source , pluginsUrls ) ;
370409 const obj = {
371410 locations : data . locations ,
372411 oldAST : JSON . parse ( data . oldAST ) ,
373412 transformedAST : JSON . parse ( data . transformedAST || '{}' ) ,
374- trace : Object . assign ( new Trace ( ) , JSON . parse ( data . trace ) ) ,
413+ trace : Trace . deserializedFrom ( data . trace ) ,
375414 transformedCode : data . transformedCode
376415 } ;
377416
378417 const trace = obj . trace ;
379418
380- for ( const entry of trace . _log ) {
381- entry . position = obj . locations [ entry . position ] ;
382- //console.log(entry)
383- }
384-
385419 trace . oldAST = obj . oldAST ;
386420 trace . transformedAST = obj . transformedAST ;
387-
421+
388422
389423 trace . analyze ( ) ;
390424 return trace ;
0 commit comments