Take the 2-minute tour ×

I'm doing some optimizations and decided to compress my javascript files using YUI Compressor. The problem is, that some code lines are missing ";" at the end, since javascript allows that and developers do not look too much at this.

Is it going to be a problem when code is compressed? If it is, is there a way to check javascript for lines that are missing ";"?

share|improve this question
I'm not familiar with YUI Compressor, but if it changes the semantics of syntactically valid javascript, then it's broken... –  Jon Jan 28 '11 at 9:27
If you remove newlines in semicolon-less JavaScript it will break.. without the compressor being broken. Besides that, IMO JavaScript where semicolons are not used properly is broken.. they should really screw backwards compatibility and enforce semicolons.. or at least add a mode to enforce it (does use strict; already do that?) –  ThiefMaster Jan 28 '11 at 9:29
@ThiefMaster I find it a shame that so many people believe that semi-colons somehow reduce the number of problems in code or that ASI is somehow hard to "get right". I can count the number of semi-colons in my code on my fingers. A JavaScript tool that does not understand JavaScript is simply broken. A program who uses semi-colons "because they need to be there" is just as broken. (I'm not against coders who use semi-colons in JavaScript, just those who can't appreciate others writing in a semi-colon-free style). –  user166390 Jan 28 '11 at 9:37
add comment

4 Answers

up vote 9 down vote accepted

jsLint can check your code for that. And yes it will most likely cause issues unless the compressor actually contains a JavaScript parser and actively fixes missing semicolons.

share|improve this answer
yup jsLint will check for that. It will also hurt your feelings(first quote jsLint.com) ;) –  Alfred Jan 28 '11 at 9:31
And you totally deserve that if you write crappy code. ;) –  ThiefMaster Jan 28 '11 at 9:33
@ThiefMaster You are falsely assuming that lack of semi-colons makes for "crappy code". It does not. Crappy code is crappy code -- semi-colons or not (or "passing" jsLint or not, for that matter). Good code is good code -- semi-colons or not. –  user166390 Jan 28 '11 at 9:40
jsLint doesn't only complain about missing semicolons but other things which might actually mean the code is crappy. But IMO omitting semicolons makes code kind of crappy. –  ThiefMaster Jan 28 '11 at 9:42
@ThiefMaster That problem is actually mis-attributed to being a problem with ASI. The problem is really that return is a "restricted production" (there are 5 of them) -- the exact same issue would exist without ASI. Here is a good explanation of the problem, along with how to write safe semi-colon free code when a line may continue an expression: JavaScript Semicolon Insertion Everything you need to know. See the "Misconceptions" section at the bottom. –  user166390 Jan 28 '11 at 9:51
show 3 more comments

According to this SO answer, YUI Compressor can handle it.

share|improve this answer
According to docs ( developer.yahoo.com/yui/compressor/#using ): Preserve unnecessary semicolons (such as right before a '}') This option is useful when compressed code has to be run through JSLint (which is the case of YUI for example). So, this does not do... –  Silver Light Jan 28 '11 at 11:02
add comment

I've done a simple test on three JavaScript compressors: Yuicompressor, Yuglify and Google Closure Compiler. On my PC with Ubuntu 12.10, I've downloaded the binaries of every compressor, and then tested this file on every one of them:

function dbz(){
    var goku = 1
    var vegeta = 2
    var freeza = 3
    console.log(goku + vegeta + freeza)

And here is the results:

Yuicompressor (2.4.7):

function dbz(){var b=1;var c=2;var a=3;console.log(b+c+a)}dbz();

Yuglify (0.1.2):

function dbz(){var e=1,t=2,n=3;console.log(e+t+n)}dbz();

Closure-Compiler (version 20121212 revision 2388):

function dbz(){console.log(6)}dbz();

Althought this is a very simple example, all of then worked fine on lines with missing semicolons. All of them detected lines without semicolons at the end, added it and removed the line break afterwards.

share|improve this answer
add comment

You could always use regexp to match for newlines that dont have a ; before them, and naturally make exceptions for stuff like empty lines, )} etc.

But to be honest, if it really doesn't already do this sort of thing automatically, seems like it really is broken \ plain bad.

share|improve this answer
Not the best idea to use a regular expression for this sort of thing (in general :-), but you may be interested in how jsmin works. It won't correctly minify all code, but it does a fair job at most code and explains the (text replacement) rules as well as notes some limitations with the approach. Welcome to SO. –  user166390 Jan 28 '11 at 11:30
Well explained there. I'm actually using it already with one of my Drupal sites, but haven't read how it actually works. Thanks for the welcome! –  red Jan 28 '11 at 15:27
add comment

Your Answer


