ast-grep

Search code with AST patterns and refactor across JavaScript, TypeScript, Python, and Go files.

Updated Feb 21, 2024
One-click install
npx skills add https://github.com/zhongjis/nix-config --skill ast-grep-zhongjis
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/zhongjis/nix-config/tree/main/modules/home-manager/features/ai-tools/common/skills/general/ast-grep
Command: npx skills add https://github.com/zhongjis/nix-config --skill ast-grep-zhongjis

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) and scripts (resource) components.

What problem does it solve?

This Skill automates complex code analysis and refactoring tasks that are difficult or impossible to achieve with regular expressions, ensuring structural integrity and consistency across codebases.

Core Features & Use Cases

  • Structural Code Search: Find code snippets based on Abstract Syntax Tree (AST) patterns, not just text.
  • Automated Refactoring: Rewrite code programmatically by matching and transforming AST nodes.
  • Cross-File Analysis: Apply patterns and refactoring rules across entire projects or specific directories.
  • Use Case: Automatically convert all console.log statements to a structured logger across a JavaScript project, or enforce consistent function signature patterns.

Quick Start

Use the ast-grep skill to find all occurrences of console.log in the current project.

Frequently Asked Questions about ast-grep

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

FAQPage Schema
How do I search and refactor code structurally instead of using regular expressions?

Structural code search uses Abstract Syntax Tree (AST) patterns to match code snippets based on syntax structure, ensuring structural integrity where regular expressions fail. It programmatically matches AST nodes to accurately transform code across multiple files.

What is the best way to replace console.log statements across a JavaScript project?

Automated refactoring with AST patterns can find all occurrences of `console.log` and programmatically rewrite them into a structured logger across a JavaScript project. This enforces consistent function signatures and pattern-based code transformation.

Does ast-grep work with Python and Go codebases for cross-file analysis?

Yes, ast-grep supports structural code search and automated refactoring for languages like JavaScript, TypeScript, Python, and Go. You can apply AST pattern matching rules and transform code across entire project directories.

How do I carry out automated code transformation using AST patterns?

Automated code transformation requires the ast-grep CLI to match specific AST nodes and programmatically rewrite code. You define structural patterns to find target snippets and apply transformation rules across files.

Do I need to install any dependencies to use ast-grep for code analysis?

Yes, you need the ast-grep CLI installed to perform pattern-based code manipulation and rewriting. This command-line tool executes the structural code analysis and automated refactoring logic across your files.

When should I not use regular expressions for code refactoring?

You should avoid regular expressions for code refactoring when structural integrity is required, as regex cannot understand syntax structure. AST pattern matching is necessary for accurate cross-file code analysis and consistent AST node transformations.