All Projects → estools → Estraverse

estools / Estraverse

Licence: bsd-2-clause
ECMAScript JS AST traversal functions

Programming Languages

javascript
184084 projects - #8 most used programming language
ecmascript
72 projects

Labels

Projects that are alternatives of or similar to Estraverse

Tiny Compiler
A tiny compiler for a language featuring LL(2) with Lexer, Parser, ASM-like codegen and VM. Complex enough to give you a flavour of how the "real" thing works whilst not being a mere toy example
Stars: ✭ 425 (-46.54%)
Mutual labels:  ast
Webassemblyjs
Toolchain for WebAssembly
Stars: ✭ 566 (-28.81%)
Mutual labels:  ast
Js Code To Svg Flowchart
js2flowchart - a visualization library to convert any JavaScript code into beautiful SVG flowchart. Learn other’s code. Design your code. Refactor code. Document code. Explain code.
Stars: ✭ 6,290 (+691.19%)
Mutual labels:  ast
Qone
Next-generation web query language, extend .NET LINQ for javascript.
Stars: ✭ 463 (-41.76%)
Mutual labels:  ast
Escope
Escope: ECMAScript scope analyzer
Stars: ✭ 497 (-37.48%)
Mutual labels:  ast
Tiny Compiler
A tiny evaluator and compiler of arithmetic expressions.
Stars: ✭ 680 (-14.47%)
Mutual labels:  ast
Cst
🌿 JavaScript Concrete Syntax Tree
Stars: ✭ 417 (-47.55%)
Mutual labels:  ast
Php Parser
PHP parser written in Go
Stars: ✭ 787 (-1.01%)
Mutual labels:  ast
Esquery
ECMAScript AST query library.
Stars: ✭ 513 (-35.47%)
Mutual labels:  ast
Tolerant Php Parser
An early-stage PHP parser designed for IDE usage scenarios.
Stars: ✭ 717 (-9.81%)
Mutual labels:  ast
Fantomas
FSharp source code formatter
Stars: ✭ 475 (-40.25%)
Mutual labels:  ast
Mdast
Markdown Abstract Syntax Tree format
Stars: ✭ 493 (-37.99%)
Mutual labels:  ast
Meriyah
A 100% compliant, self-hosted javascript parser - https://meriyah.github.io/meriyah
Stars: ✭ 690 (-13.21%)
Mutual labels:  ast
Unist
Universal Syntax Tree used by @unifiedjs
Stars: ✭ 438 (-44.91%)
Mutual labels:  ast
Esprima
ECMAScript parsing infrastructure for multipurpose analysis
Stars: ✭ 6,391 (+703.9%)
Mutual labels:  ast
Debundle
🗃 A javascript debundler. Takes a Browserify or Webpack bundle and recreates the initial, pre-bundled source.
Stars: ✭ 420 (-47.17%)
Mutual labels:  ast
Rehype
HTML processor powered by plugins part of the @unifiedjs collective
Stars: ✭ 634 (-20.25%)
Mutual labels:  ast
Micromark
the smallest commonmark compliant markdown parser that exists; new basis for @unifiedjs (hundreds of projects w/ billions of downloads for dealing w/ content)
Stars: ✭ 793 (-0.25%)
Mutual labels:  ast
Astring
🌳 Tiny and fast JavaScript code generator from an ESTree-compliant AST.
Stars: ✭ 757 (-4.78%)
Mutual labels:  ast
Postcss
Transforming styles with JS plugins
Stars: ✭ 25,612 (+3121.64%)
Mutual labels:  ast

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].