|
| 1 | +## Automatic Semicolon Insertion |
| 2 | + |
| 3 | +Although JavaScript has C style syntax, it does **not** enforce the use of |
| 4 | +semicolons in the source code, so it is possible to omit them. |
| 5 | + |
| 6 | +JavaScript is not a semicolon-less language. In fact, it needs the |
| 7 | +semicolons in order to understand the source code. Therefore, the JavaScript |
| 8 | +parser **automatically** inserts them whenever it encounters a parse |
| 9 | +error due to a missing semicolon. |
| 10 | + |
| 11 | + var foo = function() { |
| 12 | + } // parse error, semicolon expected |
| 13 | + test() |
| 14 | + |
| 15 | +Insertion happens, and the parser tries again. |
| 16 | + |
| 17 | + var foo = function() { |
| 18 | + }; // no error, parser continues |
| 19 | + test() |
| 20 | + |
| 21 | +The automatic insertion of semicolon is considered to be one of **biggest** |
| 22 | +design flaws in the language because it *can* change the behavior of code. |
| 23 | + |
| 24 | +### How it Works |
| 25 | + |
| 26 | +The code below has no semicolons in it, so it is up to the parser to decide where |
| 27 | +to insert them. |
| 28 | + |
| 29 | + (function(window, undefined) { |
| 30 | + function test(options) { |
| 31 | + log('testing!') |
| 32 | + |
| 33 | + (options.list || []).forEach(function(i) { |
| 34 | + |
| 35 | + }) |
| 36 | + |
| 37 | + options.value.test( |
| 38 | + 'long string to pass here', |
| 39 | + 'and another long string to pass' |
| 40 | + ) |
| 41 | + |
| 42 | + return |
| 43 | + { |
| 44 | + foo: function() {} |
| 45 | + } |
| 46 | + } |
| 47 | + window.test = test |
| 48 | + |
| 49 | + })(window) |
| 50 | + |
| 51 | + (function(window) { |
| 52 | + window.someLibrary = {} |
| 53 | + |
| 54 | + })(window) |
| 55 | + |
| 56 | +Below is the result of the parser's "guessing" game. |
| 57 | + |
| 58 | + (function(window, undefined) { |
| 59 | + function test(options) { |
| 60 | + |
| 61 | + // Not inserted, lines got merged |
| 62 | + log('testing!')(options.list || []).forEach(function(i) { |
| 63 | + |
| 64 | + }); // <- inserted |
| 65 | + |
| 66 | + options.value.test( |
| 67 | + 'long string to pass here', |
| 68 | + 'and another long string to pass' |
| 69 | + ); // <- inserted |
| 70 | + |
| 71 | + return; // <- inserted, breaks the return statement |
| 72 | + { // treated as a block |
| 73 | + |
| 74 | + // a label and a single expression statement |
| 75 | + foo: function() {} |
| 76 | + }; // <- inserted |
| 77 | + } |
| 78 | + window.test = test; // <- inserted |
| 79 | + |
| 80 | + // The lines got merged again |
| 81 | + })(window)(function(window) { |
| 82 | + window.someLibrary = {}; // <- inserted |
| 83 | + |
| 84 | + })(window); //<- inserted |
| 85 | + |
| 86 | +> **Note:** The JavaScript parser does not "correctly" handle return statements |
| 87 | +> which are followed by a new line, while this is not neccessarily the fault of |
| 88 | +> the automatic semicolon insertion, it can still be an unwanted side-effect. |
| 89 | +
|
| 90 | +The parser drastically changed the behavior of the code above. In certain cases, |
| 91 | +it does the **wrong thing**. |
| 92 | + |
| 93 | +### Leading Parenthesis |
| 94 | + |
| 95 | +In case of a leading parenthesis, the parser will **not** insert a semicolon. |
| 96 | + |
| 97 | + log('testing!') |
| 98 | + (options.list || []).forEach(function(i) {}) |
| 99 | + |
| 100 | +This code gets transformed into one line. |
| 101 | + |
| 102 | + log('testing!')(options.list || []).forEach(function(i) {}) |
| 103 | + |
| 104 | +Chances are **very** high that `log` does **not** return a function; therefore, |
| 105 | +the above will yield a `TypeError` stating that `undefined is not a function`. |
| 106 | + |
| 107 | +### In Conclusion |
| 108 | + |
| 109 | +It is highly recommended to **never** omit semicolons; it is also advocated to |
| 110 | +keep braces on the same line with their corresponding statements and to never omit |
| 111 | +them for one single-line `if` / `else` statements. Both of these measures will |
| 112 | +not only improve the consistency of the code, but they will also prevent the |
| 113 | +JavaScript parser from changing its behavior. |
| 114 | + |
0 commit comments