Here are the answers to the test. The red semi's are the result of ASI.
Test 1: 101
This was a simple test to test the basic rules presented to you at start. There were two traps though. Both had to do with the function expression. One comes right after the d of the function body. It's just a script, so it ends with a semi-colon. The other one is that there is NO ASI after a function declaration. The specification simply doesn't require them so there's no reason for ASI to act. Don't forget that ASI is applied at the end of the source as well!
You used semi's. There were ASI's. You found . You wasted semi's and missed ASI's.
Test 2: Textbook
The first part really is an example right from the specification. ASI is applied after the 2 because of the curly and of course after the three.
The second I altered for the test but was also an example. The incremental operator is a so called "restricted production". A bit counter-intuitively this causes ASI before the operator, rather than after.
You used semi's. There were ASI's. You found . You wasted semi's and missed ASI's.
Test 3: Never do this
Classical example of a return statement gone rogue. The ASI happens right after the return statement. Again, function declaration so no ASI after it.
In the for statement, the only ASI is after the second alert call. And of course ASI happens after regular expressions. Don't be eluded by the rules ;).
You used semi's. There were ASI's. You found . You wasted semi's and missed ASI's.
Test 4: Calling you out
Ninja's should have been primed for bracket problems here. Of course the foo and bar calls were decoys. Nothing interesting happens there. The issue lies with the function inside the for-loop that acts as a local scope. There are parenthesis surrounding these. These cause ASI to fail because they are interpreted as the call to the result of the first foo() expression. So it's basically foo()(function(){...}).
Other than that no ASI happens after the for, the rest is "normal".
In the for-statement, the only ASI is after the second alert call. And of course ASI happens after regular expressions. Don't be eluded by the rules ;).
You used semi's. There were ASI's. You found . You wasted semi's and missed ASI's.
Test 5: Just warming up
No hint this time. Not much to report either. I'm testing whether you can spot that the return statement doesn't apply ASI mid-way an expression.
And the last line should have been obvious as it was outlined in the rules, no ASI if the next line starts with a regular expression (the whole thing becomes a double divison).
You used semi's. There were ASI's. You found . You wasted semi's and missed ASI's.
Test 6: Out of control
This one is mainly about break having the same problem as return and whether you'd figure out that a labeled statement still counts as a line terminator (and there fore ASI being applied).
You used semi's. There were ASI's. You found . You wasted semi's and missed ASI's.
Test 7: Stack 'm up
About annoyingly stacked functions. Function expressions get a semi-colon while function declarations do not. Often the cramped curly and parenthesis obscure the fact that they are still terminated by a semi-colon.
You used semi's. There were ASI's. You found . You wasted semi's and missed ASI's.
Test 8: Mister y
Did you spot the ASI in the while loop? Note that no ASI takes place right after the while loop. And the tilde (~) operator is unary so ASI is applied before it.
You used semi's. There were ASI's. You found . You wasted semi's and missed ASI's.
Want to test your own script for ASI? Check out my parser.
This test was made for fun. But it also tries to prove a point. Using semi-colons is a "best practice" when it comes down to group projects. I don't care what you do with your own projects. But as soon as you are working with other people on the same project, you can't assume they all have the same knowledge on the JS rules as you do. And I hope theses tests also prove that some constructs come so natural that even the best scripter could fall for an ASI trap. So always use semi-colons in your JavaScripts. And if you do this always you won't have to be annoyed by them when you are required to use them.