Live data from Hacker News

A Prettier JavaScript Formatter

jlongster.com

11–20 of 159 posts

Re: A Prettier JavaScript Formatter

#11
Fantastic. One thing I've learned that's incredibly important is code style has to be uniform across all engineers. Seems like an unimportant point to devs that haven't worked with teams that enforce it, but it really is.

As was once told to me, my code should look like your code and yours like mine.

Re: A Prettier JavaScript Formatter

#12

> If computers are good at anything, they are good at parsing code and analyzing it. So I set out to make this work, and prettier was born. I didn't want to start from scratch, so it's a fork of recast's printer with the internals rewritten to use Wadler's algorithm from "A prettier printer". Bob Nystrom (munificent) disagrees[0] after writing one himself: > The search space we have to cover is exponentially large, a…

That's a great article, it really digs into some of the complex issues

Re: A Prettier JavaScript Formatter

#13
post #4

How does this compare to clang-format, which also formats JavaScript and also (to use one of the features highlighted in the article) obeys line length?

Not sure, didn't know it formatted JavaScript! This is written in JS at least so if anything it's more approachable to the community.

Re: A Prettier JavaScript Formatter

#14
Nice attempt, but currently wrong. For example, it pretty-formats this code:

  function makeComponent() {
    return /*test*/ { a: 1
    };
  }
into

  function makeComponent() {
    return /*test*/
    { a: 1 };
  }
From my own experience writing a formatter, comments are very difficult (if one wants to preserve them). I guess the author should reparse the generated code to ensure the AST hasn't changed.

Re: A Prettier JavaScript Formatter

#15
I appreciate the emacs bindings [0]!

Between this and eslint+flycheck I will feel less envious of Visual Studio Code's editor environment (if only there were a way to leverage some of its fantastic IntelliSense from emacs...)

[0] https://github.com/jlongster/prettier/tree/master/editors/em...

Re: A Prettier JavaScript Formatter

#16
post #14

Nice attempt, but currently wrong. For example, it pretty-formats this code: function makeComponent() { return /*test*/ { a: 1 }; } into function makeComponent() { return /*test*/ { a: 1 }; } From my own experience writing a formatter, comments are very difficult (if one wants to preserve them). I guess the author should reparse the generated code to ensure the AST hasn't changed.

While understanding the state of a project is important for potential adopters, might I suggest filing a GitHub issue in lieu of this comment as something that might actually have a positive outcome?

Re: A Prettier JavaScript Formatter

#17
It seems a pretty format can be subjective depending on personal preference and code complexity. I'm surprised that there aren't and formatted that are customizable in that you break down the format of a function into a few sections and give the user options on how they would best prefer the format to look. (At least there's not a formatter like that which I'm aware of)

Re: A Prettier JavaScript Formatter

#18
post #14

Nice attempt, but currently wrong. For example, it pretty-formats this code: function makeComponent() { return /*test*/ { a: 1 }; } into function makeComponent() { return /*test*/ { a: 1 }; } From my own experience writing a formatter, comments are very difficult (if one wants to preserve them). I guess the author should reparse the generated code to ensure the AST hasn't changed.

We do. The entire Flow test suite passes when pretty-printing and re-parsing and comparing ASTs.

There are a very few subtle bugs like the one you mentioned above. As noted in the readme, this is a beta. It's certainly more than an attempt.

Re: A Prettier JavaScript Formatter

#19
post #14

Nice attempt, but currently wrong. For example, it pretty-formats this code: function makeComponent() { return /*test*/ { a: 1 }; } into function makeComponent() { return /*test*/ { a: 1 }; } From my own experience writing a formatter, comments are very difficult (if one wants to preserve them). I guess the author should reparse the generated code to ensure the AST hasn't changed.

We do. The entire Flow test suite passes when pretty-printing and re-parsing and comparing ASTs. There are a very few subtle bugs like the one you mentioned above. As noted in the readme, this is a beta. It's certainly more than an attempt.

I'm actually very excited by your "attempt". Looking forward to where it goes!
Post reply on HN