@@ -2,10 +2,27 @@ import * as t from '@babel/types'
2
2
import { NodePath } from '@babel/traverse'
3
3
4
4
export default function recastBugWorkarounds ( path : NodePath < any > ) : void {
5
+ const visitedComments : Set < t . Comment > = new Set ( )
5
6
path . traverse ( {
6
7
exit ( path : NodePath < t . Node > ) {
8
+ const { leadingComments } = path . node
7
9
const anyNode = path . node as any
8
- anyNode . comments = anyNode . leadingComments
10
+ if ( leadingComments ) {
11
+ anyNode . comments = [ ]
12
+ if ( leadingComments ) {
13
+ for ( const comment of leadingComments ) {
14
+ if ( visitedComments . has ( comment ) ) continue
15
+ visitedComments . add ( comment )
16
+ anyNode . comments . push ( {
17
+ ...comment ,
18
+ leading : true ,
19
+ trailing : false ,
20
+ } )
21
+ }
22
+ }
23
+ } else {
24
+ anyNode . comments = null
25
+ }
9
26
} ,
10
27
AwaitExpression ( path : NodePath < t . AwaitExpression > ) {
11
28
const argument = path . get ( 'argument' )
@@ -18,4 +35,22 @@ export default function recastBugWorkarounds(path: NodePath<any>): void {
18
35
}
19
36
} ,
20
37
} )
38
+ path . traverse ( {
39
+ exit ( path : NodePath < t . Node > ) {
40
+ const { trailingComments } = path . node
41
+ const anyNode = path . node as any
42
+ if ( trailingComments ) {
43
+ if ( ! anyNode . comments ) anyNode . comments = [ ]
44
+ for ( const comment of trailingComments ) {
45
+ if ( visitedComments . has ( comment ) ) continue
46
+ visitedComments . add ( comment )
47
+ anyNode . comments . push ( {
48
+ ...comment ,
49
+ leading : false ,
50
+ trailing : true ,
51
+ } )
52
+ }
53
+ }
54
+ } ,
55
+ } )
21
56
}
0 commit comments