views:

67

answers:

3

I'm looking for some tool to remove cooments from Javascript sources. I was able to Google some, but none of them satisfied the following requirement: Everything else should be left as it is, in particular white space is not removed, BUT if a comment takes a whole line, the line is removed too.

Shortly, I want to be able to go from a nicely formatted source with comments to an equally formatted source without comments. Lines which only contain comments are removed, and traliing comments are removed together with the trailing spaces. All the rest is left as it is.

Do you know any tool for such a job?

EDIT: I try to be more specific. Using regular expressions is not possible, as the characters // or /* can also appear inside strings, regular expressions and so on.

The tool should take this input

var a = true;

//the following code is every useful
var b = 2;//really, really useful
 /**
Never, ever do this
var c = 3;
  */
var d = 4;

and give this output

var a = true;

var b = 2;
var d = 4;
A: 

Just do a regular expression find and replace.

  1. Regex starting with //, containing all symbols and whitespace, and ending with line break
  2. Regex starting with /*, containing all symbols, whitespace, and breaks, and ending with */
js1568
Won't work properly if strings or regexes contain those characters.
trinithis
That will also match instances of those inside strings and regex literals. Probably an edge case for most people, but still worth being aware of. You need something that can actually parse JavaScript to do it correctly in all cases.
Damian Edwards
Or rather, lex the code.
trinithis
+2  A: 

Here's some code I whipped up: http://trinithis.awardspace.com/commentStripper/stripper.html

Here's one I didn't write that could be handy, though his code will fail on certain regex literals: http://james.padolsey.com/javascript/removing-comments-in-javascript/

trinithis
It leaves a lot of white space instead of comments, like other tools that I have tried. I'd like to have it remove lines consisting of comments altogether.
Andrea
It shouldn't be difficult to modify the code to do what you want...
trinithis
It becomes messier, since the original code parses by character, and to do what I want, one need to keep track of lines too. But I decided to accept this anyway, and make the necessary modifications.
Andrea
@Andrea: Updated the code to remove the last line in a file if that line only contains comments.
trinithis
+2  A: 

Use Google's Closure Compiler with WHITE_SPACE_ONLY and PRETTY_PRINT -- the only thing that it will do is remove the comments (Unless of course you don't format your code in the way that PRETTY_PRINT does.)

It turns this:

// This function alerts a name
function hello(name) {
    /**
    * One lone
    * multi-line
    * comment
    */
    alert('Hello, ' + name);
}
hello('New user');

Into this:

function hello(name) {
  alert("Hello, " + name)
}
hello("New user");
Sean Vieira
This is almost what I want, but not quite. It removes all new lines, regardless if there where some in the original code. I would like to only remove lines entirely composed of comments, not intentional new lines.
Andrea
It also messes with other things, for instance it replaces my tabs with two spaces. I need a tool that does not come in my way, it only should remove comments.
Andrea