Live data from Hacker News

Why is Prettier rock solid?

mrmr.io

151–160 of 163 posts

Re: Why is Prettier rock solid?

#151

Is this remarkable? I also find black, the Python formatter, to be very reliable. Would anyone really use these tools if they occasionally munged your code?

Black is actually the reason I've been drifting from indifferent to against linters/formatters. It once broke our code around string handling, and it sometimes makes some odd choices when splitting long function calls - in a way that's inconsistent and reduces readability. And because they refuse to make it more configurable, you can't fix these cases.

Re: Why is Prettier rock solid?

#152
post #84
post #72

Earlier quoted context omitted.

The more silly[1] comments are from the maintainer, saying you should update configs with workarounds for patch releases. 1: Suggested package: sillier.

"It's not my commit that it is wrong, it's the rest of the world that needs to fix their code." This is such an insane position to take for a maintainer. Don't be idealistic and silly. If you break compatibility, it is on you. No one cares that you are right. The Linus Torvalds "we do not break the user-space, ever" mantra should be a best practice for every open source programmer, and user-space in this case is "wha…

> The Linus Torvalds "we do not break the user-space, ever" mantra should be a best practice for every open source programmer

I think that has to forever live in tension with its opposite. I mean, every opensource project also lives or dies based on the volunteered time of its maintainers. If "being be idealistic and silly" is needed to keep maintainers around, then its not silly.

Re: Why is Prettier rock solid?

#153

Earlier quoted context omitted.

It was a breaking change, lots of people had to change their broken code, including me

So, don't upgrade it until you fixed your code?

Normally patch updates do not require you to fix your code but often include desirable security fixes. That is the purpose of semantic versioning.

Re: Why is Prettier rock solid?

#154
post #148

Earlier quoted context omitted.

It was a breaking change, lots of people had to change their broken code, including me

You had to change your code because it was literally broken to begin with. It's not a "breaking change" if a library continues to do exactly what it's advertised as doing in a completely compatible way and already broken code downstream at no fault of the authors suddenly doesn't like it. Say a JPEG library added an additional and completely harmless metadata field - and then some JPEG parser explodes because it had…

My code wasn't broken, I had a tsconfig file that was formatted by prettier and prettier removed trailing commas. Then I updated prettier without reformatting and my tests failed because prettier rejected the now-incorrectly formatted tsconfig file.

Also your analogy is wrong and not what is happening here. Suppose that a JPEG encoder encoded files as png when the flag is `--filetype=unknown`. Then in a patch release the behavior changes so that "unknown" means jpeg. That's closer to what happened here.

Re: Why is Prettier rock solid?

#155
post #53

Earlier quoted context omitted.

Oh I'm curious why you're rewriting it? Maybe speed, quality, handling new language features, or all of the above? I may dive into this problem What do you think of the functional "pretty printing languages" like Wadler's (cited in the blog post)? It is kinda interesting that the author of prettier credits the algorithm, but other people say that it's more about labor and testing of specific style rules. Having never…

> Oh I'm curious why you're rewriting it? The primary driver is that we're moving to a fairly different formatting style: https://github.com/dart-lang/dart_style/issues/1253 The formatter works sort of like a compiler in that it parses the code, translates it to an internal representation, does optimization on that IR, and then outputs final code. The main difference is that the "final code" is also source code, and…

OK very interesting! I skimmed over the issue and some of the examples.

It does seem like there are at least two schools of thought on the pretty printers -- those influenced by the Wadler pretty printing language (functional style), and those more influenced by go fmt (which does no line wrapping) and clang-format (which does).

There's a very recent paper linked here which has a good survey of the functional style. Table 1 is a nice summary of the different algorithms and IRs, a bunch of them appearing only in the last 10 years.

https://lobste.rs/s/aevptj/why_is_prettier_rock_solid

A Pretty Expressive Printer (with Appendices) - https://arxiv.org/pdf/2310.01530.pdf

I'm probably biased toward the "non-PPL" style of clang-format because I think it's the best formatter I've used. It's fast and does what I want. I would probably attribute that to all the elbow grease and testing put in over the years, and being written in C++, not necessarily the wrapping algorithm.

But now that I see the examples, my suspicion is that some of the newer IRs and algorithms do address some of the Dart problems (though I didn't try anything!). Dart seems to have long expressions very much in common with functional languages, as opposed to Go, which apparently doesn't need wrapping at all!

I found this post linked from the paper, and it has 3 examples of formatting s-expressions that seem pretty similar to your examples.

And they discuss what IR is needed to express that. They talk about greedy algorithms vs. "non-local" dependencies.

https://jyp.github.io/posts/towards-the-prettiest-printer.ht...

It seems like the IR of chunks, rules, and spans has a lot of similarity to what they call "groups" and so forth. Several years ago, long before I looked at any of this, I actually implemented a data structure printer with the same 2 rules as on page 2 of this paper - https://lindig.github.io/papers/strictly-pretty-2000.pdf

i.e. try to fit all the parts on a single line, and if that fails, put each part on its own line. So the basic intuition is natural and obvious.

---

But I always want to see how these algorithms are tested with real languages, and how they perform. I'm still not sure how easy it is to encode all the language specific rules in these "cost functions", but I may give it a stab (I have more than one formatter to write!).

The paper says it's tested on Racket S-expressions, and that there is a large amount of non-trivial convention around that syntax. But I think that's still a far cry from C++ or Python, in both the amount of syntax and how many users / how much code, etc.

I do wonder about the problem of computing layouts from scratch vs. fixing existing layouts. And the problem of formatter upgrades causing churn. My impression is that go fmt tends to fix things, erring on the side of leaving code alone, and people seem to like that? I guess the fear is with the "global optimization" algorithms, the formatter will make a bunch of choices and it's hard to figure out why.

Re: Why is Prettier rock solid?

#156
Do not use Prettier with HTML. We can't use Prettier at work because it doesn't reformat HTML but actually rewrites it, taking liberties to insert new closing tags where it sees fit and where they were never present before. We found this because it actually led to things breaking in practice. A code formatter should never add meaningful new code to your project which changes its behaviour and content. That's no longer a code formatter but a random bug generator.

Re: Why is Prettier rock solid?

#159
post #117

Earlier quoted context omitted.

If I'm reading this correctly, you think juniors should live without formatters because they need to learn to format manually? I entirely disagree. Juniors have to learn so many more important things - how to logically structure code, testing, working on teams, git... Why take time away from learning those (and similar) concepts to teach them something a tool does well and, if necessary, they can learn later? I like…

> If I'm reading this correctly, you think juniors should live without formatters because they need to learn to format manually? You did not interpret what I said correctly. Not that you had much to work with, but that small wall of text is based on a major extrapolation. I said completely relying on these tools is doing them disservice. How do you train your muscle memory when the tools are doing everything for you.…

Only an fool would waste time on manual code formatting. The editor should automatically apply the organization's formatting rules. No muscle memory is required.
Post reply on HN