Disable, or at least detect, automatic semicolon insertion

javascript, syntax

Solution

The expression after `return` keyword MUST ALWAYS start on the same line that the keyword is, this is not interpreter related, it's defined by the ECMAScript standard, it's a bad part of the language but if you respect the rules of writing the JS code described by Douglas Crockford then you'll not encounter this again.

From "JavaScript: The Good Parts" by Douglas Crockford (Appendix A.3 Awful Parts):

JavaScript has a mechanism that tries to correct faulty programs by automatically inserting semicolons. Do not depend on this. It can mask more serious errors.

It sometimes inserts semicolons in places where they are not welcome. Consider the consequences of semicolon insertion on the return statement. If a return statement returns a value, that value expression must begin on the same line as the return:

return
{
    status: true
};

This appears to return an object containing a status member. Unfortunately, semicolon insertion turns it into a statement that returns undefined. There is no warning that semicolon insertion caused the misinterpretation of the program. The problem can be avoided if the { is placed at the end of the previous line and not at the beginning of the next line:

return {
    status: true
};

Also see the code conventions for JavaScript by Douglas Crockford: http://javascript.crockford.com/code.html

Problem

I always code in `strict mode` hoping to be shielded(or at least forcibly told to change my code) from problems with the Javascript language like using deprecated methods or misinterpretable syntax. However I hit this problem today and I was wondering whether there was any way to disable semicolon insertion in the browser or otherwise have similar-to-strict-mode 'compile'-time errors? JS[H/L]int doesn't happen to be able to pick up where JS interpreters would insert semicolons and flag them for us to mitigate would it? EDIT JShint and JSLint both error if a new line is present before a semicolon is found after a the `return` keyword. However, I don't know about the other caveats regarding automatic insertion and whether they are each detected too. Regardless, if an answer actually solves the 'disabling' part, that would be more relevant.

Original source

Related problems