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

Query: Can prettier create a js file from an AST #5998

Closed
jaitaiwan opened this issue Mar 22, 2019 · 4 comments
Closed

Query: Can prettier create a js file from an AST #5998

jaitaiwan opened this issue Mar 22, 2019 · 4 comments
Labels
area:api Issues with Prettier's Application Programming Interface locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. status:needs discussion Issues needing discussion and a decision to be made before action can be taken type:enhancement A potential new feature to be added, or an improvement to how we print something

Comments

@jaitaiwan
Copy link

Hi, I've gone through and tried to find similar issues one of which is #1059.

I'm looking at creating some sort of visual scripting interface for JavaScript as a personal project. I'm very much on board with Prettier's goal, so I feel that code generated by the Visual Scripting tool should result in the same code as prettier would output. So I have a similar question to what the person in 1059 has:

Is there an exposed api that would allow me to input a programatically created AST into prettier and output a Javascript file?

@bakkot
Copy link
Collaborator

bakkot commented Mar 25, 2019

#2068 would presumably provide this, but to my knowledge there's nothing currently. Your best bet at the moment is probably using something like @babel/generator or escodegen or shift-codegen to turn your AST into an unformatted string, then pass that to Prettier. It adds a bit of overhead (though less than you might expect), but it works.

@alexander-akait alexander-akait added status:needs discussion Issues needing discussion and a decision to be made before action can be taken type:enhancement A potential new feature to be added, or an improvement to how we print something labels Aug 7, 2019
@thorn0
Copy link
Member

thorn0 commented Mar 19, 2020

It's possible to do this using the Custom Parser API:

// suppose you have your AST in the yourAst variable
const formatted = prettier.format(
  ".", // any non-empty, non-whitespace string 
  { parser: () => yourAst }
);

But keep in mind that Prettier mutates the AST.

@thorn0 thorn0 added the area:api Issues with Prettier's Application Programming Interface label May 6, 2020
@benatkin
Copy link

Worth reading about gofmt's substitution command https://golang.org/cmd/gofmt/

@thorn0
Copy link
Member

thorn0 commented Nov 18, 2020

Closing in favor of #9114

@thorn0 thorn0 closed this as completed Nov 18, 2020
@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Feb 17, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area:api Issues with Prettier's Application Programming Interface locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. status:needs discussion Issues needing discussion and a decision to be made before action can be taken type:enhancement A potential new feature to be added, or an improvement to how we print something
Projects
None yet
Development

No branches or pull requests

5 participants