4 ms·
There's a lot of FUD around Automatic Semicolon Insertion for some reason. Basically, the parser inserts a semicolon anywhere where the program is otherwise unp
by wolfgang42 10y ago
There's a lot of FUD around Automatic Semicolon Insertion for some reason. Basically, the parser inserts a semicolon anywhere where the program is otherwise unparseable but a semicolon would make it syntactically valid. Importantly, this will occur even if you are otherwise using semicolons, so simply saying that you need to manually put the semicolons in doesn't help the situation since the parser may add them for you in places you didn't expect anyway. Leaving them out forces you to know the rules, but you should have known them anyway since otherwise they will bite you at some point.
ESLint provides--and StandardJS uses--the `no-unexpected-multiline` rule, which will insist that you insert semicolons in places where the meaning would otherwise be unexpected. In practice, with `semi` set to never and `no-unexpected-multiline` enabled, I find that I have never had a problem with semicolons that was not caught by the linter. By contrast, when I was afraid of ASI (since "omitting semicolons actually has an effect", eek!) I would find that I'd occasionally have mysterious issues where arrays were interpreted as indexes and so on. (I switched to semicolonless-style at the same time as I started using a linter, so I can't compare the two independently.)
- partycoder 10y agoThere's a lot of FUD around not using a seat belt. Basically, if you are careful to not crash your car, your seat belt should not be necessary. You can die even if using a seat belt. My standard of car safety relies on that each time you crash your car, a red light will flash telling you that you have crashed your car. It is very effective detecting car crashes, therefore, very safe.
- randallsquared 10y agoHm. In the case where the ASI inserts a semicolon where you wouldn't have expected one, how do the semicolons you already added where you would expect them help? Using semicolons in JS to avoid problems is like making sure the seatbelt is securely fastened across the seat before you sit down: there's a satisfying click and the worrying light on the dash goes off, but it won't actually protect you in an accident.