Fabulous Adventures In Coding

Eric Lippert's Blog

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.

 

Published Tuesday, October 11, 2005 10:35 AM by Eric Lippert
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

TC said:

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?
October 12, 2005 10:08 AM
 

Eric Lippert said:

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.

October 12, 2005 10:50 AM
 

Csaba Gabor said:

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 with\n'+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 Vienna
March 22, 2006 4:46 PM
 

Christopher Yeleighton said:

If the code contains functions and subroutines only (no event handlers),

the code will validate without running.

March 18, 2007 2:33 PM
 

Ben said:

You may want to consider Javascript Lint or JSLint

May 16, 2007 11:57 AM
 

Mona said:

What is the function name in VB.NET that is equal to eval function in VBScript?

September 13, 2007 8:10 AM
 

Mona said:

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.

September 18, 2007 4:13 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Eric Lippert

Eric Lippert is a senior developer on the Microsoft C# compiler team. Before that he worked on the framework of Visual Studio Tools For Office. Before that, he worked on the compilers, runtimes and tools for VBScript, JScript, Windows Script Host and other Microsoft Scripting technologies. He lives in Seattle and spends his free time editing books about programming languages, playing the piano, and trying to keep his tiny sailboat upright in Puget Sound.

This Blog

Syndication


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker