Skip to content

Commit 9a401ed

Browse files
committed
fix(recastBugWorkarounds): rework how comments are handled
1 parent 17619e9 commit 9a401ed

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/util/recastBugWorkarounds.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,27 @@ import * as t from '@babel/types'
22
import { NodePath } from '@babel/traverse'
33

44
export default function recastBugWorkarounds(path: NodePath<any>): void {
5+
const visitedComments: Set<t.Comment> = new Set()
56
path.traverse({
67
exit(path: NodePath<t.Node>) {
8+
const { leadingComments } = path.node
79
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+
}
926
},
1027
AwaitExpression(path: NodePath<t.AwaitExpression>) {
1128
const argument = path.get('argument')
@@ -18,4 +35,22 @@ export default function recastBugWorkarounds(path: NodePath<any>): void {
1835
}
1936
},
2037
})
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+
})
2156
}

0 commit comments

Comments
 (0)