Socket
Socket
Sign inDemoInstall

estree-walker

Package Overview
Dependencies
1
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Install Socket

Protect your apps from supply chain attacks

Install

estree-walker

Traverse an ESTree-compliant AST

    3.0.3latest
    GitHub
    npm

Version published
Maintainers
1
Weekly downloads
21,975,359
increased by3.79%

Weekly downloads

Package description

What is estree-walker?

The estree-walker package is a simple utility for walking an ESTree-compliant AST (Abstract Syntax Tree), such as those produced by parsers like Acorn or Esprima. It allows users to traverse the tree and manipulate nodes during the traversal.

What are estree-walker's main functionalities?

Walking an AST

This feature allows you to traverse an AST, performing actions when entering and leaving each node. The 'enter' function is called when a node is entered, and the 'leave' function is called when a node is left during the traversal.

const { walk } = require('estree-walker');

const ast = { /* some AST object */ };

walk(ast, {
  enter(node, parent, prop, index) {
    // Perform actions upon entering a node
  },
  leave(node, parent, prop, index) {
    // Perform actions upon leaving a node
  }
});

Manipulating Nodes

This feature demonstrates how you can manipulate nodes during traversal. In this example, numeric literal values are doubled.

const { walk } = require('estree-walker');

const ast = { /* some AST object */ };

walk(ast, {
  enter(node) {
    if (node.type === 'Literal' && typeof node.value === 'number') {
      node.value *= 2; // Double the number
    }
  }
});

Other packages similar to estree-walker

Changelog

Source

3.0.3

  • Add dependencies on @types/estree (#34)
  • Internal refactoring (#34)

Readme

Source

estree-walker

Simple utility for walking an ESTree-compliant AST, such as one generated by acorn.

Installation

npm i estree-walker

Usage

var walk = require('estree-walker').walk;
var acorn = require('acorn');

ast = acorn.parse(sourceCode, options); // https://github.com/acornjs/acorn

walk(ast, {
  enter(node, parent, prop, index) {
    // some code happens
  },
  leave(node, parent, prop, index) {
    // some code happens
  }
});

Inside the enter function, calling this.skip() will prevent the node's children being walked, or the leave function (which is optional) being called.

Call this.replace(new_node) in either enter or leave to replace the current node with a new one.

Call this.remove() in either enter or leave to remove the current node.

Why not use estraverse?

The ESTree spec is evolving to accommodate ES6/7. I've had a couple of experiences where estraverse was unable to handle an AST generated by recent versions of acorn, because it hard-codes visitor keys.

estree-walker, by contrast, simply enumerates a node's properties to find child nodes (and child lists of nodes), and is therefore resistant to spec changes. It's also much smaller. (The performance, if you're wondering, is basically identical.)

None of which should be taken as criticism of estraverse, which has more features and has been battle-tested in many more situations, and for which I'm very grateful.

License

MIT

FAQs

Last updated on 20 Jan 2023

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc