Every line that begins with a (
, [
, `, or any operator (/, +, - are the only valid ones), must begin with a semicolon.
func()
;[0].concat(myarr).forEach(func)
;(myarr).forEach(func)
;`hello`.forEach(func)
;/hello/.exec(str)
;+0
;-0
This prevents a
func()[0].concat(myarr).forEach(func)(myarr).forEach(func)`hello`.forEach(func)/hello/.forEach(func)+0-0
monstrocity.
To mention what will happen: brackets will index, parentheses will be treated as function parameters. The backtick would transform into a tagged template, and regex or explicitly signed integers will turn into operators. Of course, you can just add a semicolon to the end of every line. It's good to keep mind though when you're quickly prototyping and are dropping your semicolons.
Also, adding semicolons to the end of every line won't help you with the following, so keep in mind statements like
return // Will automatically insert semicolon, and return undefined.
(1+2);
i // Adds a semicolon
++ // But, if you really intended i++ here, your codebase needs help.
The above case will happen to return/continue/break/++/--. Any linter will catch this with dead-code or ++/-- syntax error (++/-- will never realistically happen).
Finally, if you want file concatenation to work, make sure each file ends with a semicolon. If you're using a bundler program (recommended), it should do this automatically.