Navigating through the JavaScript AST (Esprima.Net) and converting it into a tree structure using C#

Utilizing Esprima.Net (https://github.com/Diullei/Esprima.NET), I extracted the AST (Abstract Syntax Tree) from a piece of JavaScript code. The output is a collection of nodes containing various child and sub-child elements. I am pondering the most efficient way to navigate through these nodes in C# for analysis purposes, specifically aiming to extract function names, variable names, and their corresponding functions.

Consider the following snippet of JavaScript:

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

My desired outcome would be:

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

I am encountering difficulties navigating the List<Dynamic> provided by Esprima.Net. Does anyone have suggestions on how to process or traverse this list in a tree or some other structure that allows easy access? Thank you.

Answer №1

Instead of utilizing Esprima.NET, I opted for Esprima JS () in my project. By incorporating Esprima JS into the webpage and creating an external JavaScript file that invoked the Esprima parser to generate Abstract Syntax Trees (AST), I was able to process the code effectively. Subsequently, I employed estraverse (https://github.com/estools/estraverse) to traverse the AST and obtain desired outcomes.

This approach may be beneficial to others facing similar challenges.

Answer №2

If you're looking for a JavaScript interpreter in .NET with an internal port of Esprima (ES5), consider using Jint. It provides the same AST as Esprima.

Alternatively, you can check out Esprima.NET, which is based on ES6 and is distributed separately from Jint.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Experiencing difficulties with setting up SearchApp

Issue: I am encountering a problem with my cloudant account after attempting to install the SearchApp from this link. The installation process was successful, but when I try to use the search functionality on the site, nothing happens. Query The website ...

JSONP OpenWeather API

I've been trying to access and retrieve weather data from OpenWeather using their API, but unfortunately, I'm facing some difficulties in getting it to work. It seems like there might be an issue with the way I am fetching the JSON data. To quer ...

Choosing a value from an HTML5 datalist using Selenium

When attempting to choose from a datalist, I am encountering an issue where only the first element in the list is selectable. Below is an example of the HTML snippet: <td> <input id="applianceFilterTextbox" class="flat" name="applianceFilter" li ...

What is the best way to populate an object with an array's elements using JSON?

I previously inquired about a similar question, but with some variations. You can view it here. Since then, I have altered my approach to tackling the issue. You can find my JSON file here. This is the progress I've made with my javascript: $(docu ...

Having Trouble with Sending Emails Using Google Scripts - Javascript POST Request Issue

I have been working on setting up a basic form on my website where users can input their name, email, and a short message. This information is then sent to Google Apps Script which forwards the message to me via email. Unfortunately, I keep encountering an ...

Obtaining the FEN from a non-nested chess board list using JavaScript

My chess Program.js is not running smoothly; I need to extract the FEN from the board list that looks like this board1 = [ "R", "N", "B", "K", "Q", "B", "N", "Q", ...

Icons are not compatible with the picturebox in C#

Let me start off by stating that I own two computers - a notebook and a desktop PC. When working on my Desktop, adding a 16x16 icon is as simple as selecting it for the picturebox. However, the same process on my Notebook results in the icon not fitting p ...

Troubleshooting CSS Hover Not Displaying Properly in Angular 14

In the footer class of my HTML, there is a code snippet with chevrons: <div class="link-list"> <div *ngFor="let link of insideLinksLeft | originalOrderKeyValue" class="link"> <a [href]="link.val ...

Converting Byte Array to Image in C#

Looking for advice on converting a simple byte string (matrix elements read row by row) into an image using C#. Is there an easy way to accomplish this task? I came across byte array to image conversion methods online, but it seems like the byte string ne ...

The y-axis max property in Highcharts does not effectively hide plot bands and labels

I have set up two separate jsfiddles to demonstrate my issue. The first jsfiddle represents the desired behavior, while the second one displays the problem I am encountering. In the first jsfiddle (https://jsfiddle.net/n5ua6krj/1/), the y-axis[0] does not ...

Animation displayed on page load before running

Is there a way to ensure that my animation runs smoothly without any lag or jankiness, either while the page is loading or immediately after loading? I have attempted to preload the images using a preload tag in the header of my HTML, but unfortunately, t ...

Tips for altering the currently active tab in a separate window using a browser extension?

I'm currently working on developing a Firefox Extension and I'm facing a challenge. I'm trying to navigate to a specific browser tab in a different window. After reading through the Firefox Browser Extensions API documentation, I learned tha ...

What is the name of the JavaScript code editor that includes line numbering for plain text?

Looking to incorporate a text area with line numbering features. I experimented with EditArea, but encountered difficulties when working with text files. While syntax highlighting for various programming languages would be a nice touch, my primary focus ...

Attempting to retrieve the current time using JavaSscript

const currentTime = new Date(); const hours = now.getHours(); Console.log(hours); This block of code is returning an error message... Uncaught ReferenceError: now is not defined Please note that this snippet is written in JavaScript. I attempted to us ...

Having trouble locating the variable "module" or "inject" when testing an AngularJS controller using Chutzpah

I am working with an AngularJS controller angular.module('App.ctrl.guests', []) .controller('guestsController', ['$scope', '$http', '$location', '$timeout', 'guestsService', functio ...

Lock it up or leave it open - that is the question

My JavaScript/jQuery web application features an object that is accessed for reading and writing by users through DOM events, as well as by the server via web sockets or xhr requests. Although I am aware that JavaScript is single-threaded, I have concerns ...

Implementing TypeScript type definitions for decorator middleware strategies

Node middlewares across various frameworks are something I am currently pondering. These middlewares typically enhance a request or response object by adding properties that can be utilized by subsequent registered middlewares. However, a disadvantage of ...

Develop a unit test specifically designed for Xsockets

I am delving into the world of unit tests and trying to understand how to effectively use them. Specifically, I aim to test the Send and Receive functions in Xsockets using Nunit. However, when I execute the test currently, nothing seems to be happening a ...

Proceed the flow of event propagation using the react-aria button element

In the react-aria library, event bubbling for buttons is disabled. I am facing an issue where my button, which is nested inside a div that acts as a file uploader, does not trigger the file explorer when clicked due to event bubbling being disabled. How ...

Tips for Crafting a Query in MongoDB using MongoDBRef

I've encountered an issue while working with the MongoDB official driver (10Gen). Specifically, I am struggling to query a MonogoDBRef property within my code base. The classes involved are as follows: public class UserData { private ObjectId id; ...