views:

319

answers:

4

The question is pretty self explanatory.

I'm working with code from multiple developers. Some of whom are inconsistent in their use of semicolons, and I just want them after every line for consistency (and to prevent any masking of errors).

I use Aptana to format my source code, but it won't add semicolons for you AFAIK.

Thanks.

+2  A: 

Ah ... just discovered YUI compressor has a preserve semicolons options that will actually add the semicolons to the appropriate lines. Then it's just a matter of sending the minified, unmunged version back thru the aptana formatter.

If someone comes up with a simpler solution, please post.

Keith Bentrup
Unfortunately, I lose the comments, which I can protect to a certain extent or add again but then I'm creating more work.
Keith Bentrup
I'll check out the YUI compressor source and see if there's a way around this.
Keith Bentrup
+2  A: 

I would recommend you to encourage your developers to use JSLint to improve the overall quality of your code.

This tool will look for problems in your code based on a set of rules, and of course, it will detect missing semicolons.

And since you are using Aptana, is fairly easy to install the JSLint Eclipse Plugin.

CMS
This doesn't answer my question. I'm already aware of jslint and the plugin. The question was regarding a way to automatically insert the semicolons.
Keith Bentrup
+2  A: 

I wrote an extension to Douglas Crockford's JSMin. It automatically inserts semicolons where they are missing from sloppy code. This is very slow. Read the documentation before you use it. It is integrated into the Pretty Diff tool, but you can access the bit of code directly from the documentation.

The tool also features a JSLint compliant JavaScript beautifier so if you choose to go with the more efficient YUI Compressor you can still use the Pretty Diff tool to expand the code back out and even diff instantly between minified and expanded code.

http://mailmarkup.org/prettydiff/prettydiff.html

A: 

I think a very important question here is "why?". Seriously, if it's not causing any problems (which it doesn't), then who cares? Yes, I agree that semicolons in Javascript should be required, but they're not, so it's not going to cause any errors. If you feel strongly about this (as I would too), educate your fellow developers about why they should care and solve the problem, rather than employ a hacky script to alter their code.

nickf
JavaScript missing semicolons is impossible to unit test accurately and impossible to minify. Developers who do not understand why it is important to add semicolons will not likely understand the implications upon the code of fixing it manually, which is more reason to use a hacky script for prior bad code. I do agree that educating the developers to write the code properly is important, but that does not fix prior bad acts.
i completely agree with the reasons to strictly use semicolons, and trust me, I'd be looking to intervene if my co-workers were in the same bad habit of leaving them off. I just don't think it's a good idea to let them continue writing sloppy code and letting a program fix it up for them. Depending how much existing code there is, then yeah I suppose you might require an automated way to solve it, but it's not a long term solution: that's the crux of my argument.
nickf
I completely agree that automated semicolon insertion is an extremely bad idea as a standard practice. I would even go so far as to say that developers who are not formally educated or personally versed in writing JavaScript must not be provided the opportunity to write JavaScript unsupervised. Unless better controls are implemented its only a matter of time before client side scripting kills all possibility of the web as a platform for generating business revenue due to security compromise, collisions, and interoperability problems from difference poor uses in even the same single language.