News for hackers. Stuff that matters.
Re:A Prettier JavaScript Formatter by tomp on Tuesday January 10, 2017 @11:27AM (#13366016)
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 by jlongster on Tuesday January 10, 2017 @11:36AM (#13366115)
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 by artursapek on Tuesday January 10, 2017 @11:55AM (#13366297)
Don't let HN negativity get you down. This is an awesome project and I'll contribute bug fixes/test cases if we end up adopting it on our team (very likely based on your blog post).
Re:A Prettier JavaScript Formatter by mi_lk on Tuesday January 10, 2017 @12:07PM (#13366461)
the parent gave an ill example and suggest a solution, how's that a negativity?
Re:A Prettier JavaScript Formatter by dangoor on Tuesday January 10, 2017 @12:35PM (#13366791)
I believe it was the "Nice attempt, but currently wrong." at the start of the comment. "Nice attempt" could imply that this should be thrown out in favor of another attempt to solve the problem.
Re:A Prettier JavaScript Formatter by n42 on Tuesday January 10, 2017 @01:46PM (#13367637)
The entire comment was patronizing. "Nice attempt" minimizes the effort involved and implies the author is a novice. "but currently wrong" categorizes the entire project as a failure because of a single bug. The followup "From my own experience writing a formatter" really drives the whole thing home.
Re:A Prettier JavaScript Formatter by artursapek on Tuesday January 10, 2017 @01:58PM (#13367775)
Classic HN :-)
Re:A Prettier JavaScript Formatter by tomp on Tuesday January 10, 2017 @03:12PM (#13368501)
Wasn't meant that way.
Re:A Prettier JavaScript Formatter by softawre on Tuesday January 10, 2017 @05:18PM (#13369713)
"Nice attempt" used to actually mean the words it said. You know, like "nice job".
Re:A Prettier JavaScript Formatter by acdha on Tuesday January 10, 2017 @12:48PM (#13366965)
Also, “currently wrong” is a really sweeping statement to make based on a single problem written in a fairly uncommon style. Yes, there are things which need to be fixed but that doesn't mean we have to be so dismissive of someone else's work which they've given away for free to the community.
Re:A Prettier JavaScript Formatter by artursapek on Tuesday January 10, 2017 @01:28PM (#13367439)
Like dangoor said, it was the way it was delivered.
Re:A Prettier JavaScript Formatter by moogleii on Tuesday January 10, 2017 @11:38AM (#13366135)
I'm actually very excited by your "attempt". Looking forward to where it goes!
Re:A Prettier JavaScript Formatter by mnarayan01 on Tuesday January 10, 2017 @01:36PM (#13367527)
I think what the GP is saying is to do the re-parsing before/after every time you reformat (and then presumably error exit if the ASTs don't match). When/if you no longer have any known bugs like that you might want to remove it, but it does seem like a good idea until you get there.
Re:A Prettier JavaScript Formatter by tlrobinson on Tuesday January 10, 2017 @01:24PM (#13367381)
Running existing codebases through it and comparing the ASTs is a nice idea. Seems like it should be pretty easy to automated it for the top N (100?) JavaScript projects.
Re:A Prettier JavaScript Formatter by masterj on Tuesday January 10, 2017 @11:33AM (#13366085)
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 by gopher_protocol on Tuesday January 10, 2017 @02:44PM (#13368227)
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 by dgreensp on Wednesday January 11, 2017 @12:17AM (#13371943)
I used Recast on a codebase at work to migrate to a new code style, and I ran into a long tail of issues with preserving comments that looked like they would require some significant work to fix. I know the author of Recast (Hi Ben) and corresponded with him a little bit about it at the time.
Re:A Prettier JavaScript Formatter by matthuggins on Tuesday January 10, 2017 @02:58PM (#13368370)
It also is not preserving JSX spacing correctly, and the example the site provides even shows that:

    JSX is<strong>supported</strong>
Note the lack of space after the word "is".
Re:A Prettier JavaScript Formatter by pg_bot on Tuesday January 10, 2017 @11:46PM (#13371821)
I know this isn't really practical, but as a rule you shouldn't mix comments and code. Comments should be above any function definitions for this and many other obvious reasons. Removing inline comments seems like a reasonable default for a project like this, since those adopting it have already ceded some amount of control over the way their code is structured.
Re:A Prettier JavaScript Formatter by imh on Tuesday January 10, 2017 @12:56PM (#13367050)
I don't do much serious JS. What's the problem there?
Re:A Prettier JavaScript Formatter by k__ on Tuesday January 10, 2017 @01:27PM (#13367422)
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 you use semicolons.
Re:A Prettier JavaScript Formatter by dustinswan on Tuesday January 10, 2017 @12:59PM (#13367091)
The first one returns an object, the second one returns undefined