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

regjstraverse

v0.1.2

Published

Traverse the RegJS AST (a AST for JavaScript's regular expressions).

Downloads

3

Readme

RegJSTraverse

Traverse the RegJS AST (a AST for JavaScript's regular expressions).

Installation

npm install regjstraverse

Testing

To run the tests, run the following command:

npm test

Background

A RegJS AST can be generated by using the regjsparser library:

// Create the AST for the regular expression `/abc/`.
var ast = require('regjsparser').parse('abc');

Example Usage

regjstraverse makes it easy to traverse the regular expression AST using the enter and leave functions:

var regjstraverse = require('regjstraverse');

regjstraverse.traverse(ast, {
  enter: function(node) {
    // Called when entering a node.
    console.log('enter', node.type);
  },

  leave: function(node) {
    // Called when leaving a node.
    console.log('leave', node.type);
  }
})

When traversing the nodes, it's possible to skip the sub-nodes of the current node by calling the this.skip() method or returning regjstraverse.VisitorOption.Skip:

var regjstraverse = require('regjstraverse');

regjstraverse.traverse(ast, {
  enter: function(node) {
    console.log('enter', node.type);
    if (node.type === 'characterClass') {
      // The following two lines have the same effect.
      this.skip();
      return regjstraverse.VisitorOption.Skip;
    }
  },

  leave: function(node) {
    // NOTE: Invoking `skip` in the leave function has no effect.

    // Called on leave the node.
    console.log('leave', node.type);
  }
})

Breaking at the current point in the tree traversal is possible by invoking break:

var regjstraverse = require('regjstraverse');

regjstraverse.traverse(ast, {
  enter: function(node) {
    console.log('enter', node.type);
    if (node.type === 'characterClass') {
      // The following two lines have the same effect.
      this.break();
      return regjstraverse.VisitorOption.Break;
    }
  }
})

Note: After invoking break stops the entire tree traversal - no further calls to enter or leave are made afterwards.


Replacing the current visited node is doable using the replace function:

var regjstraverse = require('regjstraverse');
var parse = require('regjsparser').parse;

var newAst = regjstraverse.replace(ast, {
  enter: function(node) {
    if (node.type === 'value') {
      // Replace the `value` node with a new value node /a/.
      // The following two lines have the same effect.
      this.replace(parse('a'));
      return parse('a');
    }
  }
})

Note: if the enter function returns a new AST, the subnodes of the new-replaced AST are visited. Example:

var regjstraverse = require('regjstraverse');
var regjsparser = require('regjsparser');

var rawValues = '';
var ast = regjstraverse.replace(regjsparser.parse('a|b'), {
  enter: function(node, parent) {
    if (node.type === 'disjunction') {
      return regjsparser.parse('c|d');
    } else {
      // This visits the new replaced nodes `c` and `d` from above and not the
      // original `a` and `b` ones.
      rawValues += node.raw;
    }
  }
});
// Tests if `enter` was called on the replaced node.
assert.equal(rawValues, 'cd');