
Jsfmt – Like Gofmt, but for JavaScript - jimfleming
https://github.com/rdio/jsfmt
======
mwsherman
The key here is adoption. Go’s adoption of gofmt is that it was there from the
start and the community has adopted it.

It’d be great to have it on the JS side too. It’s a question of everyone
agreeing that it’s the right one and achieving critical mass.

~~~
sambeau
I think you might be missing the point here. gofmt doesn't only enforce a
style guide, it has allowed the Go team to change Go's syntax and
automatically update everyone's code.

This tool, to me, looks really useful for updating old javascript codebases -
removing old library calls and polyfills — bringing in new native techniques.

~~~
ihsw
> it has allowed the Go team to change Go's syntax and automatically update
> everyone's code

That's go fix.

~~~
dsymonds
Gofix does the work, but it is only feasible because gofmt exists.

------
arthurdenture
The clang-format tool from the LLVM project can also format Javascript. It's
similarly AST-aware.

Reference:
[http://clang.llvm.org/docs/ClangFormatStyleOptions.html](http://clang.llvm.org/docs/ClangFormatStyleOptions.html),
heading "Language".

------
GeZe
For a tool dedicated to searching and replacing JS in this way (AST aware),
check out [http://graspjs.com](http://graspjs.com)

You can use both CSS style selectors, and pattern matching, to both search and
replace your code.

------
masklinn
> js = jsfmt.rewrite(js, "_.each(a, b) -> a.forEach(b)");

Of course _.each handles objects (not just arrays) and returns its input
(where Array#forEach returns undefined), and there does not seem to be any
type-based conditions (or otherwise) so you've got a souped-up sed, not
something you can add to any sort of automated workflow.

~~~
placeybordeaux
As far as I understand go fmt works on the AST, if your reformatter doesn't
work on the AST then you will eventually have problems.

Also go fmt doesn't ever change your code, thats what go fix is for.

~~~
moonlighter
gofmt -r applies rewrite rules, and -s also performs source transformations;
both change the code.

~~~
NateDad
gofmt without flags does not change your code. Yes, you can ask it to change
your code if you want it to, but by default, it won't.

------
shurcooL
This is neat. I've created something equivalent for Markdown, and decided to
post it today:

[https://news.ycombinator.com/item?id=7724159](https://news.ycombinator.com/item?id=7724159)

After getting so used to gofmt-on-save (err, rather goimports-on-save now), I
find it very odd when I edit other file types and nothing happens to the
obvious mis-formatting when I save. So I'm a big supporter of any new formats
coming out in the future to come with some sort of similar automated formatter
tool.

One important aspect that allows gofmt to succeed with effectively full
adoption is that it still allows for (limited, but nice looking) custom
formatting. If you want an extra newline between two lines, it will be
preserved. It gives enough freedom that always having it on is quite
acceptable.

------
JacksonGariety
This is really interesting. But wouldn't it work better as an Emacs or Vim
plugin?

It ought to work at the editor level, and would be really easy to make this
work with Emacs with a little lisp magic and the Emacs js interpreter.

~~~
awda
This is an equally valid criticism of `gofmt`. I think the rational is two-
fold: you aren't tied to any one editor to use the formatting tool, and the
authors of the fmt tool may not know enough (or care to know enough) about the
editor to integrate it.

~~~
saraid216
UNIX philosophy. Do one thing right. Editor integration is a second thing.

~~~
sinkasapa
Some of the go developers are also acme users so they get both. The Plan9
philosophy, I guess.

------
prezjordan

         /**
          * Sets the light position for drawing.
          */
        -Isomer.prototype.setLightPosition = function (x, y, z) {
        +Isomer.prototype.setLightPosition = function(x, y, z) {
    
        ...
    
           var yMap = new Point(point.y * this.scale * Math.cos(Math.PI - this.angle),
        -                    point.y * this.scale * Math.sin(Math.PI - this.angle));
        +  point.y * this.scale * Math.sin(Math.PI - this.angle));
    

No thanks :( In all seriousness, cool library. What standard does it follow?
Can't seem to find that anywhere.

~~~
jimfleming
Currently, its based on esformatter[0]. Most of the work so far has gone into
search and replace and we don't yet expose many of the properties provided by
esformatter[1].

[0]
[https://github.com/millermedeiros/esformatter](https://github.com/millermedeiros/esformatter)
[1]
[https://github.com/millermedeiros/esformatter/blob/master/li...](https://github.com/millermedeiros/esformatter/blob/master/lib/preset/default.json)

------
zackmorris
I really miss being able to search comments or code in Metrowerks Codewarrior.
I wish there was a plugin for that in Xcode (a formatter would also be nice,
its copy and paste is a little wonky for things like blocks and literals
shorthand).

------
moonlighter
A package for atom.io and a grunt-plugin would be nice.

~~~
anarchy8
Why don't you write one?

~~~
Pacabel
That's rarely a productive question to ask.

It's just a slight variation on the totally unhelpful "Why don't you fix the
bug yourself?" response that's sometimes given to people reporting a problem
with an open source project.

They're useless questions because the answer is pretty much irrelevant.

Maybe he doesn't know how. Maybe he isn't qualified. Maybe he doesn't have the
time. Maybe he doesn't have the money to fund somebody else doing it. Maybe he
just doesn't want to.

It doesn't matter why he might not do it himself. But that in no way means he
shouldn't publicly express such interest in such functionality, in case there
is somebody else who is willing or interested in adding it.

~~~
hl555
Well, while we're being pedantic, publicly expressing an "interest in such
functionality" deep in some HN thread is roughly as productive as asking him
directly why he doesn't scratch his own itch.

This whole notion that saying "patches welcome" is
insulting/dismissive/pointless is completely missing the point. Open Source
mostly runs on people _doing_ , not pontificating. It may be "unprofessional"
or "rude" in other contexts, but it's a fundamental part of what makes Open
Source work. I suspect you've never actually spent any considerable time
working on such a project, otherwise you'd most likely have seen things from
the other perspective and realized this already.

------
whatupdave
This is awesome. I want this for ruby.

------
f2f
+1 so hard my keyboard almost broke

Every language needs this except lisp and python :)

Note that the one trick gofmt has over say indent is that it truly understands
go syntax. It's not just a pretty printer

~~~
gbog
Serious question: why not python?

~~~
kibibu
No braces?

