Live data from Hacker News

A Prettier JavaScript Formatter

jlongster.com

81–90 of 159 posts

Re: A Prettier JavaScript Formatter

#81
https://github.com/facebook/pfff is a tool which support pretty printing a bunch of different languages (currently c, c++, java, js, php, python, rust, and a bunch more).

The idea is to have a common AST (with parsers for each language) which keeps track of whitespace/comments/etc and then share a ton of logic by writing writing code at the AST layer.

You can use pfff to pretty print according to your style needs. Also supports a bunch of other neat things such as search/replace.

Re: A Prettier JavaScript Formatter

#82
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?

I attempted to use clang-format on a mixed typescript/tsx codebase and found that it mangled jsx expressions. This wasn't surprising but I suspect that typescript users probably overlap heavily with react users so it's not a great solution as is.

Re: A Prettier JavaScript Formatter

#83

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

If you're using typescript and emacs, https://github.com/ananthakumaran/tide is a very good integration with the built-in language server for semantic auto-completion and "jump-to" support. I use it every day on large projects.

I think that vscode uses the typescript language server behind-the-scenes for plain javascript analysis.

Re: A Prettier JavaScript Formatter

#84

After using Elm and the elm-format package in Emacs for the last few weeks I can say that not having to format the source and just letting your editor do it for you on save or via a command is so very nice. This is a trend that I feel is going to catch on across any language that can support it. It makes trivial decisions and arguments about formatting a thing of the past.

Gofmt was the catalyst for me. I do it with StandardJS in Visual Studio Code on save automatically, I won't waste my time manually formatting code - or caring about it - again.

Personal code formatting preferences is a problem. Devs should just let go of it, once it applies automatically your preferences quickly change.

You can even wire up GitHub etc repos to automatically reject pull requests that add pointless formatting preferences someone will pointedly defend.

Re: A Prettier JavaScript Formatter

#85

After using Elm and the elm-format package in Emacs for the last few weeks I can say that not having to format the source and just letting your editor do it for you on save or via a command is so very nice. This is a trend that I feel is going to catch on across any language that can support it. It makes trivial decisions and arguments about formatting a thing of the past.

It's almost crazy to think this already isn't just a thing. Why is this just coming to a head now, after all these years of carrying on over code formatting?

I'm not convinced it is just coming to a head now. I've seen automatic/forced formatting on projects at least as far back as 2000, when I entered the workforce.

Re: A Prettier JavaScript Formatter

#86
> Many of you know that I usually don't use JSX when writing React code. Over a month ago I wanted to try it out, and I realized one of the things holding me back was poor JSX support in Emacs.

huh? I've been using emacs with WebMode + tide + FlyCheck and it supports jsx just fine. Moreover, tide[1] provides great support for plain javascript (I've used it extensively on a ES6 codebase with great results).

[1]: https://github.com/ananthakumaran/tide

Re: A Prettier JavaScript Formatter

#87

Earlier quoted context omitted.

It's almost crazy to think this already isn't just a thing. Why is this just coming to a head now, after all these years of carrying on over code formatting?

I'm not convinced it is just coming to a head now. I've seen automatic/forced formatting on projects at least as far back as 2000, when I entered the workforce.

Yes, forced/automated based on that company standards. I'm referring more to universal.

Re: A Prettier JavaScript Formatter

#88
If I personally prefer the second Promise chain:

    myPromise.then(() => {
      // ...
    }).then(() => {
      // ...
    }).catch(() => {
      // ..
    });
and if I also prefer the Lisp-like approach and hate putting:

    );
on its own line, am I part of the problem? What if my whole team uses that style? For example, we would write:

    foo(
      reallyLongArg(),
      omgSoManyParameters(),
      IShouldRefactorThis(),
      isThereSeriouslyAnotherOne()
    );
as:

    foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(),
            isThereSeriouslyAnotherOne());
(100 characters wide, double indent on the continuation line. This is pretty standard for Java formatting.)

Stated differently, is this meant to be very opinionated in hopes that all JS would follow a uniform style? I think that can work for Go since it was like that from Day 1. For JS, though, it has been out for so long that many teams have developed their own preferred style. I suspect that most of them would avoid an opinionated formatter that differs in a few small ways from their in-house format, if only because it seems silly to break diffs and git blame for a sweeping formatting change.

Re: A Prettier JavaScript Formatter

#89

If I personally prefer the second Promise chain: myPromise.then(() => { // ... }).then(() => { // ... }).catch(() => { // .. }); and if I also prefer the Lisp-like approach and hate putting: ); on its own line, am I part of the problem? What if my whole team uses that style? For example, we would write: foo( reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne() ); as: foo(reallyL…

I avoid opinionated lint rules because it's annoying to retrain yourself, but if my editor does all the work and it's readable enough I'd love to adopt this or something like it.

The version control issues might be a blocker though. It would be neat if git had a way to do diffs at the syntax tree level.

Re: A Prettier JavaScript Formatter

#90

If I personally prefer the second Promise chain: myPromise.then(() => { // ... }).then(() => { // ... }).catch(() => { // .. }); and if I also prefer the Lisp-like approach and hate putting: ); on its own line, am I part of the problem? What if my whole team uses that style? For example, we would write: foo( reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne() ); as: foo(reallyL…

I avoid opinionated lint rules because it's annoying to retrain yourself, but if my editor does all the work and it's readable enough I'd love to adopt this or something like it. The version control issues might be a blocker though. It would be neat if git had a way to do diffs at the syntax tree level.

Would using the git diff with whitespace ignored not help?
Post reply on HN