AST-Grep Code Search

Find and refactor code patterns using AST structure across multi-language codebases.

Updated Sep 11, 2021
One-click install
npx skills add https://github.com/biwsantang/dotfiles --skill ast-grep-code-search
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: AST-Grep Code Search
Source: https://github.com/biwsantang/dotfiles/tree/main/claude/.claude/skills/ast-grep
Command: npx skills add https://github.com/biwsantang/dotfiles --skill ast-grep-code-search

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill overcomes the limitations of traditional regex for code search and refactoring. It understands code structure, allowing you to find and transform patterns across multiple lines and languages, ignoring formatting differences, which saves immense time on large-scale refactorings.

Core Features & Use Cases

  • Structural Pattern Matching: Finds code patterns based on Abstract Syntax Tree (AST) structure, not just text, for precise results.
  • Smart Refactoring: Replaces code patterns while preserving formatting and semantic integrity across your codebase.
  • Use Case: Automatically migrate an old API call like user.getName() to user.profile.name across an entire codebase, ensuring all instances are updated correctly and consistently without manual review.

Quick Start

Example: Find all console.log statements in JavaScript files

ast-grep --pattern 'console.log($$$)' --lang js

Frequently Asked Questions about AST-Grep Code Search

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I search for code patterns across my codebase without using regex?

Structural code search uses Abstract Syntax Tree (AST) matching to find patterns based on code structure rather than text. This method understands language syntax, ignores formatting differences, and finds multi-line patterns regex cannot, making it precise for large-scale searches across JavaScript, TypeScript, Python, Rust, Go, and other languages.

Can I refactor code patterns automatically while preserving formatting?

AST-based refactoring replaces code patterns while maintaining your original formatting and semantic integrity. You define transformations using YAML rule configurations with metavariable capture, then execute dry-run or interactive workflows to safely update all matching instances across your entire codebase.

How do I migrate an old API call to a new one across multiple files?

Define an AST pattern matching the old API syntax, specify the replacement pattern, and run the refactoring tool against your codebase. It finds all instances regardless of formatting or location, validates the transformation preserves meaning, and applies changes consistently across files in a single operation.

Does structural code search work with multiple programming languages?

Yes, AST-based search supports JavaScript, TypeScript, Python, Rust, Go, and other languages. You specify the target language when running a search, and the tool applies language-aware pattern matching to find equivalent code structures across multi-language codebases.

What's the advantage of AST pattern matching over regex for code refactoring?

AST matching understands code semantics and structure instead of treating code as plain text, so it captures patterns that span multiple lines, ignore whitespace variations, and account for language syntax. Regex cannot distinguish between similar-looking code with different meanings; AST matching can, reducing false positives and unsafe transformations.

Can I test a refactoring before applying it to my codebase?

Yes, dry-run workflows let you preview all matched patterns and proposed changes before execution. Interactive mode allows you to review and approve each transformation individually, ensuring accuracy before committing changes to your codebase.