{"id":14436,"library":"ast-parser","title":"AST Parser","description":"ast-parser is a JavaScript and TypeScript utility library designed to simplify the traversal and extraction of information from Abstract Syntax Trees (ASTs), particularly those generated by `@babel/parser`. At version 0.2.0, it is in an early development stage, focused on providing helper functions like `parse`, `find`, and `findInfo` to navigate complex AST structures efficiently. While its release cadence is not formally established due to its nascent state, it appears to be actively developed, shipping with TypeScript types for enhanced developer experience. Its primary differentiation lies in offering a more declarative way to query and inspect AST nodes, complementing the output of parsers like Babel, rather than replacing them. This allows developers to easily locate specific node types or properties within a given code's AST representation.","status":"active","version":"0.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/daybrush/ast-parser","tags":["javascript","ast","babel","tsdoc","jsdoc","typescript"],"install":[{"cmd":"npm install ast-parser","lang":"bash","label":"npm"},{"cmd":"yarn add ast-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add ast-parser","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required to generate the initial AST that ast-parser processes.","package":"@babel/parser","optional":false}],"imports":[{"note":"CommonJS 'require' style is shown in older examples but ESM 'import' is the recommended approach for modern JS/TS.","wrong":"const { parse } = require('ast-parser');","symbol":"parse","correct":"import { parse } from 'ast-parser';"},{"note":"Use named import for the `find` utility function.","wrong":"const { find } = require('ast-parser');","symbol":"find","correct":"import { find } from 'ast-parser';"},{"note":"Use named import for the `findInfo` utility function.","wrong":"const { findInfo } = require('ast-parser');","symbol":"findInfo","correct":"import { findInfo } from 'ast-parser';"}],"quickstart":{"code":"import { parse as babelParse } from '@babel/parser';\nimport { parse, find, findInfo } from 'ast-parser';\n\n// Helper to get a Babel AST node from code\nfunction getBabelAst(code: string) {\n  return babelParse(code, {\n    sourceType: 'module',\n    plugins: ['classProperties', 'typescript', 'decorators-legacy'],\n  });\n}\n\n// Example 1: Basic AST parsing and node type assertion\nconst astNode = parse(getBabelAst('const x = 10;').program);\nconsole.log(`Parsed AST node type: ${astNode.nodeType}`);\n// Expected: Parsed AST node type: Program\n\n// Example 2: Finding specific nodes\nconst codeWithObject = `\n  const myVar = { key1: 'value1', key2: 123 };\n`;\nconst objectExpressionInfo = find('ObjectExpression', getBabelAst(codeWithObject));\nconsole.log(`Found ObjectExpression string: ${objectExpressionInfo.string}`);\n// Expected: Found ObjectExpression string: { key1: 'value1', key2: 123 }\n\n// Example 3: Finding child information within a node\nconst objectProperties = findInfo('ObjectProperty', objectExpressionInfo);\nobjectProperties.forEach(prop => {\n  console.log(`  Property: ${prop.key.string} = ${prop.value.string}`);\n});\n/* Expected:\n  Property: key1 = 'value1'\n  Property: key2 = 123\n*/\n\n// Example 4: Finding class properties and methods\nconst classCode = `\n  class MyClass {\n    static id: string = 'abc';\n    private name: string;\n    constructor(name: string) { this.name = name; }\n    public greet(): string { return 'Hello, ' + this.name; }\n  }\n`;\nconst classDeclaration = find('ClassDeclaration', getBabelAst(classCode));\nconsole.log(`Class declaration string: ${classDeclaration.string}`);\n// Expected: Class declaration string: class MyClass\n\nconst classProperties = findInfo('ClassProperty', classDeclaration);\nconsole.log(`Found ${classProperties.length} class properties.`);\nconst classMethods = findInfo('ClassMethod', classDeclaration);\nconsole.log(`Found ${classMethods.length} class methods.`);\n\n// Expected: Found 2 class properties.\n// Expected: Found 2 class methods.","lang":"typescript","description":"Demonstrates basic parsing, finding specific AST nodes (like ObjectExpressions and ClassDeclarations), and extracting detailed information (like ObjectProperties, ClassProperties, and ClassMethods) using `ast-parser` with a Babel AST input."},"warnings":[{"fix":"Refer to the GitHub repository for the most current API surface and usage examples with each new release. Consider pinning to exact patch versions for stability in production until a 1.0.0 release.","message":"As of version 0.2.0, `ast-parser` is in early development. APIs are subject to change without major version bumps. Always review the latest documentation upon updating.","severity":"breaking","affected_versions":">=0.2.0"},{"fix":"Ensure `@babel/parser` is installed (`npm i @babel/parser`) and its `parse` function is used to create the AST object that `ast-parser` functions expect as input.","message":"This library is designed to process ASTs generated by `@babel/parser` and does not parse code directly. You must use `@babel/parser` (or a compatible alternative) to generate the initial AST.","severity":"gotcha","affected_versions":">=0.2.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Verify that the input to `ast-parser` functions is a valid, non-null Babel AST node. Often this occurs if the initial Babel parsing failed or if a `find` operation returned no results and its output was subsequently used.","cause":"The AST node passed to an `ast-parser` function (like `parse`, `find`, or `findInfo`) is `null` or `undefined`, or it lacks the expected `nodeType` property.","error":"TypeError: Cannot read properties of undefined (reading 'nodeType')"},{"fix":"Pass a string literal representing the desired AST node type, e.g., `find('ObjectExpression', ...)` instead of `find(ObjectExpression, ...)`.","cause":"The `nodeType` argument for `find` or `findInfo` functions was not provided as a string.","error":"Error: nodeType must be a string"}],"ecosystem":"npm"}