4

There is a GNU indent for styling C sources, are there any equivalents for JavaScript?

I want it to have options for coding style, indention style, etc. just like indent.

I will be using it for a pretty large project on *NIX platform, so I will not like Windows-only utilities or online tools.

Ryan Li
  • 9,020
  • 7
  • 33
  • 62

5 Answers5

2

I haven't used it, but I bookmarked js-beautify a while back, and a quick check seems to indicate it might satisfy your requirements. (And, being written in JavaScript, you probably can improve on it easily.)

  • Thanks for the answer, but the command line tool requires a V8 dependency, and that's pretty huge… Something built on top of `indent` as Crowder mentioned would of course be better. – Ryan Li Dec 11 '10 at 12:04
  • @ryanli: It doesn't require V8 (and V8 isn't a huge dependency; it's trivial to add to a \*nix system -- I was surprised at just how easy it was). You *can* use V8 to run it from the command line, or Rhino (larger dependency in my book). – T.J. Crowder Dec 11 '10 at 12:07
  • V8 itself is about 10 times the size of `indent`, but I will check it out anyway, as it seems that it has no competitors here. – Ryan Li Dec 11 '10 at 12:10
  • @ryanli: Oh no question, it's a bigger hammer than `indent` (it's a full JavaScript implementation). On my system, the compiled v8 library is 142X the size of the `indent` program. But at just shy of 10M, it's not going to break my storage device, and it lets me do some fun things. :-) – T.J. Crowder Dec 11 '10 at 13:09
2

You have a few options:

Some editors also do this for you:

  • Emacs
  • vi(m)
  • Eclipse
  • ...

EDITS:

  • Another online tool, JavaScript Indent claims it uses Indent in the background itself. Maybe the author would share the settings. Not so great, but a start.
  • the JS Beautifier page also mentions that a Python version if being developed as we speak. Let's check again later. In the meantime you can use it with Rhino as mentioned on their page.

Finally, there are already a few SO questions answering this which you can find by searching (some of the results point to formatters to display code online, which is a different thing, though you could use that as well):

Community
  • 1
  • 1
haylem
  • 22,460
  • 3
  • 67
  • 96
  • Thanks for the answer. I have performed several searches before, but most results aren't satisfying. Take lint for example, it's mainly for Windows, and there are several other tools that are written in JavaScript. I would prefer the ones written in C. – Ryan Li Dec 11 '10 at 12:21
  • How does the Closure Linter help with pretty-printing? I mean, sure, there's the `fixjsstyle` command, but you have virtually no control over it. And the compiler service is, of course, an online tool -- ryanli asked specifically for offline options. – T.J. Crowder Dec 11 '10 at 12:30
  • @T.J. Crowder: True, you don't have much control over them, so if you cannot find something that has the right set of configurable options, maybe you can find one that does it the way you like. Personally, I use Closure Linter and I edited its code to do what I want and not complain about things where I don't care. – haylem Dec 11 '10 at 12:36
  • @ryanli: I see your point, I am also dissatisfied with the general state of tooling for JavaScript. But have a look at Closure Linter. It's written in Python is portable across platforms. The code is very readable and easy to modify. It's a shame it doesn't come with options really, but I think that's probably on purpose as they really want to **enforce** a style and not let people get away with other things. But for your personal use, feel free to edit it. – haylem Dec 11 '10 at 12:37
  • +1 for editors that do it for you. @Ryan Li - vi is standard on linux, though admittedly harder than `indent` to run on multiple files... – David Tang Dec 11 '10 at 13:14
  • @emacs: personnally I use emacs 24 with the builtin javascript-mode (otherwise Yegge's js2-mode is nice as well). The built-in indenter for c-syntax based languages works fine for me. There's also good a tutorial to have a flymake-like compilation feedback. Then I'm happy :) – haylem Dec 11 '10 at 13:22
1

There's jsBeautifier, which can be run from the command line via Rhino in a Java-enabled environment or run via V8 (or a couple of others, check out the github page). Apparently they're working on a Python command-line version.

Apparently someone's made a start (at least) on updating indent itself to understand JavaScript. The joy of open source! Even if incomplete, the syntaxes are so similar... (No, apparently it's just a web page front-end to indent — not helpful at all.) So rephrasing: I wonder how hard it would be to modify indent to support JavaScript? The syntaxes are so similar...

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • The start was back in 2005. :-) – Ryan Li Dec 11 '10 at 12:04
  • @ryanli: I'd struck that bit out well before your comment, you must have been looking at a stale page -- looks like he didn't even modify `indent`, just did a front-end to it. Not useful. :-) – T.J. Crowder Dec 11 '10 at 12:06
  • I see, I type slow… It would be useful if someone could hack `indent` for JavaScript. But if not, I may have to try out js-beautifier, although building it is quite complex. – Ryan Li Dec 11 '10 at 12:08
0

libjavascript-beautifier-perl package has got /usr/bin/js_beautify, which does the trick for me.

domen
  • 1,819
  • 12
  • 19
0

There is Javascript Tidy, which uses the browser itself to reformat Javascript code. So the results depend on the browser you use.

You cannot determine the style, but once the Javascript has been reformatted to a standard format, it's easier to modify the sourcecode to match your own style.

bart
  • 7,640
  • 3
  • 33
  • 40