2

I used Esprima.Net (https://github.com/Diullei/Esprima.NET) to get AST (Abstract Syntax Key) from a JavaScript code. It returns a List<Dynamic> consisting many child and sub-child nodes. I wonder how best to traverse all these nodes in C# for analysis. Basically I want to get the function name, variable name & function that it is under.

For example, in the following JavaScript code:

var y = 45;
function fTest(d)
{
   var key: Argument.Callee;
   var cars = 'Hello';
   for (i = 0; i < cars.length; i++) 
   { 
     text += cars[i];
   }
}

I wish to get the following result at the end:

variable: 45
function:parameter:'d'
function:variable:argument.callee
function:variable:'Hello'
funtion:loop:variable:object

I'm having a difficulty to traverse the List<Dynamic> given by Esprima.Net. Any ideas to process or traverse this list in a Tree or any structure so that I can access them? Thanks.

2
  • I did something with Esprima in the past and I ended up implementing it using NodeJS. Since Esprima is made in JS (ok, there's this .NET port... but I mean the official one), analyzing JS from JS feels a bit more simple. Maybe you can also do the whole work using Jurassic and then read the results from .NET. Commented Sep 13, 2015 at 7:02
  • After looking around, it seems that Jurassic does not create AST unless modification to its code is made. I ended up using Esprima JS and an external Javascript as mentioned below. Commented Sep 16, 2015 at 11:25

2 Answers 2

2

I ended up not using the Esprima.NET but Esprima JS (http://esprima.org/). I added Esprima JS in webpage and create an external javascript file that called Esprima parser to create AST. Once I had the AST, I used estraverse (https://github.com/estools/estraverse) to traverse the AST to get results.

Hope this helps others.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use Jint which is a JavaScript interpreter in .NET, and has an internal port of Esprima (ES5). It returns the same AST as Esprima.

Or you can use this other Esprima.NET that is based on ES6 and distributed separately from Jint.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.