Jsfmt – Like Gofmt, but for JavaScript
github.com
github.com
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.
Given that, jsfmt doesn't need 100% adoption: using it in a single project or library would still be useful. Stating new contributions must be jsfmt'ed would be less trouble than most standard pull request requirements (pass + update any relevant testing code, ...).
I personally love these formatting tools and am looking forward to more of them, such as ClangFormat[1].
* You might be thinking of unused variables or imports resulting in compiler errors, which isn't the same as not compiling due to formatting?
This program won't compile: http://play.golang.org/p/8DSev4ZjWZ
func main() { }
vs
func main() { }
which produces the error "prog.go:4: syntax error: unexpected semicolon or newline before {". As I understand it this is because of the rules of where it will insert semicolons. It isn't enforcing everything about the format which is why I said "to a degree" but it is still one of the most opinionated compilers on format that I know of.
Either way, I'm glad they did it. The "where should the curly brace go" is one of the most annoying bike-sheds.
func main()
{
}This tool, to me, looks really useful for updating old javascript codebases - removing old library calls and polyfills — bringing in new native techniques.
That's go fix.
My prediction is that since this is "go-flavoured", it will face some (unwarranted) adoption resistance amongst influential JS devs. This tool might attract new devs into the code manipulation space and they'll eventually converge on to the tool with strongest community support.
Reference: http://clang.llvm.org/docs/ClangFormatStyleOptions.html, heading "Language".
You can use both CSS style selectors, and pattern matching, to both search and replace your code.
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.
Also go fmt doesn't ever change your code, thats what go fix is for.
JsFmt also works on the AST. Working on the AST does not mean your rewrite rules can be bulletproof, let alone are.
> Javascript formatters exist but most (all?) work on just strings, not the AST. Using Esprima under the hood we have access to the full AST and can do useful things like intelligent find and replace as in gofmt.
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.
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.
From my vimrc:
> au FileType javascript setlocal equalprg=/usr/local/share/npm/bin/js-beautify\ -f\ -\ -q\ -t\ -j\ -w\ 140\ --good-stuff\ -b\ \"end-expand\"
edit: The same event (BufWritePre) is what I've got set for go fmt too. That IS a plugin, but it makes no significant difference as compared to js-beautify.
https://github.com/mooz/js2-mode/blob/master/js2-mode.el
11k amazing loc
One of my favorite things installed on my computer.
/**
* 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.[0] https://github.com/millermedeiros/esformatter [1] https://github.com/millermedeiros/esformatter/blob/master/li...
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.
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.
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