JavaScript reformatter for Linux?

J

Jonathan Kamens

OK, so I've searched high and low for the answer to this
question. Perhaps there is an answer that I just couldn't
find because it was too hard to filter out the signal from
the noise in the search results.

I want a tool I can install on Linux that will reformat
JavaScript. I've found the javascript-mode that comes with
XEmacs and installed it with no trouble into my GNU Emacs
installation, and I can use that to reindent, but I can't use
that to format the code nicely if, e.g., all the extra
newlines and whitespace were removed to reduce download time
and obfuscate the code. I've also found that GNU indent can
"mostly" indent JavaScript, but it has problems with missing
semicolons, JavaScript string constants bounded by single
quotes instead of double quotes, and JavaScript regular
expressions operators.

So, is there something for Linux specifically designed to
take ugly JavaScript and make it pretty?

Thanks in advance for any help you can provide. And I
apologize if this question has been asked and answered; I
simply can't find the answer, despite looking pretty hard for
it.
 
L

Lasse Reichstein Nielsen

I want a tool I can install on Linux that will reformat
JavaScript.

Try Firefox :)
E.g., put this in a page:
---
<script type="text/javascript">
function prettyScript(code) {
try {
var fcode = (new Function(code)).toString();
fcode = fcode.replace(/^\s*function\s+(?:\w+\(\)\s*)?\s*\{\s*\n/, "");
fcode = fcode.replace(/\s*}\s*$/, "");
return fcode;
} catch (e) {
return "SYNTAX ERROR: " + e.message;
}
}
</script>
<form action=""
onsubmit="this.elements.output.value =
prettyScript(this.elements.input.value);
return false;">
<textarea id="input" cols="80" rows="5">"code to format here"</textarea><br>
<input type="submit" value="format"><br>
<textarea name="output" cols="80" rows="10" readonly="readonly">Output here</textarea><br>
</form>
---

Nothing beats a Javascript parser for understanding the syntax of
Javascript :)

/L
 
J

Jonathan Kamens

Thanks, that's cool, and I suppose I can use it if there are
no other options, but I'm sort of hoping for a CLI-based tool,
not something I have to use through a Web browser.
 
J

Jim Ley

Thanks, that's cool, and I suppose I can use it if there are
no other options, but I'm sort of hoping for a CLI-based tool,
not something I have to use through a Web browser.

The same code will run in any javascript command line interpreter too

Jim.
 
J

Jonathan Kamens

Danny said:
Define 'reformat', if you mean a code coloring tool like a syntax
hightlight and such, I use Bluefish

I'm not familiar with bluefish. What is it?

By "reformat", I mean not just syntax highlighting, but rather
inserting and removing line breaks, whitespace, indentation, etc. in
the code to make it more readable.
 
J

Jonathan Kamens

The same code will run in any javascript command line interpreter too

It also discards comments. For my needs, that's a fatal flaw in this
solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top