Search code examples
javascriptaptanaautomatic-semicolon-insertion

Why are semicolons omitted in all my source js files?


I'm starting a web project in Aptana and have downloaded bootstrap (v 3.3.7) and jQuery (compressed). When I imported the files into my js folder, all the files came up with a warning sign and the only warnings I can see are that there are missing semicolons everywhere. I think I've read that semicolons aren't necessary in js (although it's best practice to use them), but I've downloaded these files before for class and didn't receive this warning.

Have I done something wrong? Are these files OK to work with?


Solution

  • Are these files OK to work with?

    Yes, missing semicolons in most (but not all) places can be corrected by the JavaScript parser using the error-correction procedure* called Automatic Semicolon Insertion, which is built into the language specification. So leaving them out in the places ASI will fix for you is indeed allowed. But it's important to know ASI's rules if you're doing that. (And important to know the rules for ; in any case.) The Bootstrap team knows the rules. (jQuery uses explicit semicolons.)

    It sounds like you just have a lint-style warning enabled that you didn't have enabled before that flags up missing semicolons even in places ASI will fix.

    You could disable the warning by searching in the options for "lint" or "code style" or "code quality" options, finding the relevant option, and turning it off, but I wouldn't. Instead, I'd look for how to exempt libraries from lint/code style/code quality analysis, since the source of every library you use is unlikely to fit your overall coding style rules.


    * According to the language's original creator, Brendan Eich:

    ASI is (formally speaking) a syntactic error correction procedure. If you start to code as if it were a universal significant-newline rule, you will get into trouble.

    (Amusingly, he also said he wishes he'd made newlines more significant originally, rather than less. But as he said, that ship sailed in 1995...)