Live data from Hacker News

Why is Prettier rock solid?

mrmr.io

141–150 of 163 posts

Re: Why is Prettier rock solid?

#141

Earlier quoted context omitted.

Yeah 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.

And autoformatters have cemented my preference for non-white spaced languages. As you say, just write whatever and let it format it. When I then switch to our python backend this strategy no longer works. Like, it can fix something, but it needs a much cleaner state to do so. For instance if my loops are indented wrong, black can't solve that as the decision it makes has a semantic meaning.

Something of a tangent: with Automatic Semicolon Insertion (ASI), JS is a white-space influenced language. Some of prettier's defaults are directly related to ASI, such as the way it often wraps things in extra ("unnecessary") parentheses, especially JSX but also anything complicated and multi-line especially after keywords like `return`. (`return` is the trickiest under ASI, so prettier's defaults seem that conservative in large part because of `return`.)

As someone who appreciates `{ "semi": false }` in my prettierrc, I take a lot of advantage of JS' ASI and find prettier's behavior interesting and conservative, but useful.

Re: Why is Prettier rock solid?

#142

Earlier quoted context omitted.

You can mellow the squiggles for stylistic errors in VS Code. Take a look at the “eslint.rules.customizations” key in this file: https://github.com/antfu/eslint-config/blob/main/.vscode/set...

Thank you! I had been wondering if that was possible. Do you know if it can be done only for a single project / directory?

You can set workspace settings in the top-level .vscode/settings.json file of your project: https://code.visualstudio.com/docs/getstarted/settings#_work...

Re: Why is Prettier rock solid?

#143
post #77

I 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/

In 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.

There'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.

Re: Why is Prettier rock solid?

#145

Off 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.

This + Copilot. I code in an entirely different way now, I would of thought it impossible to change my ways (about 20 years of coding)

Yes 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.

Re: Why is Prettier rock solid?

#146
post #137

Interestingly, 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 My only bad experience with prettier, besides the incredible slowness (orders of magnitude slower than ruff)

I 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 is…

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

Re: Why is Prettier rock solid?

#147
post #53

Earlier quoted context omitted.

Yeah, 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.

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 the "optimization" is line splitting.

The old IR grew organically over time and got increasingly difficult to work with. It baked certain formatting choices directly into the IR (mainly indentation) which line splitting then had no control over. For example, given a function call like:

    someLongFunctionName(some + long + argument + expression, [firstElement, anotherElement, aThirdElement]);
We might want to format it like this if the function name and first argument fits on one line:

    someLongFunctionName(some + long + argument + expression, [
      firstElement,
      anotherElement,
      aThirdElement
    ]);
But if the first argument doesn't fit, then we probably want:

    someLongFunctionName(
        some + long + argument + expression,
        [
          firstElement,
          anotherElement,
          aThirdElement
        ]);
Note how the indentation of the list elements depends on how we choose to line split the argument list. The old formatter's IR just couldn't model that at all.

For years, I've wanted a better IR that could express formatting like this. And since we were making sweeping changes to the formatting style (including some that would be very hard to implement with the old IR), it seemed like the right time to move to a new internal representation too.

> What do you think of the functional "pretty printing languages" like Wadler's (cited in the blog post)?

I have to confess that I worked on dartfmt for a few years before I stumbled onto that paper. I'm somewhat familiar with it, but I've never taken the time to really dig into it.

I could be wrong, but I strongly suspect that the formatting rules we want for Dart are too complex to model using Wadler's formalism directly, and I'm not sure if extending it to support the formatting rules we want would sacrifice its simplicity or performance.

Given that, I sort of stuck with the devil I already knew—dartfmt's current architecture—and built off of that.

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

Yes, and the two are deeply intertwined. The majority of dartfmt's code by line count is just implementing the style rules for every part of the language grammar. The most difficult code to write and maintain in dartfmt is the line splitting algorithm, largely because it's combinatorial when done naïvely.

Re: Why is Prettier rock solid?

#148
post #137

Earlier quoted context omitted.

I 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 is…

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 hardcoded metadata fields. That's not a breaking change on the JPEG libraries part. That's just a bug on the parsers part.

Re: Why is Prettier rock solid?

#149
post #137

Earlier quoted context omitted.

I 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 is…

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?

Re: Why is Prettier rock solid?

#150
post #117

I'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.

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

Post reply on HN