Utility to auto insert semicolons in JavaScript source code?
Asked Answered
G

6

15

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.

Gymnasiarch answered 27/9, 2009 at 7:18 Comment(1)
Did you find a way? What did you end up doing?Lusitania
G
8

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.

Gymnasiarch answered 27/9, 2009 at 7:26 Comment(2)
Unfortunately, I lose the comments, which I can protect to a certain extent or add again but then I'm creating more work.Gymnasiarch
I'll check out the YUI compressor source and see if there's a way around this.Gymnasiarch
B
7

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.

Baudekin answered 3/3, 2015 at 2:45 Comment(4)
fixjsstyle worked perfectly, should be the accepted answer IMHO.Angwantibo
Works great, and it's even easier on Mac OS X than the webpage suggests: brew install closure-linterAmato
It's deprecated now.Ctesiphon
It's now deprecated and the Closure-Linter repo was archived (now read-only) on Dec 29, 2022. So I guess your mileage may vary.Baudekin
D
6

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.

Denverdeny answered 2/4, 2014 at 2:41 Comment(1)
Does not parse version 6+ JSCrosstie
C
5

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.

Convalescent answered 27/9, 2009 at 7:30 Comment(1)
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.Gymnasiarch
Y
2

I use fixmyjs. It's available as a CLI and as a plugin for some editors.

It automatically fixes some JavaScript errors in a non-destructive way. The semicolon fix is one of the available auto corrections.

Yolanda answered 8/9, 2015 at 8:24 Comment(0)
C
0

I'm a Flash developer, and I stumbled across this while running a Google search. For fellow AS (a JS cousin) devs, if you're using Adobe Flash Professional (I'm using CS5), when you're editing an actionscript file, click "Tools" and "AutoFormat". The program will take care of the rest.

(Again, this answer is for ActionScript developers, as this is a common issue for us as well.)

Coeternity answered 3/12, 2011 at 2:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.