Verify JavaScript syntax using C#

Oct 28, 2009

In the past few days, I’ve worked on finding a way to do static code analysis on JavaScript files.The resaon is that I want to apply some sort of binary and source code checking like FxCop and StyleCop provides for C#.

There exist some tools for linting JavaScript like JavaScript Lint, but linting only checks syntax and not implementation. To do that I found the Jscript compiler build into the .NET Framework to be just what I wanted. It compiles JavaScript and reports if it finds any errors.

To test it out, I wrote a simple C# class that takes an array of JavaScript files to compile. I then called the class from a unit test, so I could make the test fail if the compiler finds any errors with the script files. The class contains a single public method called Compile and here is a simplified example on how to use it from any unit testing framework. You can download the class at the bottom of this post.

[Test]

public void JavascriptTest()

{

  string[] javascriptFiles = Directory.GetFiles(@"D:\Website", "*.js",
SearchOption.AllDirectories);

 

  using (JavaScriptCompiler compiler = new JavaScriptCompiler())

  {

    compiler.Compile(javascriptFiles);

    Assert.IsFalse(compiler.HasErrors, compiler.ErrorMessage);

  }

}

What’s nice is that by doing compile time checking of JavaScripts, I get that extra little security that’s so hard to get when developing JavaScript heavy websites. The Microsoft JScript compiler isn’t perfect, so I still recommend using a linting tool as well. The two approaches cover different scenarios. I hope to have a very simple implementation on using a linting tool soon.

Download

Remember to add a reference to Microsoft.JScript in your Visual Studio project before using this class.

JavaScriptCompiler.cs (2,48 kb)

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (10) -

Bertrand Le Roy
Bertrand Le Roy United States
10/28/2009 3:40:23 PM #

Script# is also an interesting way to build statically checked scripts.

Rush
Rush United States
10/28/2009 6:50:56 PM #

Do you know of anything similar to this within the Microsoft world that will check classic ASP?

Mads Kristensen
Mads Kristensen Denmark
10/29/2009 1:39:18 AM #

No, sorry. Maybe there is something in the Microsoft.VisualBasic namespace?

DC27
DC27 United Kingdom
10/29/2009 5:25:58 AM #

This is brilliant, thank you.  I have a couple of websites with pretty complex javascript and testing that you haven't broken something is a real chore.  This should certainly speed up the syntax checking side of things.

bondage
bondage United States
11/9/2009 6:57:00 PM #

Script# is also an interesting way to build statically checked scripts.

Klaus Graefensteiner
Klaus Graefensteiner United States
10/29/2009 6:27:40 AM #

This is an interesting idea. I'll at this test to my projects.

Internet
Internet Spain
10/29/2009 7:17:53 PM #

This is an interesting idea.

Memory Card Data Recovery Software
Memory Card Data Recovery Software United States
10/31/2009 11:28:57 AM #

I have a couple of websites with pretty complex javascript and testing that you haven't broken something is a real chore.  This should certainly speed up the syntax checking side of things.

Minisite Design
Minisite Design United States
11/1/2009 3:49:04 AM #

Thanks for great posting, will back to read other useful post ;)

bondage porn
bondage porn United States
11/6/2009 2:59:47 PM #

This is brilliant, thank you.  I have a couple of websites with pretty complex javascript and testing that you haven't broken something is a real chore.  This should certainly speed up the syntax checking side of things.

Pingbacks and trackbacks (6)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.