node-source-walk
Execute a callback on every node of a source code's AST and stop walking when you see fit
Last updated 3 years ago by xhmikosr .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install node-source-walk 
SYNC missed versions from official npm registry.

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

Current Tags

  • 7.0.0                                ...           latest (7 months ago)

32 Versions

  • 7.0.0                                ...           7 months ago
  • 6.0.2                                ...           2 years ago
  • 6.0.1                                ...           2 years ago
  • 6.0.0                                ...           2 years ago
  • 5.0.2                                ...           2 years ago
  • 5.0.1                                ...           2 years ago
  • 5.0.0                                ...           3 years ago
  • 4.3.0                                ...           3 years ago
  • 4.2.2                                ...           3 years ago
  • 4.2.1                                ...           3 years ago
  • 4.2.0                                ...           6 years ago
  • 4.1.0                                ...           6 years ago
  • 4.0.0                                ...           6 years ago
  • 3.3.0                                ...           7 years ago
  • 3.2.1                                ...           8 years ago
  • 3.2.0                                ...           8 years ago
  • 3.1.0                                ...           8 years ago
  • 3.0.3                                ...           8 years ago
  • 3.0.2                                ...           8 years ago
  • 3.0.1                                ...           8 years ago
  • 3.0.0                                ...           8 years ago
  • 2.1.0                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 1.4.2                                ...           10 years ago
  • 1.4.1                                ...           10 years ago
  • 1.4.0                                ...           10 years ago
  • 1.3.0                                ...           10 years ago
  • 1.2.0                                ...           10 years ago
  • 1.1.2                                ...           10 years ago
  • 1.1.1                                ...           11 years ago
  • 1.1.0                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
Maintainers (2)
Downloads
Total 3
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE