Ever had to write alot of JavaScript? Better yet, ever inherit alot of JavaScript and not have time to fully understand what it does? Best - have you ever inherited alot of JavaScript that you don't have time to understand and have to quickly figure out how to extend it? Well, I HAVE!
Currently I'm on a project that is built on top of a framework that contains a non-trivial amount of JavaScript. Normally this would be OK, but I have to actually extend the framework a little for the current project. And, normally this would be OK (with another language) because you would have tools like compilers, debuggers, and friendly editors that make it easier. However, we're talking about JavaScript.
So, I had this problem. I had edited a JS file used by my web app and I expected the goodness to just start flowing. However, it seemed as if the browser (IE) was not even loading the file. When I debugged (with Visual Studio Script Debugger), I could evaluate some expressions that lead me to believe none of the functions in that entire file had been defined. Hmmm... Why wasn't it reading the file - the declaration in the HTML looked correct. What's going on?
That's when I started thinking I had some sort of syntax problem with the JavaScript in the file. But, I didn't know how in the world I could figure it out without a compiler or some kind of LINT tool. Maybe if I could just find a parser and see if it puked on my file...
Enter Rhino. Rhino is an open source implementation of JavaScript written in Java and is part of the Mozilla project. Not only does Rhino contain a JavaScript parser and evaluation engine, but it also contains a JavaScript debugger and interactive shell. YES!!!
As soon as I tried to load my JS file into the debugger, Rhino told me I had a problem. The error said I had a missing variable name. I could tell from the error message that the #288 on the end meant that it was on line 288. So, I went to line 288 in my JS file:
var default = defaults[0].split("@@");
Hmmm. That looks fine to me. What's going on? Have I used some kind of reserved word as a variable? So, I switched over to the JavaScript Console window and entered default at the prompt. Syntax error. Well, what happens if I enter an undeclared variable like "dude"? An error telling me "dude" is not defined. AH HA! So I have used some kind of reserved word.
So, long story (not so) short, I edited the JS file and changed var default = ... to var defaultFilter = ... and Rhino read it just fine. YEEE HA! Back to the web page. I fire up my browser, point to my web page and BAM! No more JavaScript error. After at least 2 shameful hours of head scratching that left me on the brink looking over at insanity below, the Rhino pulled me back to safety.
Thank you Rhino... thank you.
8:14:56 AM
|