views:

515

answers:

2

I'm trying to retrofit/fix lots of legacy web code and unfortunately most of it is poorly formatted JavaScript. I'm looking for a batch/scriptable utility that can fix JavaScript that is missing simicolons at the end of executable statements.

I've tried the beautify-cl.js script with Rhino but that does not does not add semicolons. In addition, I have tried JSTidy thinking I could modify it to be scriptable, but it strips all comments. Considering we have something like 2000-3000 files, any solution has to be scriptable.

The following topics were referenced, however none of the solutions were sufficient for various reasons: Javascript Beautifier - Doesn't handle semicolon Best source code formatter for Javascript? - Not scriptable

Any ideas/solutions? Thanks in advance.

A: 

You shouldn't be worried about doing a mass update on a lot of legacy code for the sole purpose of inserting semi colons. That's a classic case of "doing it wrong".

How would you test the results? How would you ensure no "functionality" (as a side effect of a bug caused by a semi colon being missing) isn't lost?

What do you think adding semi colons to all these files will get you? Beside larger files (I'm not knocking the use of semicolons) and massive amounts of untested code changes?

As gumbo said, use jslint. I would use it on the files as you edit them in your day to day work. As you edit these files, presumably you will be testing changes to the file at that time. That would be the most ideal time to go crazy with semi colon insertion.

Also, if you're concerned about keeping 2000-3000 legacy javascript files alive and supported, you've got far bigger problems than semi colons

Allen
If there's good coverage with unit tests, you could pull it off. But chances are, if it's that poorly formatted to begin with, there aren't any unit tests.
mgroves
Exactly, mgroves. Obviously significant code coverage allows for fearless refactoring, but I highly doubt this is the case in this situation :)
Allen
I agree. It's worthwhile to use jslint (or a similar tool) whenever changing a file for another reason. But skip the huge mass migration.
Matthew Flaschen
The main issue is so that we can do things like minify more safely. In addition, many of the tools out there assume that the code is more well-formed than it currently is. We've got regression testing to verify things so that isn't as much of an issue. Plus now is actually an excellent time for us to do a "big bang" change, as other required changes necessitate a full regression. Those other changes were relatively easy, but automatable.
Paul Kuykendall
For fun, I'm going to disagree with you guys, because JavaScript implicitly adds the semicolons anyway. I think it's a reasonably safe refactor. John Candy in Stripes: "If it were me, I'd bet everything. But that's me. I'm an aggressive gambler. Mr. Vegas. Come on. Go for it."
Nosredna
What kind of regression testing? Automated? What % coverage? You just spent a good deal convincing everyone that this would be a monumental task to add semi colons due to size and scope but now you've got regression testing down pat, no worries?
Allen
+2  A: 

Obviously you'll need to do this if you want to minify the files on deployment. Missing semicolons are probably the #1 reason JS files don't minify properly, so I understand your motivation.

Write a little Python (or whatever) script to run the file through jslint, then use the output from jslint to see which lines need semicolons, then spin through the js source and add them.

I think you can be fairly fearless here, because JavaScript implicitly adds the semicolons anyway.


Update: This set of tools may be what you are looking for. The "format" tab offers missing semicolon insertion.

Nosredna
right, and who's going to ensure that it all works exactly like it did before?
Allen
That's a risk. But I think his odds are good. I have not had JSLint tell me to add a semicolon when I shouldn't. I've seen plenty of people take jslint's semicolon suggestions mindlessly anyway. He'd be wasting a lot of time to go through by hand with jslint if there is a business reason to minify. And he'd be accepting jslint's semicolon suggestions mindlessly anyway, with that many files.
Nosredna
If he has that many big files, and it's sloppily written, it's full of bugs anyway. He might as well be debugging code after it's been through a bit of jslint semicolon sanity checking.
Nosredna
I have no problem with semi colon insertion 1 file at a time, as he's working on them. That makes sense and I stated that in my answer. 1 big huge batch seems silly though, IMO. A typical minifier should be able to get the file size down to a somewhat reasonable size as the code is.
Allen