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

node-source-walk

v7.0.0

Published

Execute a callback on every node of a source code's AST and stop walking when you see fit

Downloads

6,653,298

Readme

node-source-walk

CI npm version npm downloads

Synchronously execute a callback on every node of a file's AST and stop walking whenever you see fit.

npm install node-source-walk

Usage

const Walker = require('node-source-walk');

const walker = new Walker();

// Assume src is the string contents of myfile.js
// or the AST of an outside parse of myfile.js
walker.walk(src, node => {
  if (node.type === whateverImLookingFor) {
    // No need to keep traversing since we found what we wanted
    walker.stopWalking();
  }
});

By default, Walker will use @babel/parser (supporting ES6, JSX, Flow, and all other available @babel/parser plugins) and the sourceType: module, but you can change any of the defaults as follows:

const walker = new Walker({
  sourceType: 'script',
  // If you don't like experimental plugins
  plugins: [
    'jsx',
    'flow'
  ]
});
  • The supplied options are passed through to the parser, so you can configure it according to @babel/parser's documentation.

Swap out the parser

If you want to supply your own parser, you can do:

const walker = new Walker({
  parser: mySweetParser
});
  • The custom parser must have a .parse method that takes in a string and returns an object/AST.
  • All of the other options supplied to the Walker constructor will be passed along as parser options to your chosen parser.

API

walk(src, callback)

  • Recursively walks the given src from top to bottom
  • src: the contents of a file or its (already parsed) AST
  • callback: a function that is called for every visited node
    • The argument passed to callback will be the currently visited node.

moonwalk(node, callback)

  • Recursively walks up an AST starting from the given node. This is a traversal that's in the opposite direction of walk and traverse
  • node: a valid AST node
  • callback: a function that is called for every node (specifically via visiting the parent(s) of every node recursively)
    • The argument passed to callback will be the currently visited node.

stopWalking()

  • Halts further walking of the AST until another manual call of walk or moonwalk
  • This is super-beneficial when dealing with large source files (or ASTs)

traverse(node, callback)

  • Allows you to traverse an AST node and execute a callback on it
  • Callback should expect the first argument to be an AST node, similar to walk's callback

parse(src)

  • Uses the options supplied to Walker to parse the given source code string and return its AST using the configured parser (or @babel/parser by default).

License

MIT