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

@wojtekmaj/babylon-walk

v2.0.0

Published

Lightweight Babylon AST traversal

Downloads

168

Readme

downloads build dependencies dev dependencies

babylon-walk

Lightweight AST traversal tools for @babel/parser ASTs.

@babel/parser is the parser used by the Babel project, which supplies the wonderful babel-traverse module for walking Babylon ASTs. Problem is, babel-traverse is very heavyweight, as it is designed to supply utilities to make all sorts of AST transformations possible. For simple AST walking without transformation, babel-traverse brings a lot of overhead.

This module loosely implements the API of Acorn parser's walk module, which is a lightweight AST walker for the ESTree AST format.

In my tests, babylon-walk's ancestor walker (the most complex walker provided by this module) is about 8 times faster than babel-traverse, if the visitors are cached and the same AST is used for all runs. It is about 16 times faster if a fresh AST is used every run.

Getting started

Compatibility

Your project needs to use Node 6 or later.

Installation

Add babylon-walk to your project by executing npm install @wojtekmaj/babylon-walk or yarn add @wojtekmaj/babylon-walk.

Usage

Here's an example of basic usage:

const walk = require('@wojtekmaj/babylon-walk');

User guide

walk.simple(node, visitors, state)

Do a simple walk over the AST.

When walk.simple is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding Visitor(node, state) {} to Visitor() { enter(node, state) {} }). This exploding process can take some time, so it is recommended to cache your visitors and communicate state leveraging the state parameter. (One difference between the linked article and babylon-walk is that the state is only accessible through the state variable, never as this.)

All @babel/types aliases (e.g. Expression) and the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}) work.

Arguments

|Argument name|Description| |----|----| |node|The AST node to walk.| |visitors|An object containing Babel visitors. Each visitor function will be called as (node, state), where node is the AST node, and state is the same state passed to walk.simple.| |state|State.|

walk.ancestor(node, visitors, state)

Do a simple walk over the AST, but memoizing the ancestors of the node and making them available to the visitors.

When walk.ancestor is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding Visitor(node, state) {} to Visitor() { enter(node, state) {} }). This exploding process can take some time, so it is recommended to cache your visitors and communicate state leveraging the state parameter. (One difference between the linked article and babylon-walk is that the state is only accessible through the state variable, never as this.)

All @babel/types aliases (e.g. Expression) and the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}) work.

Arguments

|Argument name|Description| |----|----| |node|The AST node to walk.| |visitors|An object containing Babel visitors. Each visitor function will be called as (node, state, ancestors), where node is the AST node, state is the same state passed to walk.ancestor, and ancestors is an array of ancestors to the node (with the outermost node being [0] and the current node being [ancestors.length - 1]). If state is not specified in the call to walk.ancestor, the state parameter will be set to ancestors.| |state|State.|

walk.recursive(node, visitors, state)

Do a recursive walk over the AST, where the visitors are responsible for continuing the walk on the child nodes of their target node.

When walk.recursive is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding Visitor(node, state) {} to Visitor() { enter(node, state) {} }). This exploding process can take some time, so it is recommended to cache your visitors and communicate state leveraging the state parameter. (One difference between the linked article and babylon-walk is that the state is only accessible through the state variable, never as this.)

Unlike other babylon-walk walkers, walk.recursive does not call the exit visitor, only the enter (the default) visitor, of a specific node type.

All @babel/types aliases (e.g. Expression) and the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}) work.

Arguments

|Argument name|Description| |----|----| |node|The AST node to walk.| |visitors|An object containing Babel visitors. Each visitor function will be called as (node, state, c), where node is the AST node, state is the same state passed to walk.recursive, and c is a function that takes a single node as argument and continues walking that node. If no visitor for a node is provided, the default walker algorithm will still be used.| |state|State.|

Example

In the following example, we are trying to count the number of functions in the outermost scope. This means, that we can simply walk all the statements and increment a counter if it is a function declaration or expression, and then stop walking. Note that we do not specify a visitor for the Program node, and the default algorithm for walking Program nodes is used (which is what we want). Also of note is how I bring the visitors object outside of countFunctions so that the object can be cached to improve performance.

import * as t from 'babel-types';
import { parse } from 'babylon';
import * as walk from '@wojtekmaj/babylon-walk';

const visitors = {
  Statement(node, state, c) {
    if (t.isVariableDeclaration(node)) {
      for (let declarator of node.declarations) {
        // Continue walking the declarator
        c(declarator);
      }
    } else if (t.isFunctionDeclaration(node)) {
      state.counter++;
    }
  },

  VariableDeclarator(node, state) {
    if (t.isFunction(node.init)) {
      state.counter++;
    }
  }
};

function countFunctions(node) {
  const state = {
    counter: 0
  };
  walk.recursive(node, visitors, state);
  return state.counter;
}

const ast = parse(`
  // Counts
  var a = () => {};

  // Counts
  function b() {
    // Doesn't count
    function c() {
    }
  }

  // Counts
  const c = function d() {};
`);

countFunctions(ast);
// = 3

Caveats

For those of you migrating from Acorn to Babylon, there are a few things to be aware of.

  1. The visitor caching suggestions do not apply to Acorn's walk module, but do for babylon-walk.

  2. babylon-walk does not provide any of the other functions Acorn's walk module provides (e.g. make, findNode*).

  3. babylon-walk does not use a base variable. The walker algorithm is the same as what babel-traverse uses.

    • That means certain nodes that are not walked by Acorn, such as the property property of a non-computed MemberExpression, are walked by babylon-walk.

License

The MIT License.

Authors

Thank you

This project wouldn't be possible without awesome work of Timothy Gu [email protected] who created its initial version. Thank you!