Utility to auto insert semicolons in javascript so

2019-03-18 05:39发布

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.

8条回答
倾城 Initia
2楼-- · 2019-03-18 06:09

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-03-18 06:19

JSCS is a linting tool that has a fix option to automatically fix linting issues including missing semicolons.

enter image description here

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-18 06:22

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.

查看更多
地球回转人心会变
5楼-- · 2019-03-18 06:22

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.

查看更多
来,给爷笑一个
6楼-- · 2019-03-18 06:23

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.

查看更多
Viruses.
7楼-- · 2019-03-18 06:28

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.

查看更多
登录 后发表回答