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

Parsing and generating while preserving whitespace #16621

Open
1 task done
KernelDeimos opened this issue Jul 8, 2024 · 7 comments
Open
1 task done

Parsing and generating while preserving whitespace #16621

KernelDeimos opened this issue Jul 8, 2024 · 7 comments

Comments

@KernelDeimos
Copy link

KernelDeimos commented Jul 8, 2024

💻

  • Would you like to work on this feature?

What problem are you trying to solve?

I'd like to build refactoring tools using babel, but I don't want these tools to be "disruptive" and cause unnecessary whitespace changes. For example this feature request was dropped, so my refactoring tool wouldn't be compatible with a project using 4 spaces for indentation; that said, this wouldn't have been a good solution for me anyway because a project might have inconsistent indentation or other whitespace oddities.

Seeing as @babel/parser outputs a CST rather than an AST without position information, it seems entirely plausible for @babel/generator to output in accordance with the CST.

Describe the solution you'd like

Option to preserve whitespace in the generator

Describe alternatives you've considered

Using another package like escodegen, but @babel/generator is the only package I've found with good support for modern syntax.

Documentation, Adoption, Migration Strategy

To the user of the package I imagine this would be a flag. This flag would be mutually exclusive to some other flags though such as retainLines, so perhaps this nuance should be documented as well. When the flag is not set, @babel/generator should behave exactly as it currently does to avoid regressions.

@babel-bot
Copy link
Collaborator

Hey @KernelDeimos! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@j4k0xb
Copy link

j4k0xb commented Jul 9, 2024

Check out https://github.com/benjamn/recast

@KernelDeimos
Copy link
Author

Check out https://github.com/benjamn/recast

Oh, this can be de-coupled! After reading the README of recast that makes a lot of sense. I'm closing my issue because IMO this is the right solution.

@nicolo-ribaudo
Copy link
Member

Fyi, I plan to start working on this soon: babel/rfcs#15

@KernelDeimos
Copy link
Author

KernelDeimos commented Jul 9, 2024

Fyi, I plan to start working on this soon: babel/rfcs#15

I eagerly await this and I'm re-opening the issue I posted. I don't know which will come first, but "august of last year" doesn't inspire confidence in the recast issue I linked.

@KernelDeimos KernelDeimos reopened this Jul 9, 2024
@KernelDeimos
Copy link
Author

KernelDeimos commented Jul 9, 2024

Does this mean... it's impossible to make refactoring tools like I described right now? (without writing a generator or fixing one I mean) Is there actually no source code in existence that can do this on modern javascript code? I just want to make sure I'm not misunderstanding something; Javascript is one of the most popular languages on the planet right now and this came as a surprise to me.

@coderaiser
Copy link
Contributor

You can also use @putout/printer it is made for Babel AST and has lots of configurations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants