From 12a3b41eb30844a4a98c3efa1dcf85de9237d22c Mon Sep 17 00:00:00 2001 From: Martin Muzatko Date: Mon, 25 Mar 2019 13:33:25 +0100 Subject: [PATCH] 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. --- README.md | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 31b34a71..225d06f7 100644 --- a/README.md +++ b/README.md @@ -271,28 +271,48 @@ function someFunction()

-## ![✔] 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 ```javascript // Do -const count = 2; -(function doSomething() { - // do something amazing -}()); +function doThing() { + // ... +} + +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 const count = 2 // it tries to run 2(), but 2 is not a function (function doSomething() { // 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) +

## ![✔] 3.5 Name your functions