Code Monkey home page Code Monkey logo

esprima-walk's Introduction

esprima-walk

A very fast esprima AST node walker with no dependencies.

To naïvely traverse the AST object with callbacks and without knowing its structure will result in an exponential explosion of function calls and could take several seconds for a single parsed file of a thousand or so lines.

esprima-walk calls no functions itself, only the callback passed in, and it doesn't invoke the callback on every property and value, only on primary nodes of the tree - those with a type property, corresponding to Node objects in the parser API. It also uses the fastest possible ways (on V8) to check types and iterate over properties and arrays.

The normal require( 'esprima-walk' ).walk method (also available as just require( 'esprima-walk' )) does not change the AST. It has no return value.

A method require( 'esprima-walk' ).walkAddParent is also available to add a .parent property to every node before invoking the callback on it. If a node has a parent, all of its ancestors will have a .parent property at that point too. The root node will have no .parent property.

Usage

var esprima = require( 'esprima' )
var walk = require( 'esprima-walk' )

var ast = esprima.parse( '"orange"' )

ast => {
    type: 'Program',
    body: [
        {
            type: 'ExpressionStatement',
            expression: {
                type: 'Literal',
                value: 'orange',
                raw: '"orange"'
            }
        }
    ]
}

var types = []

walk( ast, function ( node ) { types.push( node.type ) } )

types => [ 'Program', 'ExpressionStatement', 'Literal' ]

esprima-walk's People

Contributors

jonvuri avatar psyked avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.