Sidebar

Index

javascript:how-to-add-semicolon-automatically-on-every-line-to-existing-scripts

Javascript: How to add semicolon automatically on every line to existing scripts

Suppose you've found some interesting javascript code on the internet but this one has been written down without the final semicolon at the end of every line (That's not so rare because unfortunately the missing semicolon is perfectly legal in javascript :-( ), but you want it for some good reason, e.g.:

  • Consistency with your code
  • Your IDE is alarming you (see Eclipse)
  • Some minifier could destroy your code guessing where the line terminates.

For the above reasons, I was looking for a tool able to add the semicolon automatically without the need to manually adding it among tons of lines… and I found one online here: http://refresh-sf.com/yui/

This is not exactly a tools for that purpose, anyway before shrinking it adds semicolon, so the only thing to do after that is using a beautifier to reformat the code to something human readable.

So, the first thing to do is going to http://refresh-sf.com/yui/ and before submitting the compression, set the following options:

  • Minify only, no symbol obfuscation.
  • Preserve unnecessary semicolons (such as right before a '}').
  • Disable all the built-in micro optimizations.

The resulting file can now be reformatted into an IDE or if you don't have one you can use one of the many online beautifiers such this one http://jsbeautifier.org/

NB: the comments are inevitably lost because the minifiers cuts them off. If you know some better and specific tool, let me know! — Riccardo Bartolini 2013/07/15 13:16

javascript/how-to-add-semicolon-automatically-on-every-line-to-existing-scripts.txt · Last modified: 2014/09/03 16:31 by rik