Skip to content
Skills Library
Programming · Analysis

ast-grep

/ast-grep

Write and debug ast-grep rules for structural code search and rewrite. Use when tasks require AST-aware matching (not plain text), such as finding specific syntax shapes, relational patterns (inside/has), composite logic (all/any/not), or precise code refactors across a codebase.

Repository
mtga ↗
Licence
AGPL-3.0
Last repository change
18.06.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
3
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 18.06.2026, without references to retired models or APIs.

Licence

AGPL-3.0, copyleft: przeróbki udostępniane innym muszą mieć tę samą licencję.

What it does and when to use it

Write and debug ast-grep rules for structural code search and rewrite. Use when tasks require AST-aware matching (not plain text), such as finding specific syntax shapes, relational patterns (inside/has), composite logic (all/any/not), or precise code refactors across a codebase.

Original author description

Write and debug ast-grep rules for structural code search and rewrite. Use when tasks require AST-aware matching (not plain text), such as finding specific syntax shapes, relational patterns (inside/has), composite logic (all/any/not), or precise code refactors across a codebase.

How it works

  1. Overview
  2. General Workflow
  3. CLI Quick Reference
  4. Tips for Writing Effective Rules
  5. Resources
Original SKILL.md headings · 135 lines, 425 words
SKILL.md preview
---
name: ast-grep
description: Write and debug ast-grep rules for structural code search and rewrite. Use when tasks require AST-aware matching (not plain text), such as finding specific syntax shapes, relational patterns (inside/has), composite logic (all/any/not), or precise code refactors across a codebase.
---

# ast-grep Code Search

## Overview

Translate user intent into `sg` patterns/rules, validate quickly, then run against target scope.

## General Workflow

### Step 1: Understand the Query

Pin down:
- target syntax shape
- language
- file scope
- include/exclude constraints

### Step 2: Start with the Smallest Pattern

Use `sg run --pattern` first.

```bash
sg run --pattern 'console.log($ARG)' --lang javascript src
```

### Step 3: Escalate to YAML Rule for Structure Logic

For relational/composite logic (`inside` / `has` / `precedes` / `follows` / `all` / `any` / `not` / `matches`), use `sg scan --rule`.

```yaml
# rule.yml
id: async-with-await
language: javascript
rule:
  kind: function_declaration
  has:
    pattern: await $EXPR
    stopBy: end
```

```bash
sg scan --rule rule.yml src
```

### Step 4: Validate and Search

Test on a tiny sample first, then search the project.

```bash
echo "async function f(){ await g() }" | sg scan --rule rule.yml --stdin
sg scan --rule rule.yml src --json=compact
```

## CLI Quick Reference

### Inspect Code Structure (--debug-query)

Use when kind/pattern mismatch is unclear.

```bash
sg run --pattern 'class $NAME { $$$BODY }' \
  --lang javascript \
  --debug-query=pattern
```

### Test Rules (scan with --stdin)

```bash
echo "const x = await fetch();" | sg scan --inline-rules "id: test
language: javascript
rule:
  pattern: await \$EXPR" --stdin --json=compact
```

### Search with Patterns (run)

```bash
sg run --pattern 'function $NAME($$$)' --lang javascript src --json=compact
```

### Search with Rules (scan)

```bash
sg scan --rule rule.yml src
sg scan --inline-rules "id: find-async
language: javascript
rule:
  kind: function_declaration
  has:
    pattern: await \$EXPR
    stopBy: end" src
```

## Tips for Writing Effective Rules

### Default to stopBy: end for Deep Searches

For relational rules (`inside`, `has`), default to:

```yaml
has:
  pattern: await $EXPR
  stopBy: end
```

This ensures the search traverses the entire subtree rather than stopping at the first non-matching node.

### Start Simple, Then Add Complexity

1. `pattern`
2. `kind`
3. add `inside`/`has`
4. add `all`/`any`/`not`

### Metavariable Rules

1. Use exact metavariable forms only: `$A`, `$$OP`, `$$$ARGS`.
2. Keep metavariable text as the only content in its AST node.
3. If a rule depends on previously captured metavariables, combine sub-rules in `all` to guarantee match order.

### No-Match Triage

1. Run with `--json=compact`.
2. If output is `[]`, treat as no match and refine pattern/scope.
3. If stderr has parser/arg errors, fix language, pattern, or flags first.

## Resources

### references/

- `rule_reference.md`: Full rule syntax (atomic, relational, composite, metavariables)
- `windows.md`: PowerShell quoting/escaping tips and Vue SFC script-block workflow

Load only the reference needed for the current task.
Files in this package (3)
  • SKILL.md 3 KB
  • references/rule_reference.md 10 KB
  • references/windows.md 1 KB

Versions

RepositoryScoreLicence
This page · bifangknt/mtga100AGPL-3.0
code-yeongyu/oh-my-openagent100MIT
pleaseai/claude-code-plugins100MIT
edmundmiller/dotfiles100MIT
hakula139/plugins100MIT
code-yeongyu/lazycodex100MIT
bahayonghang/my-ai-cli-toolkit100MIT
wunki/dotfiles100MIT
pproenca/dot-skills100MIT

Database snapshot: 13.11.2026 · package version b04cbb2738e3 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 95ast-grep-search
    Find and replace code patterns structurally with ast-grep. Use when matching code by AST structure, finding functions with specific signatures, or detecting anti-patterns regex cannot match.
  • 100file-search
    This skill should be used when agents need to search codebases for text patterns or structural code patterns. Provides fast search using ripgrep for text and ast-grep for syntax-aware code search.
  • 100structural-search
    Search code by AST structure using ast-grep. Find semantic patterns like function calls, imports, class definitions instead of text patterns. Triggers on: find all calls to X, search for pattern, refactor usages, find where function is used, structural search, ast-grep, sg.
  • 100search-code
    Search for code patterns in Syncpack. Use when finding symbols, implementations, or understanding how code is used. Covers ast-grep for Rust and grep/rg for other cases.
  • 95codebase-search
    Search and navigate large codebases efficiently. Use when finding specific code patterns, tracing function calls, understanding code structure, or locating bugs. Handles semantic search, grep patterns, AST analysis.
  • 90codegraph-ast-grep
    Set up, update, or diagnose CodeGraph and ast-grep so coding agents can use semantic repository scope and structural syntax evidence automatically. Use when a repository needs an idempotent CodeGraph/ast-grep installation, stable tool and index migrations, MCP reconnection,…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.