What is a good standalone JavaScript formatter to fix missing semicolons?

I am trying to modify / fix a lot of legacy web code, and unfortunately most of it is poorly formatted JavaScript. I am looking for a batch / script utility that can fix JavaScript that lacks simikolons at the end of executable instructions.

I tried a beautify-cl.js script with Rhino, but this does not add a semicolon. Also, I tried JSTidy , thinking that I can change it so that it is scriptable , but it shares all the comments. Given that we have something like 2000-3000 files, any solution should be scriptable.

The following topics were mentioned, however, none of the solutions was sufficient for various reasons: Javascript Beautifier - does not handle semicolon. Best source code formatter for Javascript? - Not available for scripting

Any ideas / solutions? Thanks in advance.

+4
source share
5 answers

Obviously, you need to do this if you want to minimize files during deployment. Missing semicolons are probably the reason number 1, the reasons why JS files do not interfere correctly, so I understand your motivation.

Write a little Python (or something else) script to run the file through jslint, then use jslint output to see which lines are needed with a comma, then scroll through the js source and add them.

I think you can be pretty fearless here because JavaScript implicitly adds semicolons.


Update: This toolbox may be what you are looking for . The Format tab offers a missing semicolon.

+2
source

I found a winning combination in js-beautify and Google Closure Linter :

 # legacy.js is your poorly formatted JavaScript file, and will be modified in-place js-beautify -s 2 -n -b end-expand -x -r legacy.js && fixjsstyle legacy.js 

Explanation of js-beautify :

  • -s 2 : indent with two spaces
  • -n : provide a new line at the end of the file
  • -b end-expand : puts { brackets at the end of the line, but always gives } binds its own line.
  • -x : unescape \xNN escaped characters in lines
  • -r : make changes in place

fixjsstyle , which is installed with the Closure Linter package, makes default changes by default.

This pipeline saves comments (!), Discards everything (basically) as I like, adds semicolons where necessary, and even replaces double quotes with single quotes where possible. Both commands can be given a list of files (for example, **/*.js ), and not just one.

To install the necessary packages on Mac OS X:

 npm install -g js-beautify brew install closure-linter 
+2
source

If you use JavaScript Utlity V2 at http://jsutility.pjoneil.net and use the format function, it will automatically replace the missing semicolons.

In addition, if you use the compaction function, it will also replace missing semicolons so that the compaction does not cause errors.

+1
source

You should not worry about mass updates of many legacy codes for the sole purpose of inserting half columns. This is the classic case of "doing it wrong."

How would you test the results? How would you guarantee the absence of "functionality" (since the side effect of the error caused by the lack of a half-site) was not lost?

Do you think half-columns will add to all of these files for you? Besides the large files (I'm not confusing the semicolon) and the huge amount of unverified code changes?

As gumbo said, use jslint . I would use it in files when you edit them in your daily work. When you edit these files, you are likely to test changes to the file at this time. This would be the most ideal time to go crazy when pasting with a colon.

Also, if you are concerned about saving and maintaining saved javascript files for 2000-3000 years, you have a lot more problems than half-columns

0
source

If http://jsutility.pjoneil.net has too many errors (and cannot format it), you can try to compress it: http://refresh-sf.com/yui/ (which will add the missing semicolons) and then go back to pjoneil.net format to get a beautiful code with semicolons.

0
source

Source: https://habr.com/ru/post/1286765/


All Articles