{"name":"node-source-walk","version":"8.0.0","description":"Execute a callback on every node of a source code's AST and stop walking when you see fit","type":"module","exports":{".":"./index.js"},"scripts":{"lint":"xo","fix":"xo --fix","uvu":"uvu test -i fixtures","test":"npm run lint && npm run uvu","test:ci":"c8 npm run uvu"},"repository":{"type":"git","url":"git+https://github.com/dependents/node-source-walk.git"},"keywords":["ast","traversal","acorn","static analysis","source code","walker","jsx"],"author":{"name":"Joel Kemp","email":"joel@mrjoelkemp.com"},"license":"MIT","bugs":{"url":"https://github.com/dependents/node-source-walk/issues"},"homepage":"https://github.com/dependents/node-source-walk","engines":{"node":">=20.19.0 || >=22.12.0"},"dependencies":{"@babel/parser":"^7.29.3"},"devDependencies":{"c8":"^11.0.0","sinon":"^22.0.0","uvu":"^0.5.6","xo":"^2.0.2"},"gitHead":"e57692f2bb271fa80d3b31299a9a568504172edd","_id":"node-source-walk@8.0.0","_nodeVersion":"24.15.0","_npmVersion":"11.12.1","dist":{"integrity":"sha512-/CsHIdZsVgpGm4A7cD+Vdajv2GyqkVPUNtpChugutV/O9e3j1YWkdOWwhWKo0NfRM4r4XoiKtkb4JoLzsCN5vw==","shasum":"c96bc53c97dcb810c2a1054ebe16315edab90558","tarball":"http://registry-npm.ywwl.com/node-source-walk/download/node-source-walk-8.0.0.tgz","fileCount":4,"unpackedSize":9751,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIBQJHOrWH6LQn81g89L4Ur4o6E8QFaEGpKBLUTxyr59TAiBHFnDeEwHoNpb54CQyriSCLtK76DPZQ0cB4eKAE0cTQg=="}],"size":3899,"noattachment":false,"key":"/node-source-walk/-/node-source-walk-8.0.0.tgz"},"_npmUser":{"name":"xhmikosr","email":"xhmikosr@gmail.com"},"directories":{},"maintainers":[{"name":"pahen","email":"patrik.henningsson@gmail.com"},{"name":"mrjoelkemp","email":"mrjoelkemp@gmail.com"},{"name":"xhmikosr","email":"xhmikosr@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/node-source-walk_8.0.0_1779192451554_0.014365735778647082"},"_hasShrinkwrap":false,"readme":"# node-source-walk\n\n[![CI](https://img.shields.io/github/actions/workflow/status/dependents/node-source-walk/ci.yml?branch=main&label=CI&logo=github)](https://github.com/dependents/node-source-walk/actions/workflows/ci.yml?query=branch%3Amain)\n[![npm version](https://img.shields.io/npm/v/node-source-walk?logo=npm&logoColor=fff)](https://www.npmjs.com/package/node-source-walk)\n[![npm downloads](https://img.shields.io/npm/dm/node-source-walk)](https://www.npmjs.com/package/node-source-walk)\n\n> Synchronously execute a callback on every node of a file's AST and stop walking whenever you see fit.\n\n```sh\nnpm install node-source-walk\n```\n\n## Usage\n\n## ESM\n\n```js\nimport Walker from 'node-source-walk';\n\nconst walker = new Walker();\n\n// Assume src is the string contents of myfile.js\n// or the AST of an outside parse of myfile.js\nwalker.walk(src, node => {\n  if (node.type === whateverImLookingFor) {\n    // No need to keep traversing since we found what we wanted\n    walker.stopWalking();\n  }\n});\n```\n\n### CommonJS\n\n```js\nconst { default: Walker } = require('node-source-walk');\n\nconst walker = new Walker();\n// ...\n```\n\nBy 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:\n\n```js\nconst walker = new Walker({\n  sourceType: 'script',\n  // If you don't like experimental plugins\n  plugins: [\n    'jsx',\n    'flow'\n  ]\n});\n```\n\n* The supplied options are passed through to the parser, so you can configure it according to `@babel/parser`'s [documentation](https://babeljs.io/docs/en/babel-parser.html).\n\n## Swap out the parser\n\nIf you want to supply your own parser, you can do:\n\n```js\nconst walker = new Walker({\n  parser: mySweetParser\n});\n```\n\n* The custom parser must have a `.parse` method that takes in a string and returns an object/AST.\n* All of the other options supplied to the Walker constructor will be passed along as parser options to your chosen parser.\n\n## API\n\n### `walk(src, callback)`\n\n* Recursively walks the given `src` from top to bottom\n* `src`: the contents of a file **or** its (already parsed) AST\n* `callback`: a function that is called for every visited node\n  * The argument passed to `callback` will be the currently visited node.\n\n### `moonwalk(node, callback)`\n\n* Recursively walks up an AST starting from the given node. This is a traversal that's in the opposite direction of `walk` and `traverse`\n* `node`: a valid AST node\n* `callback`: a function that is called for every node (specifically via visiting the parent(s) of every node recursively)\n  * The argument passed to `callback` will be the currently visited node.\n\n### `stopWalking()`\n\n* Halts further walking of the AST until another manual call of `walk` or `moonwalk`\n* This is super-beneficial when dealing with large source files (or ASTs)\n\n### `traverse(node, callback)`\n\n* Allows you to traverse an AST node and execute a callback on it\n* Callback should expect the first argument to be an AST node, similar to `walk`'s callback\n\n### `parse(src)`\n\n* 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).\n\n## License\n\n[MIT](LICENSE)\n","publish_time":1779192451707,"_cnpm_publish_time":1779192451707,"dist-tags":{"latest":"8.0.0"}}