ast-grep
ast-grep (ast-grep or sg) searches code by AST structure, not text. Patterns must be valid parseable code with $VAR meta-variables for wildcards.
Core Commands
# Search with pattern
ast-grep run -l ts --pattern 'console.log($X)' src/
# Search in directory (auto-detects language)
ast-grep run --pattern '$FUNC()' agent/extensions/
Pattern Syntax
$VAR— matches single AST node (uppercase only:$MOD,$_,$_123)$$$VAR— matches zero or more nodes (function args, statements)$_VAR— non-capturing (same name can match different content)- Patterns must be valid code:
import { $X } from "$Y"works,from $Xfails
Common Patterns
Find re-exports (barrel files)
ast-grep run -l ts --pattern 'export * from "$Y"' ./
ast-grep run -l ts --pattern 'export { $$$X } from "$Y"' ./
ast-grep run -l ts --pattern 'export type * from "$Y"' ./
Find all imports from a module
ast-grep run -l ts --pattern 'import { $$$X } from "$MOD"' ./
Find function calls with specific structure
ast-grep run -l ts --pattern 'console.log($MSG)' ./
ast-grep run -l ts --pattern 'fetch($URL).then($H)' ./
Detect anti-patterns
# await inside Promise.all
ast-grep run -l ts --pattern 'await $X' ./ \
--selector call_expression \
--pattern 'Promise.all($$_)'
Tips
- Use
--debug-query=astto see parsed AST structure - Check playground for pattern validation
- Patterns fail silently if not parseable — verify with
--debug-query
More from knoopx/pi
podman
Manages containers, builds images, configures pods and networks with Podman. Use when running containers, creating Containerfiles, grouping services in pods, or managing container resources.
122jujutsu
Manages version control with Jujutsu (jj), including rebasing, conflict resolution, and Git interop. Use when tracking changes, navigating history, squashing/splitting commits, or pushing to Git remotes.
117nix-flakes
Creates reproducible builds, manages flake inputs, defines devShells, and builds packages with flake.nix. Use when initializing Nix projects, locking dependencies, or running nix build/develop commands.
54scraping
Fetches web pages, parses HTML with CSS selectors, calls REST APIs, and scrapes dynamic content. Use when extracting data from websites, querying JSON APIs, or automating browser interactions.
48jscpd
Finds duplicate code blocks and analyzes duplication metrics across files. Use when identifying copy-pasted code, measuring technical debt, or preparing for refactoring.
45yt-dlp
Downloads videos from YouTube and other sites using yt-dlp. Use when downloading videos, extracting metadata, or batch downloading multiple files.
42