You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[guide] Add explanation for semicolon enforcement rule
Also update code samples to highlight ways in which ES6
currently mishandles code when relying on Automatic Semicolon
Insertion
Revise hyperlink and example
- Use more up-to-date TC39 reference page on github.io
- wrap returnless function with side-effects in curly braces
also, clean up punctuation on a long comment line
Always use single-quoted strings
Except for when the string needs to be double-quoted because the string contains apostrophes
Update second example to incorporate IIFE
An IIFE is a more realistic example of code that developers may encounter which would raise an exception when relying completely upon ASI
> Why? When JavaScript encounters a line break without a semicolon, it uses a set of rules called [Automatic Semicolon Insertion](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion) to determine whether or not it should regard that line break as the end of a statement, and (as the name implies) place a semicolon into your code before the line break if it thinks so. ASI contains a few eccentric behaviors, though, and your code will break if JavaScript misinterprets your line break. These rules will become more complicated as new features become a part of JavaScript. Explicitly terminating your statements and configuring your linter to catch missing semicolons will help prevent you from encountering issues.
2771
+
2770
2772
```javascript
2771
-
// bad
2772
-
(function () {
2773
-
constname='Skywalker'
2774
-
return name
2775
-
})()
2773
+
// bad - raises exception
2774
+
constluke= {}
2775
+
constleia= {}
2776
+
[luke, leia].forEach(jedi=>jedi.father='vader')
2777
+
2778
+
// bad - raises exception
2779
+
constreaction="No! That's impossible!"
2780
+
(asyncfunctionmeanwhileOnTheFalcon(){
2781
+
// handle `leia`, `lando`, `chewie`, `r2`, `c3p0`
2782
+
// ...
2783
+
}())
2784
+
2785
+
// bad - returns `undefined` instead of the value on the next line - always happens when `return` is on a line by itself because of ASI!
2786
+
functionfoo() {
2787
+
return
2788
+
'search your feelings, you know it to be foo'
2789
+
}
2776
2790
2777
2791
// good
2778
-
(function () {
2779
-
constname='Skywalker';
2780
-
return name;
2792
+
constluke= {};
2793
+
constleia= {};
2794
+
[luke, leia].forEach((jedi) => {
2795
+
jedi.father='vader';
2796
+
});
2797
+
2798
+
// good
2799
+
constreaction="No! That's impossible!";
2800
+
(asyncfunctionmeanwhileOnTheFalcon(){
2801
+
// handle `leia`, `lando`, `chewie`, `r2`, `c3p0`
2802
+
// ...
2781
2803
}());
2782
2804
2783
-
// good, but legacy (guards against the function becoming an argument when two files with IIFEs are concatenated)
2784
-
;((() => {
2785
-
constname='Skywalker';
2786
-
return name;
2787
-
})());
2805
+
// good
2806
+
functionfoo() {
2807
+
return'search your feelings, you know it to be foo';
0 commit comments