npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

estree-walk

v2.2.0

Published

Walk ESTree nodes simple and fast

Downloads

9,713

Readme

estree-walk

Walk ESTree nodes simple and fast

// Walk tree with a visitor
walk(source, {
  ReturnStatement: function (node, stop) {
    // You can call `stop` to exit the walking
  }
})

// Walk tree with a fast alrenative:
for (var q = [source], node; node = q.pop(); walk.step(node, q)) {
  switch (node.type) {
    // ...
  }
}

Functions for walking ESTree nodes. Like others, it attempts to stay future-proof by enumerating the node's keys instead of handling the node's type, while also providing simple usage.

Installation

npm i estree-walk

Usage

There is two methods of walking trees with this library:

  1. Using a visitor pattern with walk(node, visitor)
  2. Using a looping pattern with walk.step(node, queue)

walk(node, visitor)

Walks a node tree using a visitor. A visitor can be a function that executes for all nodes, or an object of functions that execute for a given node type. Visitor functions have the signature (node, stop?), where stop can be called to exit quickly.

// Visit by node type
walk(node, {
  FunctionDeclaration: function (node) {
    console.log(node.id)
  },

  ImportDeclaration: function (node, stop) {
    if (isRelative(node.source.value)) {
      // Exit walking quickly with stop
      stop()
    }
  }
})

// Visit all nodes
walk(node, function (node) {
  console.log(node.type, node.loc)
})

walk.step(node, queue)

An alternative to the visitor pattern is using a loop, which can provide a much faster way to walk trees, but at the price of extra maintence. The step function simply scans node for possible child nodes, and pushes them onto queue. This can be used with a loop to walk the tree:

// Start loop with a source node:
for (var queue = [mainNode]; queue.length;) {
  var node = queue.pop()
  // handle `node` with a switch statement or whatever
  // then continue walking using step function:
  walk.step(node, queue)
}

This method is much faster than a visitor pattern at the cost of less understandable code (as it is used internally to create the visitors). The visitor pattern is likely fast enough for most cases where it is preferred over this.

License

MIT © Jamen Marz


version travis downloads license follow