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


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

  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 

By default, Walker will use acorn supporting ES6 and the sourceType: module, but you can change any of the defaults as follows:

var walker = new Walker({
  ecmaVersion: 5,
  sourceType: 'script'
  • The supplied options are passed through to the parser, so you can configure it according to acorn's documentation:

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


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