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
A Prettier JavaScript Formatter
61–70 of 159 posts
Re: A Prettier JavaScript Formatter
#62After 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?
FWIW if you use vim, this plugin is great: https://github.com/google/vim-codefmt
Re: A Prettier JavaScript Formatter
#63Earlier 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…
Re: A Prettier JavaScript Formatter
#64Earlier 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…
Re: A Prettier JavaScript Formatter
#65Re: A Prettier JavaScript Formatter
#66Nice 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.
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
#67Earlier 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.
Re: A Prettier JavaScript Formatter
#68After 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.
Re: A Prettier JavaScript Formatter
#69We'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
#70This 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.
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(),
++];