@coderrob/typescript-ast

April 7, 2026 · View on GitHub

typescript-ast logo

@coderrob/typescript-ast

Policy-agnostic AST navigation helpers for TSESTree-compatible tooling.

@coderrob/typescript-ast is a focused utility library for inspecting, traversing, and interpreting AST nodes in ESLint rules, codemods, static analysis, and TypeScript-aware developer tooling. It helps you handle common AST tasks with small, composable helpers while keeping rule logic and policy decisions in your own code.

Highlights

  • Focused on AST ergonomics rather than framework-specific policy.
  • Split entry points for structural helpers and TypeScript-specific extensions.
  • Designed around TSESTree-shaped nodes used by the @typescript-eslint ecosystem.
  • Useful in lint rules, repository analysis, codemods, and internal code intelligence workflows.
  • Published with ESM and CommonJS bundles plus matching declaration files.

Installation

npm install @coderrob/typescript-ast

If you also need to parse source code or supply visitor keys in your own tooling, install the relevant @typescript-eslint packages used by your stack.

Requirements

  • Node.js >=18
  • AST nodes compatible with @typescript-eslint/types / TSESTree
  • A visitor key map for descendant-search helpers such as visitorKeys from @typescript-eslint/visitor-keys

Choose An Entry Point

Entry pointUse whenIncludes
@coderrob/typescript-astYou want the full public API surface.Structural helpers, TypeScript-specific helpers, guards, and import-path utilities.
@coderrob/typescript-ast/coreYou only need structural AST utilities.Calls, navigation, JSDoc helpers, descendant search, statements, import paths, and general-purpose guards.
@coderrob/typescript-ast/typescriptYou only need TypeScript ESTree extensions.Parameter annotation helpers, type-reference helpers, TS expression unwrapping, and TS-specific guards.

The root entry point is the compatibility surface. For the clearest dependency boundaries, prefer core for general AST work and add typescript only when you need TypeScript-specific node semantics.

Quick Start

This package operates on AST nodes you already have. It does not parse source code for you.

import { parse } from "@typescript-eslint/typescript-estree";
import { visitorKeys } from "@typescript-eslint/visitor-keys";
import {
  findDescendant,
  getStringLiteralCallArgument,
  isCallExpression,
  isNamedMemberCall,
} from "@coderrob/typescript-ast";

const ast = parse('console.error("boom")', { jsx: false });
const call = findDescendant(ast, visitorKeys, isCallExpression);

if (call && isNamedMemberCall(call, "console", "error")) {
  console.log(getStringLiteralCallArgument(call, 0));
}

Common Workflows

Match Call Shapes Without Manual Callee Inspection

Use the call helpers when you need to recognize direct calls, dotted member calls, or simple argument patterns.

import {
  getCalleeNamePath,
  getStringLiteralCallArgument,
  hasCallCalleeNamePath,
  isNamedCall,
  isNamedMemberCall,
} from "@coderrob/typescript-ast/core";

const calleePath = getCalleeNamePath(callExpression.callee);
const isConsoleError = isNamedMemberCall(callExpression, "console", "error");
const isReadFileSync = hasCallCalleeNamePath(callExpression, ["fs", "readFileSync"]);
const isDescribeCall = isNamedCall(callExpression, "describe");
const message = getStringLiteralCallArgument(callExpression, 0);

Traverse Descendants With Explicit Stop Conditions

The search helpers work well when you need predictable traversal and want to stop descending into boundaries such as nested functions or classes.

import { AST_NODE_TYPES } from "@typescript-eslint/types";
import { visitorKeys } from "@typescript-eslint/visitor-keys";
import { findDescendant, isCallExpression } from "@coderrob/typescript-ast/core";

const nestedCall = findDescendant(
  program,
  visitorKeys,
  isCallExpression,
  (node) => node.type === AST_NODE_TYPES.FunctionDeclaration,
);

Inspect TypeScript Parameters And Wrapped Expressions

Use the TypeScript entry point when you need parameter annotations, this parameter handling, or wrapper-expression unwrapping.

import {
  getFirstNonThisParameter,
  getParameterTypeNode,
  unwrapTsExpression,
} from "@coderrob/typescript-ast/typescript";

const param = getFirstNonThisParameter(functionNode.params);
const typeNode = param ? getParameterTypeNode(param) : null;
const runtimeExpression = unwrapTsExpression(expression);

Normalize Import-Path Checks

The import-path helpers cover a few recurring repository-analysis checks without bringing in a full path abstraction layer.

import { getFilename, isBarrelFile, isParentDirectoryImportPath } from "@coderrob/typescript-ast/core";

const filename = getFilename(importSource);
const isIndexFile = isBarrelFile(resolvedPath);
const walksUp = isParentDirectoryImportPath(importPath);

API Areas

The public API is organized by concern rather than by large utility classes.

AreaRepresentative helpers
Call analysisgetCalleeNamePath, getCallArgument, hasCallCalleeNamePath, isNamedCall, isNamedMemberCall
Structural navigationfindAncestor, findEnclosingFunction, getNodeParent, getParentBlockStatement, isInsideBoundary
Descendant searchfindDescendant, hasMatchingDescendant, hasMatchingDescendantUntil, hasSomeDescendant
JSDoc ownershipgetJsdocComment, getTargetNode, getParentOwnedTargetNode, isJsdocBlockComment
Statement helpersgetBooleanLiteralReturnValue, getReturnStatement, getSingleReturnStatement
TypeScript helpersgetParameterTypeAnnotation, getParameterTypeNode, getFirstTypeArgument, unwrapTsExpression
GuardsisCallExpression, isIdentifier, isFunctionLike, isUncomputedMemberExpression, isTSTypeReference
Import pathsgetFilename, isBarrelFile, isParentDirectoryImportPath

For the complete surface, inspect the exported declarations in dist or use editor IntelliSense from one of the supported entry points.

Design Principles

  • Keep AST utilities small, explicit, and easy to test.
  • Separate structural analysis from rule policy and reporting logic.
  • Avoid coupling the package to a single lint engine or transform framework.
  • Expose TypeScript-specific behavior through a distinct entry point instead of mixing it into every structural helper.

Repository Quality Standards

The repository is maintained with release gates intended for publishable library code:

  • ESLint and Prettier for source consistency
  • TypeScript validation for library and test projects
  • Vitest coverage enforcement
  • source-duplication checks with jscpd
  • circular-dependency checks with madge
  • package-quality linting with publint
  • repeatable micro-benchmarks with tinybench

Useful commands:

  • npm run build
  • npm run test
  • npm run test:coverage
  • npm run lint
  • npm run typecheck
  • npm run quality
  • npm run deps:circular
  • npm run bench
  • npm run ci

Benchmarks

Latest local benchmark snapshot:

  • Date: April 7, 2026
  • Command: npm run bench
  • Runtime: Node.js v22.19.0
  • OS: Microsoft Windows NT 10.0.26200.0
  • Benchmark coverage plan: every exported function or function alias is benchmarked or explicitly skipped

These numbers are machine-dependent and should be treated as a point-in-time reference rather than a portability guarantee.

Taskops/secavg (ns)Margin
getCalleeNamePath: member5,526,017216±0.71%
getStringLiteralCallArgument: first13,449,85857±0.40%
hasCallCalleeNamePath: member7,125,853206±2.56%
hasMemberCallee: member14,309,82153±0.48%
isNamedCall: simple10,416,38783±1.08%
getCallMemberMethodName: member14,316,05053±0.89%
getMemberPropertyName: member17,801,97543±0.94%
getVisitorChildNodes: nested2,224,307549±5.15%
resolveFunctionName: declaration12,171,61264±0.36%
getJsdocComment: basic14,378,23354±4.28%
isJsdocBlockComment: basic17,822,07443±0.61%
findAncestor: function15,325,78849±0.45%
getParentBlockStatement: return15,003,03951±4.15%
isInsideBoundary: ancestors7,381,892157±0.10%
getNamedParameterIdentifier: identifier14,808,10451±0.36%
getObjectDestructuredParameterTypeNode: object9,918,20894±0.79%
getParameterTypeAnnotation: identifier10,692,93477±0.24%
findDescendant: call1,149,2201055±3.88%
hasMatchingDescendant: call1,147,1351047±7.89%
hasMatchingDescendantUntil: identifier705,4771571±3.02%
getBooleanLiteralReturnValue: return12,703,01660±0.26%
getReturnStatement: return14,658,74452±0.29%
unwrapTsExpression: wrapped9,376,505111±0.47%
isNamedTypeReference: promise12,910,82359±0.79%
hasTypeArguments: promise18,079,02142±0.97%
isCallExpression: simple14,820,74251±0.50%
isIdentifier: callee14,730,62252±1.01%
isTestFile: convention9,831,030103±1.27%
isUncomputedMemberExpression: callee14,698,69051±0.28%
getFilename: path9,924,89094±0.62%
isBarrelFile: index9,692,900104±0.64%
isParentDirectoryImportPath: relative16,641,77446±0.71%

Run the suite again locally when you need numbers for a different machine, Node.js version, or code revision:

npm run bench

Documentation

License

Apache-2.0