Skip to content

Commit 673e6c1

Browse files
authored
Fix extracting unnecessary tokens (#16)
1 parent 3ada41e commit 673e6c1

File tree

2 files changed

+21
-12
lines changed
  • src/parser/converts
  • tests/src/parser/converts

2 files changed

+21
-12
lines changed

src/parser/converts/es.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -494,23 +494,29 @@ function convertESNode0<N extends ESTree.Node>(
494494
node: N | null | undefined,
495495
parent: any,
496496
ctx: Context,
497-
extracted = new Set<ESTree.Node>(),
497+
extracted = new Set<ESTree.Node | string>(),
498498
): N | null {
499499
if (!node) {
500500
return null
501501
}
502502
if (!extracted.has(node)) {
503-
EXTRACT_TOKENS[node.type]?.(node, ctx, parent)
504-
for (const comment of [
505-
...(node.leadingComments || []),
506-
...((node as any).innerComments || []),
507-
...(node.trailingComments || []),
508-
]) {
509-
ctx.addComment({
510-
type: comment.type,
511-
value: comment.value,
512-
...ctx.getConvertLocation(getWithLoc(comment)),
513-
})
503+
const key = `${node.type}@[${getWithLoc(node).start},${
504+
getWithLoc(node).end
505+
}]`
506+
if (!extracted.has(key)) {
507+
EXTRACT_TOKENS[node.type]?.(node, ctx, parent)
508+
for (const comment of [
509+
...(node.leadingComments || []),
510+
...((node as any).innerComments || []),
511+
...(node.trailingComments || []),
512+
]) {
513+
ctx.addComment({
514+
type: comment.type,
515+
value: comment.value,
516+
...ctx.getConvertLocation(getWithLoc(comment)),
517+
})
518+
}
519+
extracted.add(key)
514520
}
515521
extracted.add(node)
516522
}

tests/src/parser/converts/es.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ describe("tokens test", () => {
146146
"const a = -b.c",
147147
"c instanceof a",
148148
"a++;++a;a--;--a",
149+
`o = {
150+
c,
151+
}`,
149152
]
150153
for (const code of tests.reverse()) {
151154
it(code, () => {

0 commit comments

Comments
 (0)