Could prettier be used to print from an AST?
See original GitHub issueI really like how prettier handles code wrapping and I’m experimenting with using it to print ASTs constructed with babel-types
and ast-types
(jscodeshift
).
However, differentiating from recast, prettier’s printing algorithm now relies on the .raw
values of literals (https://github.com/jlongster/prettier/blob/6e68f7495fba705c4d1e58e63efb5a41dea2df7d/src/printer.js#L714-L723), which makes it unable to print a programmatically constructed AST literal node.
Is reading from the .raw
field necessary for properly handling unicode characters? If I understand it correctly, would you be interested in a PR that let prettier check and fallback to use/cast the .value
field when .raw
does not present on a node? 😃
Issue Analytics
- State:
- Created 7 years ago
- Reactions:7
- Comments:18 (11 by maintainers)
Top Results From Across the Web
Technical Details - Prettier
The basic idea is that the printer takes an AST and returns an intermediate representation of the output, and the printer uses that...
Read more >How to write a plugin for Prettier | by Franco Victorio | Medium
and then prints the code using only the AST. That means that the style of the original code is (almost) completely ignored.
Read more >Exploring the AST with Babylon and Prettier | Swizec Teller
We're going to use Babylon to parse code into an AST and Prettier to print it ... The rest is there to parse...
Read more >On pretty printers | William Durand
Pretty printers are tools used to format textual content according to a set of stylistic conventions. Prettier, black, rustfmt are great ...
Read more >ICDevs.org - Bounty #10(150ICP - 5:1 match) - Prettier Plugin
Prettier requires a special AST format which is certainly not supported by the Motoko parser. So you can get some kind of AST...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Holy crap, its beautiful.
I think there are some specific nuances with spacing and indent we’ll look into but it looks like I can pass recast and pretty options that seem pretty similar.
By the way, we also read from the original source in order to figure out if there are new lines for comments and preserve empty lines. You want to make sure that the location of the original source is preserved otherwise it’s going to look completely broken.