Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
parse
✓ import { parse } from 'ast-parser';
✗ const { parse } = require('ast-parser');
CommonJS 'require' style is shown in older examples but ESM 'import' is the recommended approach for modern JS/TS.
find
✓ import { find } from 'ast-parser';
✗ const { find } = require('ast-parser');
Use named import for the `find` utility function.
findInfo
✓ import { findInfo } from 'ast-parser';
✗ const { findInfo } = require('ast-parser');
Use named import for the `findInfo` utility function.
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.
import { parse as babelParse } from '@babel/parser';
import { parse, find, findInfo } from 'ast-parser';
// Helper to get a Babel AST node from code
function getBabelAst(code: string) {
return babelParse(code, {
sourceType: 'module',
plugins: ['classProperties', 'typescript', 'decorators-legacy'],
});
}
// Example 1: Basic AST parsing and node type assertion
const astNode = parse(getBabelAst('const x = 10;').program);
console.log(`Parsed AST node type: ${astNode.nodeType}`);
// Expected: Parsed AST node type: Program
// Example 2: Finding specific nodes
const codeWithObject = `
const myVar = { key1: 'value1', key2: 123 };
`;
const objectExpressionInfo = find('ObjectExpression', getBabelAst(codeWithObject));
console.log(`Found ObjectExpression string: ${objectExpressionInfo.string}`);
// Expected: Found ObjectExpression string: { key1: 'value1', key2: 123 }
// Example 3: Finding child information within a node
const objectProperties = findInfo('ObjectProperty', objectExpressionInfo);
objectProperties.forEach(prop => {
console.log(` Property: ${prop.key.string} = ${prop.value.string}`);
});
/* Expected:
Property: key1 = 'value1'
Property: key2 = 123
*/
// Example 4: Finding class properties and methods
const classCode = `
class MyClass {
static id: string = 'abc';
private name: string;
constructor(name: string) { this.name = name; }
public greet(): string { return 'Hello, ' + this.name; }
}
`;
const classDeclaration = find('ClassDeclaration', getBabelAst(classCode));
console.log(`Class declaration string: ${classDeclaration.string}`);
// Expected: Class declaration string: class MyClass
const classProperties = findInfo('ClassProperty', classDeclaration);
console.log(`Found ${classProperties.length} class properties.`);
const classMethods = findInfo('ClassMethod', classDeclaration);
console.log(`Found ${classMethods.length} class methods.`);
// Expected: Found 2 class properties.
// Expected: Found 2 class methods.
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'nodeType')
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.
fixVerify 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.
Error: nodeType must be a string
The `nodeType` argument for `find` or `findInfo` functions was not provided as a string.
fixPass a string literal representing the desired AST node type, e.g., `find('ObjectExpression', ...)` instead of `find(ObjectExpression, ...)`. Audit
Dependencies
@babel/parserrequiredRequired to generate the initial AST that ast-parser processes.