Esprima 3.0.4

:mega: Important notices
If you are upgrading from an older version, please note that version 3 ships with numerous breaking changes to the public API because virtually all areas of the library have been revised.
Documentation of the previous major version is available here.

Build NuGet MyGet

Esprima .NET (BSD license) is a .NET port of the esprima.org project. It is a standard-compliant ECMAScript parser (also popularly known as JavaScript).

Features

API

Esprima can be used to perform lexical analysis (tokenization) or syntactic analysis (parsing) of a JavaScript program.

A simple C# example:

var parser = new JavaScriptParser();
var program = parser.ParseScript("const answer = 42");

You can control the behavior of the parser by initializing and passing a ParserOptions to the parser's constructor. (For the available options, see the XML documentation of the ParserOptions class.)

Instead of ParseScript, you may use ParseModule or ParseExpression to make the parser treat the input as an ES6 module or as a plain JavaScript expression respectively.

In case the input is syntactically correct, each of these methods returns the root node of the resulting abstract syntax tree (AST), which you can freely analyze or transform. The library provides the AstVisitor and AstRewriter visitor classes to help you with such tasks.

When the input contains a severe syntax error, a ParserException is thrown. By catching it you can get details about the error. There are syntax errors though which can be tolerated by the parser. Such errors are ignored by default. You can record them by setting ParserOptions.ErrorHandler to an instance of CollectingErrorHandler. Alternatively, you can set ParserOptions.Tolerant to false to make the parser throw exceptions also in the case of tolerable syntax errors.

The library is able to write the AST (except for comments) back to JavaScript code:

var code = program.ToJavaScriptString(format: true);

It is also possible to serialize the AST into a JSON representation:

var astJson = program.ToJsonString(indent: "    ");

Considering the example above this call will return the following JSON:

{
    "type": "Program",
    "body": [
        {
            "type": "VariableDeclaration",
            "declarations": [
                {
                    "type": "VariableDeclarator",
                    "id": {
                        "type": "Identifier",
                        "name": "answer"
                    },
                    "init": {
                        "type": "Literal",
                        "value": 42,
                        "raw": "42"
                    }
                }
            ],
            "kind": "const"
        }
    ],
    "sourceType": "script",
    "strict": false
}

Benchmarks

Here is a list of common JavaScript libraries and the time it takes to parse them, compared to the time it took for the same script using the original Esprima in Chrome.

Script Size Esprima .NET (.NET 6) Esprima (Chrome 116)
underscore-1.5.2 43 KB 1.0 ms 1.4 ms
backbone-1.1.0 59 KB 1.2 ms 1.6 ms
mootools-1.4.5 157 KB 5.2 ms 7.1 ms
jquery-1.9.1 262 KB 6.6 ms 7.9 ms
yui-3.12.0 330 KB 4.6 ms 6.9 ms
jquery.mobile-1.4.2 442 KB 10.0 ms 17.7 ms
angular-1.2.5 702 KB 8.5 ms 15.1 ms

Showing the top 20 packages that depend on Esprima.

Packages Downloads
Jint
Javascript interpreter for .NET.
239
Jint
Javascript interpreter for .NET.
18
Jint
Javascript interpreter for .NET which provides full ECMA 5.1 compliance.
17
Jint
Javascript interpreter for .NET.
17
Jint
Javascript interpreter for .NET which provides full ECMA 5.1 compliance.
16
Jint
Javascript interpreter for .NET.
16
Jint
Javascript interpreter for .NET.
15

.NET Framework 4.6.2

.NET Standard 2.0

.NET Standard 2.1

  • No dependencies.

Version Downloads Last updated
3.0.5 9 04/24/2024
3.0.4 164 02/01/2024
3.0.3 9 02/01/2024
3.0.2 8 11/16/2023
3.0.1 8 10/11/2023
3.0.0 13 10/10/2023
3.0.0-rc-04 12 08/24/2023
3.0.0-rc-03 9 11/02/2023
3.0.0-rc-02 14 06/05/2023
3.0.0-rc-01 15 06/05/2023
3.0.0-beta-9 15 06/05/2023
3.0.0-beta-8 13 06/05/2023
3.0.0-beta-7 17 06/05/2023
3.0.0-beta-6 14 06/05/2023
3.0.0-beta-5 15 06/05/2023
3.0.0-beta-4 12 06/05/2023
3.0.0-beta-3 14 06/05/2023
3.0.0-beta-2 18 06/05/2023
3.0.0-beta-10 17 06/05/2023
3.0.0-beta-1 16 06/05/2023
2.1.3 16 06/05/2023
2.1.2 15 06/05/2023
2.1.1 15 06/05/2023
2.1.0 16 06/05/2023
2.0.5 15 06/05/2023
2.0.4 16 06/05/2023
2.0.3 14 06/05/2023
2.0.2 15 06/05/2023
2.0.1 16 06/05/2023
2.0.0 13 06/05/2023
2.0.0-beta-1339 17 06/05/2023
2.0.0-beta-1338 17 06/05/2023
2.0.0-beta-1328 15 06/05/2023
2.0.0-beta-1317 14 06/05/2023
2.0.0-beta-1311 17 06/05/2023
2.0.0-beta-1303 18 06/05/2023
2.0.0-beta-1302 13 06/05/2023
2.0.0-beta-1298 15 06/05/2023
2.0.0-beta-1295 17 06/05/2023
2.0.0-beta-1292 16 06/05/2023
1.0.1270 12 06/05/2023
1.0.1258 14 06/05/2023
1.0.1251 18 06/05/2023
1.0.1246 14 06/05/2023
1.0.0-beta-1202 15 06/05/2023
1.0.0-beta-1186 16 06/05/2023
1.0.0-beta-1178 18 06/05/2023
1.0.0-beta-1070 16 06/05/2023
1.0.0-beta-1051 13 06/05/2023
1.0.0-beta-1036 16 06/05/2023
1.0.0-beta-1026 17 06/05/2023
1.0.0-beta-1023 16 06/05/2023
1.0.0-beta-1012 14 06/05/2023
1.0.0-beta-001 18 06/05/2023
1.0.0-alpha-003 12 06/05/2023
1.0.0-alpha-002 13 06/05/2023
1.0.0-alpha-001 15 06/05/2023