Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emitted AST should optionally include comments #4165

Closed
saibotsivad opened this issue Mar 26, 2022 · 9 comments
Closed

Emitted AST should optionally include comments #4165

saibotsivad opened this issue Mar 26, 2022 · 9 comments

Comments

@saibotsivad
Copy link

Describe the feature

When using the parse method in the JavaScript lib @swc/core to generate AST, comments are not emitted. For example:

const swc = require('@swc/core')
const source = `
/**
 * Hello world!
 */
export class Test {}
`
swc.parse(source, {
    syntax: 'typescript',
    comments: true,
    isModule: true,
}).then(out => {
    console.log(JSON.stringify(out.body, undefined, 4))
})

Currently logs this, which notably does not contain the comment:

[ {
    "type": "ExportDeclaration",
    "span": { "start": 25, "end": 45, "ctxt": 0 },
    "declaration": {
        "type": "ClassDeclaration",
        "identifier": {
            "type": "Identifier",
            "span": { "start": 38, "end": 42, "ctxt": 0 },
            "value": "Test",
            "optional": false
        },
        "declare": false,
        "span": { "start": 32, "end": 45, "ctxt": 0 },
        "decorators": [],
        "body": [],
        "superClass": null,
        "isAbstract": false,
        "typeParams": null,
        "superTypeParams": null,
        "implements": []
    }
} ]

Of course, storing comments in the AST increases the size significantly, so it makes sense for this to be optional.

Having the comments in the AST is important to enable certain developer tools, so I would like to have it as an option.

Babel plugin or link to the feature description

No response

Additional context

No response

@kwonoj
Copy link
Member

kwonoj commented Mar 27, 2022

@kdy1 this sounds like similar (not exactly same) discussion we had for plugin transform side comment support.

  • Do you think it makes sense if we could have consolidated way to support both?
  • if so, any suggestions?

@kdy1
Copy link
Member

kdy1 commented Mar 27, 2022

We can return a map in comments field for parse().
I don't think this is related to transform apis.

@kdy1 kdy1 added this to the Planned milestone Mar 27, 2022
@kdy1 kdy1 modified the milestones: Planned, v1.2.199 Jun 11, 2022
@RoenLie
Copy link

RoenLie commented Oct 3, 2022

Any updates on this?

@kwonoj
Copy link
Member

kwonoj commented Oct 3, 2022

If you see the issue does not have an update, that's reflecting the current state. Core team has small resources and sometimes cannot keep up all the feature improvement request from the community vs. internal priorities we are currently focusing.

@RoenLie
Copy link

RoenLie commented Oct 3, 2022

No worries! I just asked since I didn't find it in the milestone it was added to.
You're all doing a great job and it's highly appreciated!

@kdy1 kdy1 removed this from the Planned milestone Dec 22, 2022
@recover758126
Copy link

so, this be solved?

@Nickersoft
Copy link

@kdy1 Was this ever implemented?

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 12, 2024
@Nickersoft
Copy link

@kdy1 I actually realized that the TypeScript compiler doesn't preserve comments in its AST either. I was looking at the advanced demo for the TSDoc API and realized they use the TS method getLeadingCommentRanges along with node.pos to find the beginning of declarations and traverse upwards to find the block comment.

I was hoping to do the same using @swc/core instead of the TS compiler, but found this bug in which the span start increases with every invocation of the parser, making it impossible to get an accurate pos position for declarations.

Does this mean there's absolutely no way to extract comments from a file?

@swc-bot
Copy link
Collaborator

swc-bot commented Sep 12, 2024

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Sep 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

7 participants