My blog has been moved to ariya.ofilabs.com.

Monday, September 20, 2010

offline, command-line beautifier for JavaScript code

There are many different ways to autoformat JavaScript code, my favorite is always jsbeautifier.org. Apparently, you can also use it locally without a web browser since it supports running it with Rhino.

It took me just few minutes to reimplement the same functionality, but using Qt Script, the fantastic ECMAScript support built into Qt.

The code is in the usual X2 repository, under the javascript/jsbeautify. Note that this is just a convenient mirror as Einar's js-beautify repository already includes this Qt Script driver.

With the rise of server-side JavaScript and desktop-esque JavaScript tools, I hope Qt Script will become more popular. It is sadly still "underrated" right now.

6 comments:

Anonymous said...

I guess you can give jslint the same treatment.
http://www.jslint.com/rhino/

It would be nice if one could pass parameters to the beautifier. It does not do wrapping to 80 characters, does it?

Ariya Hidayat said...

Anonymous: lint will be my next victim, along with command line options and editor integration (see http://bit.ly/x2-jsedit)

Anonymous said...

Do you know about the javascript javascript editor?

http://codemirror.net/jstest.html

Ariya Hidayat said...

Anonymous: yes, I am aware of that. Why?

Anonymous said...

It seems that it would make sense to write the entire javascript editor in javascript. That way it is easy to deploy and make it possible for people to help out with code by visiting your website.

Anonymous said...

Well, that command line utility is not very usable as of now since it requires you to pipe the result into a new file. Maybe you should add a few commandline options to make it somewhat behave like astyle. I've slightly modified the code so it makes a backup of the original (by appending .orig to the old filename) and saves the new formatted version to the original file. It will skip files that are not modified and will give up on files that already have a *.orig backup file unless you pass --overwrite to it on the commandline. It's a dirty hack that I needed to get it to work, so it would be nice if you could add that functionality in a cleaner way.

Here is the source for those of you who might be interested:
http://www.pastebin.ca/1958967