File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -332,25 +332,24 @@ function transform(babel) {
332332}
333333
334334function getComments ( path ) {
335+ const allComments = path . hub . file . ast . comments ;
335336 if ( path . node . leadingComments ) {
336337 // Babel AST includes comments.
337338 return path . node . leadingComments ;
338339 }
339340 // In Hermes AST we need to find the comments by range.
340- const comments = path . hub . file . ast . comments ;
341- let prevSibling = path . getPrevSibling ( ) ;
342- let searchStart ;
343- if ( prevSibling . node ) {
344- searchStart = prevSibling . node . end ;
345- } else if ( path . parentPath . node ) {
346- searchStart = path . parentPath . node . start ;
347- } else {
348- throw new Error ( 'Unexpected AST structure' ) ;
341+ const comments = [ ] ;
342+ let line = path . node . loc . start . line ;
343+ let i = allComments . length - 1 ;
344+ while ( i >= 0 && allComments [ i ] . loc . end . line >= line ) {
345+ i -- ;
349346 }
350- const filteredComments = comments . filter (
351- c => c . start >= searchStart && c . end <= path . node . start
352- ) ;
353- return filteredComments . length > 0 ? filteredComments : undefined ;
347+ while ( i >= 0 && allComments [ i ] . loc . end . line === line - 1 ) {
348+ line = allComments [ i ] . loc . start . line ;
349+ comments . unshift ( allComments [ i ] ) ;
350+ i -- ;
351+ }
352+ return comments ;
354353}
355354
356355module . exports = transform ;
You can’t perform that action at this time.
0 commit comments