estraverse vs esprima
JavaScript Parsing and Traversal Libraries Comparison
1 Year
estraverseesprimaSimilar Packages:
What's JavaScript Parsing and Traversal Libraries?

Esprima is a high-performance, standard-compliant ECMAScript parser that produces a syntax tree from JavaScript code. It allows developers to analyze and manipulate JavaScript code programmatically. Estraverse, on the other hand, is a library that provides methods for traversing and manipulating the syntax trees generated by parsers like Esprima. While Esprima focuses on parsing, Estraverse is used for walking through the tree and performing operations on the nodes, making them complementary tools in JavaScript code analysis and transformation.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
estraverse94,361,862950-393 years agoBSD-2-Clause
esprima51,659,5967,082-1477 years agoBSD-2-Clause
Feature Comparison: estraverse vs esprima

Parsing Capability

  • estraverse:

    Estraverse does not parse code; instead, it operates on an existing AST. It provides a robust set of functions to traverse the tree, allowing developers to visit each node and perform specific actions. This is essential for tools that need to analyze or modify the structure of the code after parsing.

  • esprima:

    Esprima excels in parsing JavaScript code into a well-structured Abstract Syntax Tree (AST). It supports the latest ECMAScript standards, ensuring that modern JavaScript features are accurately represented in the generated AST. This makes it a go-to choice for tools that require precise parsing of JavaScript syntax.

Performance

  • estraverse:

    Estraverse is lightweight and efficient for tree traversal. It is optimized for performance, allowing developers to traverse and manipulate ASTs without significant overhead, making it suitable for large-scale code transformations.

  • esprima:

    Esprima is designed for high performance, making it one of the fastest JavaScript parsers available. Its efficiency is crucial for applications that need to parse large codebases quickly, such as linters or code analysis tools.

Ease of Use

  • estraverse:

    Estraverse offers a clear and intuitive API for traversing ASTs. Its methods allow for easy navigation through the tree, making it user-friendly for developers who want to implement custom logic during traversal.

  • esprima:

    Esprima provides a simple API for parsing JavaScript code, making it easy to integrate into projects. Developers can quickly get started with minimal setup, which is beneficial for both beginners and experienced developers looking for a straightforward parsing solution.

Integration

  • estraverse:

    Estraverse is designed to work seamlessly with ASTs generated by various parsers, including Esprima. This compatibility allows developers to use Estraverse in conjunction with Esprima or other parsing libraries, enhancing its utility in code analysis and transformation workflows.

  • esprima:

    Esprima can be easily integrated with other tools and libraries in the JavaScript ecosystem. It is often used in conjunction with code analysis tools, linters, and transpilers, making it a versatile choice for developers looking to build comprehensive JavaScript tooling.

Community and Support

  • estraverse:

    Estraverse, while less prominent than Esprima, is still supported by a dedicated community. Its documentation provides clear guidance on how to use the library effectively, and it is often referenced in projects that involve AST manipulation.

  • esprima:

    Esprima has a strong community and is widely used in the JavaScript ecosystem. It is well-documented, and there are many resources available for developers, including tutorials and examples, which facilitate learning and troubleshooting.

How to Choose: estraverse vs esprima
  • estraverse:

    Choose Estraverse if you already have an AST and need to traverse or manipulate it. It is particularly useful for building tools that need to analyze or transform the structure of JavaScript code after it has been parsed.

  • esprima:

    Choose Esprima if you need a reliable and fast parser to convert JavaScript code into an Abstract Syntax Tree (AST). It is ideal for applications that require parsing JavaScript code for analysis, linting, or code transformation.

README for estraverse

Estraverse Build Status

Estraverse (estraverse) is ECMAScript traversal functions from esmangle project.

Documentation

You can find usage docs at wiki page.

Example Usage

The following code will output all variables declared at the root of a file.

estraverse.traverse(ast, {
    enter: function (node, parent) {
        if (node.type == 'FunctionExpression' || node.type == 'FunctionDeclaration')
            return estraverse.VisitorOption.Skip;
    },
    leave: function (node, parent) {
        if (node.type == 'VariableDeclarator')
          console.log(node.id.name);
    }
});

We can use this.skip, this.remove and this.break functions instead of using Skip, Remove and Break.

estraverse.traverse(ast, {
    enter: function (node) {
        this.break();
    }
});

And estraverse provides estraverse.replace function. When returning node from enter/leave, current node is replaced with it.

result = estraverse.replace(tree, {
    enter: function (node) {
        // Replace it with replaced.
        if (node.type === 'Literal')
            return replaced;
    }
});

By passing visitor.keys mapping, we can extend estraverse traversing functionality.

// This tree contains a user-defined `TestExpression` node.
var tree = {
    type: 'TestExpression',

    // This 'argument' is the property containing the other **node**.
    argument: {
        type: 'Literal',
        value: 20
    },

    // This 'extended' is the property not containing the other **node**.
    extended: true
};
estraverse.traverse(tree, {
    enter: function (node) { },

    // Extending the existing traversing rules.
    keys: {
        // TargetNodeName: [ 'keys', 'containing', 'the', 'other', '**node**' ]
        TestExpression: ['argument']
    }
});

By passing visitor.fallback option, we can control the behavior when encountering unknown nodes.

// This tree contains a user-defined `TestExpression` node.
var tree = {
    type: 'TestExpression',

    // This 'argument' is the property containing the other **node**.
    argument: {
        type: 'Literal',
        value: 20
    },

    // This 'extended' is the property not containing the other **node**.
    extended: true
};
estraverse.traverse(tree, {
    enter: function (node) { },

    // Iterating the child **nodes** of unknown nodes.
    fallback: 'iteration'
});

When visitor.fallback is a function, we can determine which keys to visit on each node.

// This tree contains a user-defined `TestExpression` node.
var tree = {
    type: 'TestExpression',

    // This 'argument' is the property containing the other **node**.
    argument: {
        type: 'Literal',
        value: 20
    },

    // This 'extended' is the property not containing the other **node**.
    extended: true
};
estraverse.traverse(tree, {
    enter: function (node) { },

    // Skip the `argument` property of each node
    fallback: function(node) {
        return Object.keys(node).filter(function(key) {
            return key !== 'argument';
        });
    }
});

License

Copyright (C) 2012-2016 Yusuke Suzuki (twitter: @Constellation) and other contributors.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.