can commented code throw an error?

in the case of bundling and minification...yes it can. i was performing cross-browser tests on one of my applications the other day and found myself chasing a javascript error that well...wasn't really an error. for some reason, the browser kept complaining about a syntax error.

 

a picture is worth a thousand words

upon first opening the script debugger, I went through each line ensuring that things were properly terminated and all blocks properly closed. it wasn't until after about ten minutes of scouring through the lines of several bundled files that i noticed that there was a problem on line 583. line 583 in the script debugger depicts the merge point of two of these script files. more specifically, the end of one of my utility files and the beginning of the jquery-cookie plugin. you see, the problem was that the end of my file contained single line comments, once used as a reference, that i hadn't bothered to remove, and the jquery-cookie plugin contained a boilerplate heading which is a common way of providing descriptions, licenses, and other disclosures in code modules. i am guessing that because the comment styles were different, the bundling engine didn't recognize the break, nor does it automatically inject a line break between merged files, the two script files ran together. this caused the debugger to look for a closing brace in order to complete a code block "}".

hmm, technically...there was a syntax error

you know on second thought, given that i was using single-line comments across multiple lines of code, which is a semantic no-no, the debugger was correct in that it was a syntax error...at least technically speaking. after all, we do have a syntax at our disposal for multi-line comments i.e. /* my comments */. okay okay...so let's fix this by wrapping the single-line commented code in multi-line comment marks.

/***********************************************

//url.protocol; (http:)

//url.hostname; (www.example.com)

//url.pathname; (/some/path)

//url.search; (?name=value)

//url.hash; (#anchor)

//var url = document.createElement('a');

//url.href = document.URL;

//var controller = url.pathname;

//var idx = controller.lastIndexOf("/");

//if (idx > 0) {

// controller = controller.substring(0, idx);

//}

************************************************/

yes, i left the commented code in the file...and no i didn't remove the single-line comment marks. why? because i wanted to. and all is good with the debugger.

Comments


Comments are closed