babel / babel/babel

Make comment node to normal node like others

Open
#13,877 3 comments 0 reactions 0 assignees View on GitHub
i: needs triage
Dominant language
TypeScript
Stars
44k
Forks
6k
Avg merge
5d 15h
Merged PRs (30d)
23

Description

### 💻

- [ ] Would you like to work on this feature?

### What problem are you trying to solve?

The current comment is a normal node, it cannot be traversed.

**leadingComments** and **trailingComments** behave very strangely.

the next node is `leadingComments` is the same as the previous node's `trailingComments`.

```js
const exp1 = 1 + 2 // foo

// new line
const exp2 = 3 + 4 // bar
```

![截屏2021-10-24 13 21 02](https://user-images.githubusercontent.com/9758711/138581960-30445b9f-0f6e-4f94-9cc6-546931ab82b6.png)

If it is a normal node, there will be no such problem.

### Why am I concerned about this issue?

I'm writing a custom generator. Regarding how to generate annotated code, I found that the AST is not clear enough

### Describe the solution you'd like

Comments should be the same as other nodes(optional)

```json
[
{
"type": "VariableDeclaration"
...
},
{
"type": "CommentLine"
...
},
{
"type": "VariableDeclaration"
...
}
]
```

### Describe alternatives you've considered

none

### Documentation, Adoption, Migration Strategy

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by tracing how the AST represents and traverses leadingComments and trailingComments for the example with two variable declarations. The change would be complete when comments are represented as traversable CommentLine nodes alongside VariableDeclaration nodes, without the current duplicated attachment behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.