7 ms·
Why is Prettier rock solid?
- jen729w 3y agoHmm I just had a (perhaps esoteric) issue in an Astro file. Specifying <!-- prettier-ignore --> above a <div> would cause the <style> block at the bottom to become duplicated on save. Weird as, and I couldn’t be bothered troubleshooting, I just added the file to .prettierignore. But still.
- Princesseuh 3y agoPrettier does not support Astro files natively, so it's the Astro plugin's fault in this case. (I maintain said plugin, so, well, it's my fault)
- jen729w 3y agoHa! Well, I’m happy to help troubleshoot if you’d like?
- keybored 3y agoIs that also why Java generics are rock solid?
- keybored 3y agoThis was said in jest but: I wouldn’t make this comment seriously. The difference here is that the authors of Java generics had to work with the hysterical raisins of pre-generics Java.
- beebeepka 3y agoI've worked with a number of developers whose output without these tools is a giant, unreadable mess. Training juniors to be reliant on this stuff does more harm (to them) than good.
- bbkane 3y agoIf 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 it to deciding to teach teens how to drive with a manual transmission instead of an automatic one- they need to be able to drive and not hit things, and the more we can simplify that, the happier we all are. If needed, some kids can go back and learn how to use a stick shift.
- beebeepka 3y ago> 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. I, for one, believe proper formatting is a sign of care and it's really easy to spot when someone is not paying attention. Go on, mod me down harder.
- nradov 3y agoOnly an fool would waste time on manual code formatting. The editor should automatically apply the organization's formatting rules. No muscle memory is required.
- assimpleaspossi 3y agoMy complaint--and MDN and the W3C's complaint--with it is that it puts a closing slash on HTML void elements. While this is allowed for backwards compatibility with XHTML, it is not specified as being useful or needed in any HTML specification and has led to countless confusion among noobs who don't read the specification and only follow online articles. https://html.spec.whatwg.org/dev/syntax.html#start-tags https://html.spec.whatwg.org/dev/syntax.html#start-tags
- littlecranky67 3y agoThe closing slash is mandatory for jsx/tsx - and thus, most React devs - though.
- assimpleaspossi 3y agoThat has nothing to do with HTML.
- strbean 3y agoI kind of hate that. Why have variation in the grammar based on what tag is used? Always using `<tag></tag>` or `<tag/>` is so simple and easy.
- Aeolun 3y agoPrettier was always nice, but these days I use biomejs because I haven’t got all day to sit around waiting for my code to be formatted.
- globular-toast 3y agoIs 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?
- Izkata 3y agoBlack 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.
- Mawr 3y agoHuh? An autoformatter is just a one big pure function, so it's easy to test. It'd be odd for it not to be solid. If it's not even fast, then nothing of note was even achieved. Anybody can write a slow formatter that conforms to a test suite.
- psankar 3y agoPrettier was inspired by gofmt. But I really wish gofmt gets inspired by prettier and automatically breaks long (80+ columns) lines into smaller lines (For example, move parameters of a long function each into their row).
- skowalak 3y agoIt is not in the stdlib, but you can use golines for that. https://pkg.go.dev/github.com/wrype/golines https://pkg.go.dev/github.com/wrype/golines
- joe0789 3y ago[flagged]
- T3RMINATED 3y ago[dead]
- junon 3y agoI don't resonate with this. We've had a bunch of annoying issues with Prettier making code a mangled mess. It's not hard to not break code syntactically, but prettier has certainly messed things up visually before.
- nojvek 3y agoAnything rock solid has a well thought out architecture, where the author(s) have thought through the entire problem domain and edge cases. Also has a ton of test cases to verify behavior. Prettier is rock solid but compared to rust based tools like ruff, it is dog shit slow.
- effnorwood 3y ago[dead]
- err4nt 3y agoDo 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.
- nkozyra 3y ago> note that gofmt doesn't indent, which is the hard part Huh? I'm pretty sure it indents with tabs.
- reactordev 3y agoIt finally settled the tabs vs spaces debate by forcing tabs.
- norir 3y agoThe only sane option.
- coffeebeqn 3y agoI’m not sure what that would even mean. It definitely intends but maybe there are some edge cases where it has no opinion - like a long function signature with too many parameters it leaves alone unless you manually start new lining things
- sa46 3y agoI suspect the author meant that gofmt doesn’t break lines, which dramatically simplifies the complexity. See https://journal.stuffwithstuff.com/2015/09/08/the-hardest-program-ive-ever-written/ https://journal.stuffwithstuff.com/2015/09/08/the-hardest-pr... for a fun, related read.
- munificent 3y agoYeah, line splitting is the hard part. I wrote that article and I'm actually in the middle of rewriting dartfmt right now to have an entirely new internal representation.
- chubot 3y agoOh 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 written a pretty printer, it does seem like there is a lot of labor in encoding the "rules people like", and flags for different styles. And then there's the actual algorithm to find the line breaks. Or maybe they are not really separate things. I think the more academic work is trying to separate the 2 things, but they can be fairly intertwined? (e.g. for performance reasons)
- hardwaregeek 3y agoEhh I would disagree here. Yes, prettier uses the classic pretty printing paper, but almost every pretty printer does in some form. And even then it’s subjective how you compile the actual syntax down to the printer elements. Prettier uses a very thick layer of complicated translation rules to get the printing just right. Especially with JSX and TypeScript. It’s honestly just good old testing and labor that makes it good. And even then I anticipate more than a few comments decrying prettier’s formatting. You can’t please everybody. That’s why it’s prettier not prettiest
- chubot 3y agoI think I agree about the testing and labor of complicated translation rules. But it doesn't appear that almost every pretty printer is based on the Wadler algorithm. It seems like MOST of them are not? e.g. clang-format is one of the biggest and best, and it has a model that includes "unwrapped lines", a "layouter", a line break cost function, exhaustive search with memoization, and Dijikstra's algorithm: https://llvm.org/devmtg/2013-04/jasper-slides.pdf https://llvm.org/devmtg/2013-04/jasper-slides.pdf The YAPF Python formatter is based on this same algorithm - https://github.com/google/yapf https://github.com/google/yapf The Dart formatter used a model of "chunks, rules, and spans" - https://journal.stuffwithstuff.com/2015/09/08/the-hardest-program-ive-ever-written/ https://journal.stuffwithstuff.com/2015/09/08/the-hardest-pr... It almost seems like there are 2 camps -- the functional algorithms for functional/expression-based languages, and other algorithms for more statement-based languages. Though I guess Prettier/JavaScript falls on the functional side. I just ran across this paper that includes a nice survey (on lobste.rs) and it seems to cover the functional pretty printing languages influenced by Wadler, in the functional style (e.g. for and in Racket or Haskell), but not the other kind of formatter ("Google" formatters perhaps) https://arxiv.org/pdf/2310.01530.pdf https://arxiv.org/pdf/2310.01530.pdf
- hardwaregeek 3y agoAh that’s a good point. The Dart one is the Wadler one with modified terminology, but the rest do appear to be different
- 3y ago
- jerbear4328 3y agoI'm not sure I got an answer to the question in the title. Prettier is rock solid because it was based on a paper by someone who designed Haskell? That's a cool link, but I'm not sure what the insight is, is this evidence that functional programming-style algorithms lead to a more stable codebase?
- itishappy 3y agoI think prettier's initial author answered the question quite concisely in the article's first link. Title and excerpt copied below: > Dig into past research. > If you're excited about an idea, it's super tempting to sit down an immediately get going. But you shouldn't do that until you've done some cursory research about how people have solved it before. Spending a few days researching the topic always completely changes how I am going to solve it. https://archive.jlongster.com/How-I-Became-Better-Programmer https://archive.jlongster.com/How-I-Became-Better-Programmer
- noam_k 3y agoI can't agree more, and I can't disagree more. There's a great deal to be learned by experimenting yourself. My workflow is: * Learn the concept * Implement it * Research prior art * Throw away my first implementation and rewrite it properly (Edit: formatting)
- rkangel 3y agoYes - I quite often get into research paralysis where I keep looking and trying to work out the solutions to different problems I'm going to encounter and never getting started. Whereas if I start protoyping I learn a lot very quickly.
- chubot 3y agoYeah I kinda think people are always "learning from their last mistake" (and that's definitely true for me) - Sometimes you hack, and you end up with a mess. And then the lesson you learn is to study the prior art first. - Sometimes you spin your wheels reading stuff that isn't actually applicable to the problem you have. Then the lesson learned is to "do the simplest thing that could work", "solve 80% of the problem with 20% of the effort", etc. So to me, programming is really a cycle of learning, and you can't really generalize the order of doing things ... It's iterative stumbling and learning, in both orders :)
- deleted 3y ago[deleted]
- haburka 3y agoThis isn’t actually true. Prettier does break code in strange circumstances and it can be very difficult to fix. The maintainers have a ton of stress on them and it’s hard to improve it sometimes. It’s very well tested and easy to add cases too - just add an input and an output so that may be why it seems so solid, just whenever there is an issue people fix it. I think the main thing is that Meta maintains it which probably is why it’s so solid.
- goodoldneon 3y agoWhat’s an example of Prettier breaking code?
- wildpeaks 3y agoAnother example: https://github.com/prettier/prettier/issues/187 https://github.com/prettier/prettier/issues/187 That issue has been open for 7 years.
- blopker 3y agoThe biggest issue I run in to is with Django HTML. It's unusable. They know about it and they said they won't fix it.
- exac 3y agoFor example, the latest Prettier makes XHTML files invalid by changing DOCTYPE to lowercase: https://github.com/prettier/prettier/issues/15476 https://github.com/prettier/prettier/issues/15476 Prettier moves ts-ignore comments which can cause TypeScript errors: https://github.com/prettier/prettier/issues/15876 https://github.com/prettier/prettier/issues/15876 Interpreting nested CSS functions' "-" as minus and inserting a space: https://github.com/prettier/prettier/issues/15369 https://github.com/prettier/prettier/issues/15369
- myaccountonhn 3y agoPrettier isn’t terrible but compared to ocamlformat it is soooo… slow. Each time I save it takes a second or two for it to format. Ocamlformat is instant in the meanwhile.
- Mogzol 3y agoHow large are the files you're formatting? I have prettier set to format on save in vscode and even for the largest files in my codebase (>1000 lines) it formats near-instantaneously on my 2019 macbook pro. Definitely well under a second.
- tracker1 3y agoTake a look at Biome formatter and linter. Blink of an eye fast on very large projects that prettier feels like forever on. Just don't enable the experimental or potentially breaking format options on a large existing code base. They aren't enabled by default. Deno's in the box trolling is pretty nice as well. YMMV of course.
- ivanjermakov 3y agoI migrated to Biomejs formatting and never looked back.
- herpdyderp 3y agoNo HTML support… bummer. I’ve been looking to switch from Prettier but I haven’t yet found a viable alternative.
- taskylizard 3y agoYou might like dprint.
- conaclos 3y agoThe Biome team is currently working on CSS support. HTML is the next item on the list.
- 3y ago
- richev 3y agoSkimming past the capital "P", I read this as "why is prettier rock, solid" . . . and was left perplexed, as most rocks are solid.
- wellthisisgreat 3y agoYeah, I thought, liquid rocks aren’t pretty at all
- Lerc 3y agoThe prettier ones do tend to be also the harder ones though.
- deleted 3y ago[deleted]
- rplnt 3y agoYeah, and some people intentionally do cause these types of confusions by using title case.
- bluSCALE4 3y agoThe debate for me is now eslint —-fix or prettier. I don’t get why people need more “control”. If it breaks code, then sure, I understand the reservation but in 99% of cases, it just, “I don’t like how it breaks up my code”. Why care? At least, if you’re all using it, the diffs will be predictable and PRs can focus on what matters. //rant
- crooked-v 3y agoThe answer is "both". Use ESLint for actual fixes and Prettier for formatting.
- treve 3y agoI can't remember the last time PR comments addressed minor formatting issues at all, in any language. If something is hard to grok it usually has to do with logic, not if there's a space between `if` and `(`.
- bluSCALE4 3y agoYou're right, I more meant that if you don't format, your code sometimes becomes difficult to read and you become disincentivized to fix anything because git blame will point at you. I run into this all the time when people tell me, don't format the code because we won't know who did what. I get this even from senior devs as if using git blame and history is rocket science. If everyone stops bickering and just formats, all this noise goes away.
- 8n4vidtmkvmk 3y agoIf that's really an issue just reformat the whole code base once and be done with it.
- rashkov 3y agoYep, plus you can always do a git blame of the commit just before this happens. For example git blame a1b2c4 -- foo.js Where a1b2c4 is the commit hash. Could even tag the commit to make it more convenient: git tag beforeRefmt a1b2c4 git blame beforeRefmt —- foo.js and push the tag up for everyone to have access to it git push —-tags
- deepfriedrice 3y agoOff topic but every once and a while I'm made aware of the impact prettier has had on my typing. It's so hard to write code without a tool like prettier because formatting related keystrokes have largely been removed from my muscle memory. You basically end up writing a sort of shorthand.
- thomasfromcdnjs 3y agoThis + Copilot. I code in an entirely different way now, I would of thought it impossible to change my ways (about 20 years of coding)
- SSLy 3y agohave*
- deepfriedrice 3y agoYes I just started dabbling with copilot and I feel much the same as when I began really leveraging prettier. I’m similarly surprised to see such an ingrained skill undergo such rapid change. And it’s funny because for me prettier was originally just meant to fix the chaos that is every dev having their own style/editor preferences.
- boredtofears 3y agoI think I probably suffer from the same thing. On top of that, I added a VS macro that adds missing imports, cleans up unused ones, and re-orders them in a single keystroke. I've never been lazier.
- nonethewiser 3y agoYeah it’s true. Its not simply that you don’t have to manually format things a certain way. You simply don’t format things at all. You can avoid writing lots of spaces, newlines, semi colons, etc. it’s absolute garbage, then you save it, then it’s fine.
- matsemann 3y ago
- lemme_tell_ya 3y agoI picked up standard[1] a while back for this reason, I don't want to have to think about it. It works fine, I have no complaints (took me a while to get used to not using semi-colons but now I prefer it) Same reason I use `cargo fmt` as well. [1] https://standardjs.com/ https://standardjs.com/
- jeswin 3y agoIn JavaScript (and in TypeScript), not using the semi-colon is often a footgun. Here's a surprise: const a = 1 const b = 2 (a+b).toString() Calling the convention "standard" was irresponsible, and it led many people (especially beginners) to assume that it was some sort of a preferred convention. Glad I'm seeing it less these days; thanks to TypeScript, and people following Microsoft's conventions.
- WorldMaker 3y agoThere's only one "extra" rule to remember when not using semicolons: the winky frown rule. If it starts with a frown (, [, ` then it must wink: ;(, ;[, ;` It's a silly sounding rule, but that makes it easy to remember. The rest of Automatic Semicolon Insertion footguns happen to everyone whether they use semicolons or not, such as: do not add a newline after return, that makes it a return undefined. Semicolons won't help. (Typescript return type annotations will help.) Related to the topic at hand, prettier configured with `{ "semi": false }` (which I prefer to standard these days because I think prettier autoformatting is, uh, prettier) in my experience catches most winky frown violations for you and auto-fixes them.
- rlt 3y agoThe hubris of naming it “standard” when it is not is a bit of a turn off.
- OccamsMirror 3y agoFake it 'til you make it.
- 3y ago
- bottlepalm 3y agoPrettier is a god send for keeping JSX/TSX consistent and readable. Never really 'needed' a formatter until then.
- LectronPusher 3y agoI initially took the title as asking why diamonds or sapphire are harder than uglier rocks. Guess I'll need to find that link myself ;)
- stavros 3y agoSame here, it really threw me off that rock-solid isn't hyphenated.
- brailsafe 3y agoMeh, people seem to have an inflated view of Prettier. I find that it's at best ok, but has produced enough frustrating errors that I just turn it off.
- pjerem 3y agoPrettier real usefulness is in team projects. At $PREVIOUS_WORKPLACE, it totally removed the burden of formatting issues in PRs. It’s far from perfect but at least everyone in the team was producing the same style and we could focus on logic errors rather than formatting (which is the first thing you spot and can’t ignore when doing a code review).
- brailsafe 3y agoFormatting errors are at best an incidental detail that nobody should be wasting time on regardless of where it pops up; if prettier helps, great, but in its absence, I don't believe a trivial formatting inconsistency should illicit more than a tiny note among other problems you'd ask to be fixed. If the formatting errors are so frequent and so glaring as to warrant a negative review on that basis alone, such that you feel a mediator is required, then I'd wager Prettier may be obscuring other communication deficiencies. That said, I do believe in the value of consistent formatting tools, I just think Prettier is a somewhat lazy choice that attempts to skirt responsibility for deciding on things or cultivating agreement. It's a disfunctional team who's members care too much about their opinions on such trite topics, and/or who aren't capable of coming to agreement on a common set of them. For PRs, I've always just asked someone to run their formatter if it's clear they forgot, and usually there's a common config used by some formatting tool, it's not worth much beyond a a one-liner to make these decisions, and you should be able to do this periodically as things change.
- ravenstine 3y agoFormatting is extremely overrated. It's important, don't get me wrong, but developers today seem to have this fetish for having a bunch of formatting rules on top of a litany of linter rules, as if the number of problems solved scales linearly with the amount of guard rails added. A level of it is useful, but so much of it is a waste of time and can lead to idiotic situations where things conflict or override one another. Not to mention time wasted bikeshedding this stuff. Prettier, to me, is about as average as these tools come. It's not particularly offensive, though I don't understand why it's not way faster than it is.
- ramenmeal 3y ago> note that gofmt doesn't indent, which is the hard part interesting because for me it's hard to read changelogs of js/ts based repos that are using prettier compared to go repos, I wonder if the indentation is a reason for that. I'm honestly not sure what the author means by that either, gofmt seems to fix indentation for me.
- andrewstuart2 3y agoYeah, gofmt does and always has fixed indentation (by tabs, thus avoiding another pointless conversation on teams using it).
- dochne 3y agoIf you're reading a PR diff on GitHub you can get it to ignore whitespace diffs by adding ?w=1 to the url - complete lifesaver in this kind of situation.
- chatmasta 3y agoAlso, the white space diffs shouldn't be significant as long as all the files were already properly formatted before the change. If your diffs are including unrelated formatting changes, then you should do a single commit to format all your files. You can also use git-ignore-revs so that this formatting commit won't show up in git blame.
- mgraczyk 3y agoInterestingly, prettier just made a breaking change in a patch release and refused to undo it for a week or so, until a particularly silly pedantic argument won them over. https://github.com/prettier/prettier/issues/15942 https://github.com/prettier/prettier/issues/15942 My only bad experience with prettier, besides the incredible slowness (orders of magnitude slower than ruff)
- oreilles 3y agoHow can a person that introduces breaking changes in patch release and then just says everyone to piss off when facing critics (just because mister "has needs for this": https://github.com/prettier/prettier/issues/15553#issuecomment-1783252676 https://github.com/prettier/prettier/issues/15553#issuecomme...) be a maintainer of such a widespread tool ?
- PufPufPuf 3y agoBecause it's an unpaid job and the only required qualification is willing to do it.
- madeofpalk 3y agoWhat's wrong with having a need for a feature and wanting it implemented?
- conaclos 3y ago> My only bad experience with prettier, besides the incredible slowness (orders of magnitude slower than ruff) Ruff is based on the same foundations that Biome (https://biomejs.dev/ https://biomejs.dev/). Although Biome doesn't support all languages that Prettier supports, you should give a try, it is fast.
- donatj 3y agoI don’t see this as a breaking change. Period. The tsconfig.json files tsc itself generates and parse are definitionally JSONC. TypeScript config files are JSONC. TypeScript itself via `tsc --init` creates a file absolutely loaded with comments. It’s clearly the other tools that can’t handle JSONC that are the broken part. They can’t handle configs that typescript itself generates. The contract for “breaking” here isn’t between the tool and every single poorly written tool in existence. It’s between Prettier and the config file, and it’s correctly encoding the config file.
- nsonha 3y agoI have never seen a formatter talked about this much, it is just a formatter and code is not poem, Jesus.
- richev 3y agoBut there's an art to writing great code, no?
- omnicognate 3y ago> But in terms of functionality, it is solid. So far, it has never broken my JS / TS code. This is the criterion for "rock solid"? I don't think a formatter has ever broken my code (in any language) and I'd be absolutely livid if one did.