question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Could prettier be used to print from an AST?

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
TheLarkInncommented, Feb 23, 2017

Holy crap, its beautiful.

screen shot 2017-02-23 at 11 36 59 am

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.

1reaction
vjeuxcommented, Jan 25, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found