JavaScript equivalent of C indent
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 li开发者_如何学Cke Windows-only utilities or online tools.
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.)
You have a few options:
- jsbeautifier
- Google Closure Linter
- Google Closure Compiler Service (w/ pretty printing option)
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):
- Best Source Code Formatter for JavaScript
- Formatting JavaScript source code
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 (No, apparently it's just a web page front-end to indent
itself to understand JavaScript. The joy of open source! Even if incomplete, the syntaxes are so similar...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...
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.
libjavascript-beautifier-perl
package has got /usr/bin/js_beautify
, which does the trick for me.
精彩评论