Verify JavaScript syntax using C#

by Mads Kristensen 28. October 2009 17:10

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)

Tags: ,

Client-side

Comments

10/28/2009 6:40:23 PM #

Bertrand Le Roy

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

Bertrand Le Roy United States |

10/28/2009 9:50:56 PM #

Rush

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

Rush United States |

10/29/2009 4:39:18 AM #

Mads Kristensen

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

Mads Kristensen Denmark |

10/29/2009 4:36:16 AM #

pingback

Pingback from blog.cwa.me.uk

Reflective Perspective - Chris Alcock  » The Morning Brew #465

blog.cwa.me.uk |

10/29/2009 8:25:58 AM #

DC27

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.

DC27 United Kingdom |

11/9/2009 9:57:00 PM #

bondage

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

bondage United States |

10/29/2009 9:27:40 AM #

Klaus Graefensteiner

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

Klaus Graefensteiner United States |

10/29/2009 10:32:52 AM #

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        Verify JavaScript syntax using C#
        [madskristensen.net]
        on Topsy.com

topsy.com |

10/29/2009 11:43:24 AM #

trackback

Social comments and analytics for this post

This post was mentioned on Twitter by fabianfigueredo: Verify JavaScript syntax using C# http://bit.ly/1N0MiR

uberVU - social comments |

10/29/2009 10:17:53 PM #

Internet

This is an interesting idea.

Internet Spain |

10/30/2009 11:42:22 AM #

trackback

Verify JavaScript syntax using C#

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

PimpThisBlog.com |

10/31/2009 2:28:57 PM #

Memory Card Data Recovery Software

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.

Memory Card Data Recovery Software United States |

11/1/2009 3:15:40 AM #

pingback

Pingback from jasper22.wordpress.com

Verify JavaScript syntax using C# « Jasper Blog

jasper22.wordpress.com |

11/1/2009 6:49:04 AM #

Minisite Design

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

Minisite Design United States |

11/6/2009 5:59:47 PM #

bondage porn

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 porn United States |

11/24/2009 8:04:26 AM #

pingback

Pingback from driessen.nl

Tech Talk – Driessen HRM _ Payroll  » Blog Archief  » Donkere uithoeken van .NET – Driessen HRM _ Payroll – Goed Bekend

driessen.nl |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008