1. esprima-walk
Walk the primary nodes of an esprima parse tree - fast.
esprima-walk
Package: esprima-walk
Created by: jrajav
Last modified: Fri, 17 Jun 2022 20:52:10 GMT
Version: 0.1.0
License: MIT
Downloads: 8,315
Repository: https://github.com/jrajav/esprima-walk

Install

npm install esprima-walk
yarn add esprima-walk

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' ]

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin