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.