Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Parser and Analyzer in C# .NET 4.5

Not sure if the title explains it correctly.

Anyways, I'm building a .NET WPF application which should go through the JavaScript and identify issues such as

  1. If the variables defined are being nullified at the end
  2. If try/catch/finally blocks are being used.
  3. Function calls

I went through the questions over here which were all revolving around c/c++. Now I regret bunking my compilers classes.

I wanted to know how to verify points 1-3 in C#. Any library out there which does this?

like image 698
Dev Avatar asked Jan 16 '13 10:01

Dev


1 Answers

What you're looking for is an abstract syntax tree parser for Javascript written in C#.

There are a few choices I know of:

Microsoft's Ajax Minifier library comes with its own AST parser (used to minify / optimize Javascript files). You can find the source code for that on GitHub.

Esprima.net is another option. It's a port of the popular Javascript library Esprima.

The good thing about Esprima is it outputs the AST in a common format (defined by Mozilla here) that's used across a few parsers, making it really easy to port utilities for walking the tree, etc. since they all use the same underlying data structure.

like image 85
Cuong Vo Avatar answered Oct 14 '22 10:10

Cuong Vo