Checking For Script Syntax Errors
A reader asked me recently whether there was a way to check a chunk of JScript or VBScript for syntax errors without actually running the code. I'm sure that there are many third-party tools which you could find that do this. If you have your own script host, you can do it yourself quite easily.
The trick is to initialize the engine by setting the script site, but do not do anything that would move the engine from initialized into started state. (Note that attempting to evaluate an expression moves the engine to started state.)
If the engine is initialzed but not started then calling ParseScriptText will check the text passed in for syntax errors but will not run the script. Rather, if the script compiles successfully, it is simply marked as "needs to run when the engine is started".
Unfortunately the script engines do not have an error-recovering parser, so they will detect only the first syntax error and then bail out. Still, better than nothing.
Comments
Anonymous
October 12, 2005
Sorry, could you clarify? I have a saved script named blah.vbs. Exactly what do I do to have it syntax checked via the method that you describe?Anonymous
October 12, 2005
Start by writing your own script host. You'll need to implement IActiveScriptSite, but since you won't actually be running any script, most of the methods can simply return E_NOTIMPL.
Then cocreate the script engine, queryinterface it for IActiveScript, set the script site to your site object to initialize the engine, and call ParseScriptText with the contents of the file you want to check. Your script site's error handler will be called back if there is a syntax error.Anonymous
March 22, 2006
What's wrong with:
var aScripts = ["x=false; x|=!x", "x=true; x||=!x"]
for (var i=0;i<aScripts.length;++i)
jsValidator(aScripts[i]) ? alert('Script'+i+' is OK') :
alert ('Script '+i+' parsing error withn'+aScripts[i]);
function jsValidator(scriptChunk) {
try {
eval("function() {"+scriptChunk+"}");
return true;
} catch(e) { return false; }
}
This idea is also seen at:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/de19d5a5c5a17092/
Csaba Gabor from ViennaAnonymous
March 18, 2007
If the code contains functions and subroutines only (no event handlers), the code will validate without running.Anonymous
May 16, 2007
You may want to consider Javascript Lint or JSLintAnonymous
September 13, 2007
What is the function name in VB.NET that is equal to eval function in VBScript?Anonymous
September 17, 2007
I am working with VB.NET, I have many mathematical equations in my windows application, which differ according to many options I selected from previous form. So I m using thesse equation as strings in database and according to the selected options from the previous form it drag the its equation, which I concatinate to a variable (number) according to the selected age. So this equation doesn't work because it defined as nvchar in the databse and the variable is number, I tried the function eval() which I use in asp to solve this but it didn't work. I want to know if there is a function in VB.NET can solve this problem or what can I do to solve it.Anonymous
July 20, 2009
The comment has been removed