Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Utility to auto insert semicolons in JavaScript source code?

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.

like image 909
Keith Bentrup Avatar asked Sep 27 '09 07:09

Keith Bentrup


3 Answers

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.

like image 199
Keith Bentrup Avatar answered Nov 03 '22 17:11

Keith Bentrup


I found Google's Closure-Linter, which seems to be Google's version of JSLint, to work extremely well.

https://developers.google.com/closure/utilities/docs/linter_howto

The webpage above not only explains how to install it on your computer, but at the bottom it explains how to run fixjsstyle which adds almost all missing semicolons as well as some other little things.

like image 40
mareoraft Avatar answered Nov 03 '22 16:11

mareoraft


I use this online formatter: http://jsutility.pjoneil.net/ (go to Format Tab, paste your code and click on: "format Javascript statements").

As you can see, there is an option to the right: "replace missing semicolons"

Sometimes, that formatter will report "too many errors" and won't do anything. If that is the case, compress your code with this utility: http://refresh-sf.com/yui/ (which will also add semicolons) and then, you can go back to the formatter to uncompress it.

like image 20
lepe Avatar answered Nov 03 '22 17:11

lepe