close
Skip to content

code4fukui/estraverse

 
 

estraverse

NPM version Build Status

日本語のREADMEはこちらです: README.ja.md

Estraverse is an ECMAScript (JavaScript) AST traversal library. It provides functions to visit and manipulate nodes in an abstract syntax tree compliant with the ESTree specification. This project was originally part of the esmangle project.

Installation

npm install estraverse

Usage

The primary API is estraverse.traverse(), which accepts an AST and a visitor object. The visitor's enter and leave methods are called for each node in the tree.

import estraverse from 'estraverse';
import { parseScript } from 'esprima'; // Or any other ESTree-compliant parser

const ast = parseScript('const answer = 42;');

estraverse.traverse(ast, {
    enter: function (node, parent) {
        console.log('Entering:', node.type);
        // 'Program'
        // 'VariableDeclaration'
        // 'VariableDeclarator'
        // 'Identifier'
        // 'Literal'
    },
    leave: function (node, parent) {
        console.log('Leaving:', node.type);
        // 'Identifier'
        // 'Literal'
        // 'VariableDeclarator'
        // 'VariableDeclaration'
        // 'Program'
    }
});

API

estraverse.traverse(ast, visitor)

Traverses the AST and calls the visitor's enter and/or leave functions for each node.

estraverse.replace(ast, visitor)

Traverses the AST and allows nodes to be replaced. If an enter or leave function returns a valid

About

ECMAScript JS AST traversal functions

Resources

License

BSD-2-Clause, BSD-2-Clause licenses found

Licenses found

BSD-2-Clause
LICENSE
BSD-2-Clause
LICENSE.BSD

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • JavaScript 100.0%