node-source-walk

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

MIT 35 个版本
安装
npm install node-source-walk
yarn add node-source-walk
pnpm add node-source-walk
bun add node-source-walk
README

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

ESM

import Walker from '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();
  }
});

CommonJS

const { default: Walker } = require('node-source-walk');

const walker = new Walker();
// ...

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

版本列表
8.0.0 2026-05-19
7.0.2 2026-04-25
7.0.1 2025-01-30
7.0.0 2024-04-08
6.0.2 2023-05-15
6.0.1 2023-05-08
6.0.0 2023-05-04
5.0.2 2023-04-26
5.0.1 2023-03-19
5.0.0 2022-03-02
4.3.0 2022-02-22
4.2.2 2022-02-19
4.2.1 2022-02-19
4.2.0 2018-12-14
4.1.0 2018-10-27
4.0.0 2018-08-29
3.3.0 2017-08-27
3.2.1 2017-05-06
3.2.0 2016-09-05
3.1.0 2016-09-03
3.0.3 2016-08-31
3.0.2 2016-08-25
3.0.1 2016-08-24
3.0.0 2016-06-12
2.1.0 2015-10-24
2.0.0 2015-06-27
1.4.2 2015-04-26
1.4.1 2014-12-22
1.4.0 2014-12-14
1.3.0 2014-12-13
1.2.0 2014-10-28
1.1.2 2014-09-11
1.1.1 2014-03-26
1.1.0 2014-03-19
1.0.0 2014-03-18