Skip to content

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

Notifications You must be signed in to change notification settings

wbinnssmith/node-source-walk

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-source-walk npm npm

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

A variation of substack/node-detective and simplification of substack/node-falafel.

npm install node-source-walk

Usage

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

  var 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, function (node) {
    if (/* some condition */) {
      // No need to keep traversing since we found what we wanted
      walker.stopWalking();
    }
  });

By default, Walker will use acorn supporting ES5, but you can switch to es6 as follows:

var walker = new Walker({
  ecmaVersion: 6
});

Public Members

walk(src, cb)

  • src: the contents of a file OR its AST (via Esprima or Acorn)
  • cb: a function that is called for every visited node

stopWalking()

  • Halts further walking of the AST until another manual call of walk.
  • This is super-beneficial when dealing with large source files

traverse(node, cb)

  • 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.

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%