Rename 3.4 Use Semicolon to proper statement separation

Fixes #26.

I hope I integrated all feedback from this issue, while leaving the core message intact: People should be aware how statements are separated or linked together, and that there are cases where ASI and/or semicolon do not help, if statement termination is not understood. This also leaves room for the reader to prefer semicolons or not.
This commit is contained in:
Martin Muzatko
2019-03-25 13:33:25 +01:00
committed by GitHub
parent 2904acc5c1
commit 12a3b41eb3

View File

@ -271,28 +271,48 @@ function someFunction()
<br/><br/> <br/><br/>
## ![✔] 3.4 Don't Forget the Semicolon ## ![✔] 3.4 Separate your statements properly
**TL;DR:** While not unanimously agreed upon, it is still recommended to put a semicolon at the end of each statement. This will make your code more readable and explicit to other developers who read it No matter if you use semicolons or not to separate your statements, knowing the common pitfalls of inproper linebreak or automatic semicolon insertion, will help you to eliminate regular syntax errors.
**Otherwise:** As seen in the previous section, JavaScript's interpreter automatically adds a semicolon at the end of a statement if there isn't one, or considers a statement as not ended where it should, which might lead to some undesired results **TL;DR:** Use ESLint to make you aware about separation concerns. Prettier or Standardjs can automatically resolve these issues.
**Otherwise:** As seen in the previous section, JavaScript's interpreter automatically adds a semicolon at the end of a statement if there isn't one, or considers a statement as not ended where it should, which might lead to some undesired results. You can use assignments and avoid using immediate invoked function expressions to prevent most of unexpected errors.
### Code example ### Code example
```javascript ```javascript
// Do // Do
const count = 2; function doThing() {
(function doSomething() { // ...
// do something amazing }
}());
doThing()
// Do
const items = [1, 2, 3]
items.forEach(console.log)
// Avoid — throws exception
const m = new Map()
const a = [1,2,3]
[...m.values()].forEach(console.log)
> [...m.values()].forEach(console.log)
> ^^^
> SyntaxError: Unexpected token ...
// Avoid — throws exception // Avoid — throws exception
const count = 2 // it tries to run 2(), but 2 is not a function const count = 2 // it tries to run 2(), but 2 is not a function
(function doSomething() { (function doSomething() {
// do something amazing // do something amazing
}()) }())
// put a semicolon before the immediate invoked function, after the const definition, save the return value of the anonymous function to a variable or avoid IIFEs alltogether
``` ```
🔗 [**Read more:** "Semi ESLint rule"](https://eslint.org/docs/rules/semi)
🔗 [**Read more:** "No unexpected multiline ESLint rule"](https://eslint.org/docs/rules/no-unexpected-multiline)
<br/><br/> <br/><br/>
## ![✔] 3.5 Name your functions ## ![✔] 3.5 Name your functions