{"library":"node-source-walk","title":"Node Source Walk AST Traversal","description":"node-source-walk is a JavaScript utility library designed for synchronously traversing Abstract Syntax Trees (ASTs) or raw source code strings. It allows developers to apply a callback function to every node within the AST, with the crucial capability to halt the traversal prematurely using `stopWalking()`. The current stable version is 7.0.1. Releases tend to align with Node.js LTS updates, primarily dropping support for older Node.js versions. A key differentiator is its flexibility in supporting both top-down (`walk`, `traverse`) and bottom-up (`moonwalk`) traversals, accepting either source code or a pre-parsed AST, and offering the option to replace its default `@babel/parser` with a custom parser.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-source-walk"],"cli":null},"imports":["import Walker from 'node-source-walk';","const Walker = require('node-source-walk');","import type { WalkerOptions } from 'node-source-walk';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import Walker from 'node-source-walk';\n\n// Example source code string\nconst src = `\n  const add = (a, b) => a + b;\n  function subtract(a, b) {\n    return a - b;\n  }\n  console.log(add(5, 3));\n  console.log(subtract(10, 4));\n`;\n\nconst walker = new Walker();\nlet foundFunctionDeclaration = false;\n\nconsole.log('Walking AST for function declarations...');\nwalker.walk(src, node => {\n  if (node.type === 'FunctionDeclaration') {\n    console.log(`Found function declaration: ${node.id.name}`);\n    foundFunctionDeclaration = true;\n    // Stop walking after finding the first one\n    walker.stopWalking();\n  }\n});\n\nif (!foundFunctionDeclaration) {\n  console.log('No function declarations found.');\n}\n\n// Example with custom parser options (for JSX)\nconst jsxSrc = `\n  import React from 'react';\n  function MyComponent() {\n    return <div>Hello, JSX!</div>;\n  }\n`;\n\nconst jsxWalker = new Walker({\n  plugins: ['jsx'] // Ensure JSX plugin is enabled for parsing\n});\n\nconsole.log('\\nWalking AST for JSX elements...');\nlet foundJsxElement = false;\njsxWalker.walk(jsxSrc, node => {\n  if (node.type === 'JSXElement') {\n    console.log('Found a JSXElement!');\n    foundJsxElement = true;\n    jsxWalker.stopWalking();\n  }\n});\n\nif (!foundJsxElement) {\n  console.log('No JSX elements found.');\n}\n","lang":"typescript","description":"This quickstart demonstrates how to instantiate the Walker class, traverse source code, identify specific AST node types (like 'FunctionDeclaration' or 'JSXElement'), and efficiently stop the traversal once a desired node is found. It also shows how to configure the internal parser with custom plugins like 'jsx'.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}