Live data from Hacker News

A Prettier JavaScript Formatter

jlongster.com

61–70 of 159 posts

Re: A Prettier JavaScript Formatter

#61
post #40
post #7

Much better than "Standard JS" and its choice of no semicolons. I know about "Semi Standard JS", but I refuse to accept such a little difference that could be a configuration has to live in a entirely new project.

But the linked formatter doesn't seem to have an option for no semicolons so I wouldn't call it better in that regard. EDIT: Looks like they may be open to it though. https://github.com/jlongster/prettier/issues/12

Semicolons are not optional in JavaScript: ASI (http://www.ecma-international.org/ecma-262/6.0/#sec-automati...) is an error correction scheme for novice programmers. The spec's parsing rules calls out the statements following where a semicolon should be "offending tokens". There is no leeway here for style or preference.

Re: A Prettier JavaScript Formatter

#62

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?

Many communities have been using their own solutions for it for a while (e.g pylint / pep8 if you've used Python, clang-format for C++, js-beautify for javascript, etc) and many companies use "linter as style enforcer" but you still need to watch out for some things that linters don't catch in most languages.

FWIW if you use vim, this plugin is great: https://github.com/google/vim-codefmt

Re: A Prettier JavaScript Formatter

#63
post #51
post #42

Earlier quoted context omitted.

I don't do much serious JS. What's the problem there?

JavaScript has automatic semicolon insertion (ASI). This will add semicolons all over your code depending on some rules, for example, if you write a return, and write the value you want to return in the next line, it adds a semicolon after the return AND after the value, effectively ignoring the value in the end. return myValue; becomes return; myValue; It's a bit meh, because you have to remember these rules even if…

[deleted]

Re: A Prettier JavaScript Formatter

#64
post #51
post #42

Earlier quoted context omitted.

I don't do much serious JS. What's the problem there?

JavaScript has automatic semicolon insertion (ASI). This will add semicolons all over your code depending on some rules, for example, if you write a return, and write the value you want to return in the next line, it adds a semicolon after the return AND after the value, effectively ignoring the value in the end. return myValue; becomes return; myValue; It's a bit meh, because you have to remember these rules even if…

[deleted]

Re: A Prettier JavaScript Formatter

#65
post #60

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 bet you heard of "go fmt".

Right, but that's a single relatively new language.

Re: A Prettier JavaScript Formatter

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

I've found another issue where it just removes comments entirely inside an otherwise empty block.

    function doSomething() : int {
      return myPromise
      .then(() => {
        // ...do thing
        /* what */
      })
      .catch(() => {
        // ..do other thing
        /* the heck */
      });
    }
becomes

    function doSomething(): int {
      return myPromise
        .then(() => {})
        .catch(() => {});
    }
Still a very nice pretty printer, though.

Re: A Prettier JavaScript Formatter

#67
post #40

Earlier quoted context omitted.

But the linked formatter doesn't seem to have an option for no semicolons so I wouldn't call it better in that regard. EDIT: Looks like they may be open to it though. https://github.com/jlongster/prettier/issues/12

Semicolons are not optional in JavaScript: ASI ( http://www.ecma-international.org/ecma-262/6.0/#sec-automati... ) is an error correction scheme for novice programmers. The spec's parsing rules calls out the statements following where a semicolon should be "offending tokens". There is no leeway here for style or preference.

The thing is, they made the "error correction scheme" work so well that they are no longer required. Give it a go sometime; you might like it.

Re: A Prettier JavaScript Formatter

#68

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.

i hate the output of elm-format, though :( it's very wasteful of vertical space.

Re: A Prettier JavaScript Formatter

#69
My team recently ganged up on me to tell me my line spacing was f*cked. Upon reflection, it seems that as I'm writing code I group lines into 'working well - one line, 'not sure - two lines', 'probably will change - lots of space', 'hey look at me! - off by itself'. Apparently, this unconcious invisible system drives others crazy. Developers are so picky! ;-)

We've actually got a bunch of automatic eslint rules, but it doesn't correct for this. Looking at the various beautifiers, I couldn't find anything that really cleaned up line spacing very well, and ended up running a bunch of sed commands to remove lines and then spacing out things manually in a "sane" way (so picky!!). Having a formatter that actually parsed the AST and rewrote the code like gofmt will be very handy if it works - I'll have to try it out.

Re: A Prettier JavaScript Formatter

#70
post #55

This is awesome, but one request: can you please extend it into a real (ie, AST based) diffing tool? Current diffing tools all compare the text (which is basically an input format) rather than the AST, and hence end up focusing on formatting rather than actual changes.

You could hack a diffing tool that reformats both the before and after and shows the diff of the generated text.

This would not catch changes like

    --val  x =  [1]
    ++var x = [
    ++  1,
    ++];
as expected, while it would give you a slightly more verbose diff if the change was

    --val x = [short(), short()];
    ++val x = [
    ++  a_really_long_name_that_pushes_the_line_length(),
    ++  a_really_long_name_that_pushes_the_line_length(),
    ++];
Post reply on HN