ES6 or CommonJS modules cause «Missing semicolon» errors #2605
I’m seeing erroneous WARNING — Missing semicolon errors when using ES6 or CommonJS modules (with jscomp_warning turned on for all checks). The warnings do not appear for Closure Library (goog) modules.
It sounds like Closure Compiler transpiles ES6 and CommonJS to Closure Library modules, so I wonder if a missing semicolon error is introduced during that process?
Here are 3 simple test examples, one for each module system. They each have an entry point test.js file that imports a module. I’ve included the module and test code for each, as well as the output of the command.
All commands were run with the v20170626 version of the compiler:
CommonJS
./module.js
./test.js
Result: 2 Warnings for Missing semicolon on lines that clearly have a semicolon.
ES6 modules
./module.js
./test.js
Result: 4 Warnings for Missing semicolon on lines that clearly have a semicolon.
How do I fix this missing semicolon syntax error in Javascript?
A friend wrote some code for me, and there was one file with a weird syntax error in it. After a bit of hunting, I narrowed it down to this section of code, which should reproduce the error:
When I run this, I see an error in the Internet Explorer console that says SCRIPT1004: Expected ‘;’ . I don’t see a semicolon missing anywhere, and I can’t imagine where it wants me to put one.
Where does it expect a semicolon and why does it expect a semicolon there?
JavaScript Missing Semicolon
For maybe the first time ever, I produced a bug in my JavaScript code that was the result of a parsing ambiguity created by omitting the semicolon at the end of a statement:
And so Ye Olde Firebug complained that console.log(“blargh”) was not actually a function. The misunderstanding stemmed from the fact that the first character on the second line is an opening parenthesis, which apparently causes the parser to assume the return value of the previous expression is a function that I intend to call, even though it’s on a separate line.
The lesson here, kids, is that you should always include the semicolons in your JavaScript. If you don’t, you might end up like me: having to waste an average of ten minutes every eight years or so picking parsing mistakes out of my sloppy, lazy code. DON ’T LET THIS BE YOU !
Comments
There’s some javascript trying to load some comments, and if you’re reading this, it’s probably not working.