ast-grep
ast-grep (sg)
ast-grep is a fast and polyglot tool for code searching, linting, and rewriting based on Abstract Syntax Trees (AST). It excels at structural search and replace where regex fails.
When to use
- Structural Search: Finding code based on structure (e.g., "all function calls to
foowith 2 arguments") regardless of whitespace. - Refactoring: Renaming variables, changing function signatures, or transforming code patterns safely.
- Linting: Creating custom rules to enforce code style or best practices.
- Code Analysis: Extracting information from codebases.
Quick Start
CLI Basics
# Search (pattern must be in single quotes)
ast-grep -p '$A + $B' --lang ts
# Rewrite (dry run)
ast-grep -p '$A != null' --rewrite '$A' --lang ts
# Interactive Rewrite
ast-grep -p 'var $A = $B' --rewrite 'const $A = $B' --interactive
Pattern Syntax
- Meta-variables:
$VARmatches any single node. - Multi-meta-variables:
$$$ARGSmatches zero or more nodes (list of items). - Wildcard:
$_matches any node (non-capturing). - Anonymous:
$$matches any list of nodes (non-capturing).
See Pattern Syntax for details.
Core Concepts
Understanding Named vs Unnamed nodes and Matching Strictness is crucial for precise patterns.
- Named Nodes:
identifier,function_definition(matched by$VAR). - Unnamed Nodes:
(,),;(skipped by default insmartmode). - Strictness: Control matching precision (
smart,cst,ast,relaxed,signature).
See Core Concepts for details.
Rule Configuration (YAML)
For complex tasks, use YAML configuration files.
id: no-console-log
language: TypeScript
rule:
pattern: console.log($$$ARGS)
inside:
kind: function_declaration
stopBy: end
fix: '' # Remove the log
See Rule Configuration for details.
Advanced Rewriting
ast-grep supports complex transformations (regex replace, case conversion) and rewriters for sub-node transformation.
See Rewriting & Transformations for details.
Project Setup & Testing
For larger projects, organize rules and tests using sgconfig.yml.
- Scaffold:
ast-grep new project - Config:
sgconfig.ymldefines rule and test directories. - Testing: Define
validandinvalidcases to ensure rule accuracy.
See Project Setup & Testing for details.
Utility Rules
Reuse logic with local or global utility rules. Enables recursive matching.
utils:
is-literal:
any: [{kind: string}, {kind: number}]
rule:
matches: is-literal
See Utility Rules for details.
Configuration Reference
Full reference for YAML fields (id, severity, files, ignores) and supported languages.
See Configuration Reference for details.
CLI Reference
Common commands: scan, run, new, test, lsp.
See CLI Reference for details.
More from outlinedriven/odin-claude-plugin
proceed
Execute an implementation plan with surgical precision. Use after a planning phase (plan-now or similar) has produced a step-by-step strategy and identified critical files. Focuses on precise code changes with verification at each step.
1askme
Verbalized Sampling (VS) protocol for deep intent exploration before planning. Use when starting ambiguous or complex tasks, when multiple interpretations exist, or when you need to explore diverse intent hypotheses and ask maximum clarifying questions before committing to an approach.
1inits
Analyze a codebase and create or improve an AGENTS.md file for future agent instances. Use when onboarding to a repository and capturing hard-to-rediscover conventions, constraints, and rationale.
1plans
Software architect and planning specialist - conduct thorough read-only planning before any action. Use when exploring a codebase to design implementation plans, defining objectives, gathering relevant files, and summarizing available tools before coding begins.
1reviews
Review the code changes on the current branch. Use when the user asks to review their current work, analyze recent commits, or get a code quality assessment of the active branch against the main branch.
1resolve
Resolve code review comments by verifying their validity and proposing multiple solutions for confirmed issues. Use when addressing review feedback, analyzing whether review comments are valid, and generating architectural solutions (not naive fixes) for confirmed issues.
1