Full Code of ast-grep/claude-skill for AI

main 577f4d450767 cached
5 files
28.4 KB
7.3k tokens
1 requests
Download .txt
Repository: ast-grep/claude-skill
Branch: main
Commit: 577f4d450767
Files: 5
Total size: 28.4 KB

Directory structure:
gitextract_enzl3012/

├── .claude-plugin/
│   └── marketplace.json
├── README.md
└── ast-grep/
    ├── .claude-plugin/
    │   └── plugin.json
    └── skills/
        └── ast-grep/
            ├── SKILL.md
            └── references/
                └── rule_reference.md

================================================
FILE CONTENTS
================================================

================================================
FILE: .claude-plugin/marketplace.json
================================================
{
  "name": "ast-grep-marketplace",
  "owner": {
    "name": "Herrington Darkholme"
  },
  "metadata": {
    "description": "A marketplace for Claude Code skills including ast-grep structural code search",
    "version": "1.0.0",
    "repository": "https://github.com/ast-grep/claude-skill",
    "homepage": "https://github.com/ast-grep/claude-skill"
  },
  "plugins": [
    {
      "name": "ast-grep",
      "source": "./ast-grep",
      "description": "Guide for writing ast-grep rules to perform structural code search and analysis. Use when users need to search codebases using Abstract Syntax Tree (AST) patterns, find specific code structures, or perform complex code queries that go beyond simple text search.",
      "version": "1.0.0",
      "keywords": [
        "ast-grep",
        "code-search",
        "ast",
        "structural-search",
        "pattern-matching",
        "refactoring"
      ]
    }
  ]
}


================================================
FILE: README.md
================================================
# ast-grep Plugin Marketplace for AI Agents

A Claude Code plugin marketplace containing the ast-grep skill for powerful structural code search using Abstract Syntax Tree (AST) patterns. Search your codebase based on code structure rather than just text matching.

## What is This?

This is a **Claude Code plugin marketplace** that provides the **ast-grep plugin**. The plugin includes a skill that teaches Claude how to write and use ast-grep rules to perform advanced code searches. Unlike traditional text-based search (grep, ripgrep), ast-grep understands the structure of your code, allowing you to find patterns like:

- "Find all async functions that don't have error handling"
- "Locate all React components that use a specific hook"
- "Find functions with more than 3 parameters"
- "Search for console.log calls inside class methods"




## Prerequisites

You need to have ast-grep installed on your system:

```bash
# macOS
brew install ast-grep

# npm
npm install -g @ast-grep/cli

# cargo
cargo install ast-grep
```

Verify installation:
```bash
ast-grep --version
```

## Installation

### Option 1: Install via skills.sh (Recommended)

```
npx skills add ast-grep/agent-skill
```

### Option 2: Install via Marketplace 

1. **Add the marketplace** to your Claude Code:

```bash
/plugin marketplace add ast-grep/agent-skill
```

2. **Install the ast-grep plugin**:

```bash
/plugin install ast-grep
```

3. **Restart Claude Code** to activate the plugin

4. **Verify installation**: Use `/help` to see if ast-grep skill is available

### Option 3: Install Locally for Development

If you're developing or testing locally:

```bash
# Clone the repository
git clone https://github.com/ast-grep/claude-skill.git /path/to/local-marketplace

# Add as local marketplace
/plugin marketplace add /path/to/local-marketplace

# Install the plugin
/plugin install ast-grep
```

### Usage Notes

You will need to ask Claude to use this skill explicitly in your queries, like "Use ast-grep to find...". Claude Code, as of Nov 2025, cannot automatically detect when to use ast-grep for all appropriate use cases.

## How to Use

Once installed, simply ask Claude to search your code using structural patterns. Claude will automatically use this skill when appropriate.

### Example Queries

**Find async functions with await:**
```
Find all async functions in this project that use await
```

**Find missing error handling:**
```
Show me async functions that don't have try-catch blocks
```

**Find specific function calls:**
```
Find all places where we call console.log with more than one argument
```

**Find code in specific contexts:**
```
Find all setState calls inside useEffect hooks
```

### How It Works

When you ask Claude to search for code patterns:

1. Claude analyzes your query and determines if ast-grep is appropriate
2. It creates example code that matches your search criteria
3. It writes an ast-grep rule to match the pattern
4. It tests the rule against the example code
5. Once verified, it searches your entire codebase
6. Results are presented with file paths and line numbers

## Supported Languages

ast-grep supports many programming languages including:

- JavaScript/TypeScript
- Python
- Rust
- Go
- Java
- C/C++
- Ruby
- PHP
- And many more

## Key Features

- **Structure-aware search**: Matches code based on AST structure, not just text
- **Metavariables**: Use `$VAR` to match any expression, statement, or identifier
- **Relational queries**: Find code inside specific contexts (e.g., "find X inside Y")
- **Composite logic**: Combine rules with AND, OR, NOT operations
- **Test-driven approach**: Rules are tested before running on your codebase

## Advanced Usage

### Direct ast-grep Commands

While Claude will handle most use cases automatically, you can also use ast-grep directly:

```bash
# Simple pattern search
ast-grep run --pattern 'console.log($ARG)' --lang javascript .

# Complex rule-based search
ast-grep scan --inline-rules "id: my-rule
language: javascript
rule:
  kind: function_declaration
  has:
    pattern: await \$EXPR
    stopBy: end" .
```

### Debugging

If a search isn't working as expected, ask Claude to:
- Show you the ast-grep rule it created
- Inspect the AST structure of your code
- Test the rule against example code

## Repository Structure

This is a Claude Code plugin marketplace repository with the following structure:

```
claude-skill/
├── .claude-plugin/
│   └── marketplace.json           # Marketplace manifest
├── ast-grep/                       # ast-grep plugin
│   ├── .claude-plugin/
│   │   └── plugin.json            # Plugin manifest
│   └── skills/
│       └── ast-grep/
│           ├── SKILL.md           # Skill instructions for Claude
│           └── references/
│               └── rule_reference.md  # ast-grep rule documentation
├── ast-grep.zip                    # Archived version
└── README.md                       # This file
```

### Key Files

- **`.claude-plugin/marketplace.json`**: Marketplace manifest defining available plugins
- **`ast-grep/.claude-plugin/plugin.json`**: Plugin manifest for the ast-grep plugin
- **`ast-grep/skills/ast-grep/SKILL.md`**: Main skill instructions that Claude uses
- **`ast-grep/skills/ast-grep/references/`**: Supporting documentation and reference materials

## Tips for Best Results

1. **Be specific**: The more details you provide, the better the search results
2. **Provide examples**: If possible, show Claude an example of what you want to find
3. **Iterate**: Start with a broad search and narrow it down
4. **Ask for explanations**: Ask Claude to explain the ast-grep rule it creates

## Examples of What You Can Search For

### Code Quality
- Functions without return statements
- Functions with too many parameters
- Unused variables
- Missing null checks

### Patterns
- React hooks usage patterns
- API call patterns
- Database query patterns
- Error handling patterns

### Refactoring
- Find all uses of deprecated functions
- Locate code that needs migration
- Find inconsistent patterns across codebase

### Security
- Potential SQL injection points
- Unsafe eval usage
- Missing input validation

## Troubleshooting

**Claude isn't using the skill:**
- Make sure ast-grep is installed (`ast-grep --version`)
- Try being more explicit: "Use ast-grep to search for..."

**No results found:**
- Try a simpler query first
- Ask Claude to show you the rule and test it
- Provide an example of code you want to match

**Unexpected results:**
- Refine your query with more details
- Ask Claude to exclude certain patterns
- Request to see the AST structure of your code

## Contributing

To improve this skill:
1. Edit `SKILL.md` to update Claude's instructions
2. Add examples to `references/` directory
3. Test with various code patterns

## Resources

- [ast-grep Official Documentation](https://ast-grep.github.io/)
- [ast-grep Playground](https://ast-grep.github.io/playground.html) - Test patterns online
- [ast-grep GitHub](https://github.com/ast-grep/ast-grep)

## License

This skill follows ast-grep's MIT license for any included documentation or examples.

## Support

For issues with:
- **This skill**: Open an issue in this repository
- **ast-grep itself**: Visit [ast-grep GitHub](https://github.com/ast-grep/ast-grep)
- **Claude Code**: Check [Claude Code documentation](https://code.claude.com/)


================================================
FILE: ast-grep/.claude-plugin/plugin.json
================================================
{
  "name": "ast-grep",
  "version": "1.0.0",
  "description": "Guide for writing ast-grep rules to perform structural code search and analysis. Use when users need to search codebases using Abstract Syntax Tree (AST) patterns, find specific code structures, or perform complex code queries that go beyond simple text search.",
  "author": {
    "name": "Herrington Darkholme"
  },
  "homepage": "https://github.com/ast-grep/claude-skill",
  "repository": "https://github.com/ast-grep/claude-skill",
  "license": "MIT",
  "keywords": [
    "ast-grep",
    "code-search",
    "ast",
    "structural-search",
    "pattern-matching",
    "refactoring"
  ]
}


================================================
FILE: ast-grep/skills/ast-grep/SKILL.md
================================================
---
name: ast-grep
description: Guide for writing ast-grep rules to perform structural code search and analysis. Use when users need to search codebases using Abstract Syntax Tree (AST) patterns, find specific code structures, or perform complex code queries that go beyond simple text search. This skill should be used when users ask to search for code patterns, find specific language constructs, or locate code with particular structural characteristics.
---

# ast-grep Code Search

## Overview

This skill helps translate natural language queries into ast-grep rules for structural code search. ast-grep uses Abstract Syntax Tree (AST) patterns to match code based on its structure rather than just text, enabling powerful and precise code search across large codebases.

## When to Use This Skill

Use this skill when users:
- Need to search for code patterns using structural matching (e.g., "find all async functions that don't have error handling")
- Want to locate specific language constructs (e.g., "find all function calls with specific parameters")
- Request searches that require understanding code structure rather than just text
- Ask to search for code with particular AST characteristics
- Need to perform complex code queries that traditional text search cannot handle

## General Workflow

Follow this process to help users write effective ast-grep rules:

### Step 1: Understand the Query

Clearly understand what the user wants to find. Ask clarifying questions if needed:
- What specific code pattern or structure are they looking for?
- Which programming language?
- Are there specific edge cases or variations to consider?
- What should be included or excluded from matches?

### Step 2: Create Example Code

Write a simple code snippet that represents what the user wants to match. Save this to a temporary file for testing.

**Example:**
If searching for "async functions that use await", create a test file:

```javascript
// test_example.js
async function example() {
  const result = await fetchData();
  return result;
}
```

### Step 3: Write the ast-grep Rule

Translate the pattern into an ast-grep rule. Start simple and add complexity as needed.

**Key principles:**
- Always use `stopBy: end` for relational rules (`inside`, `has`) to ensure search goes to the end of the direction
- Use `pattern` for simple structures
- Use `kind` with `has`/`inside` for complex structures
- Break complex queries into smaller sub-rules using `all`, `any`, or `not`

**Example rule file (test_rule.yml):**
```yaml
id: async-with-await
language: javascript
rule:
  kind: function_declaration
  has:
    pattern: await $EXPR
    stopBy: end
```

See `references/rule_reference.md` for comprehensive rule documentation.

### Step 4: Test the Rule

Use ast-grep CLI to verify the rule matches the example code. There are two main approaches:

**Option A: Test with inline rules (for quick iterations)**
```bash
echo "async function test() { await fetch(); }" | ast-grep scan --inline-rules "id: test
language: javascript
rule:
  kind: function_declaration
  has:
    pattern: await \$EXPR
    stopBy: end" --stdin
```

**Option B: Test with rule files (recommended for complex rules)**
```bash
ast-grep scan --rule test_rule.yml test_example.js
```

**Debugging if no matches:**
1. Simplify the rule (remove sub-rules)
2. Add `stopBy: end` to relational rules if not present
3. Use `--debug-query` to understand the AST structure (see below)
4. Check if `kind` values are correct for the language

### Step 5: Search the Codebase

Once the rule matches the example code correctly, search the actual codebase:

**For simple pattern searches:**
```bash
ast-grep run --pattern 'console.log($ARG)' --lang javascript /path/to/project
```

**For complex rule-based searches:**
```bash
ast-grep scan --rule my_rule.yml /path/to/project
```

**For inline rules (without creating files):**
```bash
ast-grep scan --inline-rules "id: my-rule
language: javascript
rule:
  pattern: \$PATTERN" /path/to/project
```

## ast-grep CLI Commands

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

Dump the AST structure to understand how code is parsed:

```bash
ast-grep run --pattern 'async function example() { await fetch(); }' \
  --lang javascript \
  --debug-query=cst
```

**Available formats:**
- `cst`: Concrete Syntax Tree (shows all nodes including punctuation)
- `ast`: Abstract Syntax Tree (shows only named nodes)
- `pattern`: Shows how ast-grep interprets your pattern

**Use this to:**
- Find the correct `kind` values for nodes
- Understand the structure of code you want to match
- Debug why patterns aren't matching

**Example:**
```bash
# See the structure of your target code
ast-grep run --pattern 'class User { constructor() {} }' \
  --lang javascript \
  --debug-query=cst

# See how ast-grep interprets your pattern
ast-grep run --pattern 'class $NAME { $$$BODY }' \
  --lang javascript \
  --debug-query=pattern
```

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

Test a rule against code snippet without creating files:

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

**Add --json for structured output:**
```bash
echo "const x = await fetch();" | ast-grep scan --inline-rules "..." --stdin --json
```

### Search with Patterns (run)

Simple pattern-based search for single AST node matches:

```bash
# Basic pattern search
ast-grep run --pattern 'console.log($ARG)' --lang javascript .

# Search specific files
ast-grep run --pattern 'class $NAME' --lang python /path/to/project

# JSON output for programmatic use
ast-grep run --pattern 'function $NAME($$$)' --lang javascript --json .
```

**When to use:**
- Simple, single-node matches
- Quick searches without complex logic
- When you don't need relational rules (inside/has)

### Search with Rules (scan)

YAML rule-based search for complex structural queries:

```bash
# With rule file
ast-grep scan --rule my_rule.yml /path/to/project

# With inline rules
ast-grep scan --inline-rules "id: find-async
language: javascript
rule:
  kind: function_declaration
  has:
    pattern: await \$EXPR
    stopBy: end" /path/to/project

# JSON output
ast-grep scan --rule my_rule.yml --json /path/to/project
```

**When to use:**
- Complex structural searches
- Relational rules (inside, has, precedes, follows)
- Composite logic (all, any, not)
- When you need the power of full YAML rules

**Tip:** For relational rules (inside/has), always add `stopBy: end` to ensure complete traversal.

## Tips for Writing Effective Rules

### Always Use stopBy: end

For relational rules, always use `stopBy: end` unless there's a specific reason not 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

Begin with the simplest rule that could work:
1. Try a `pattern` first
2. If that doesn't work, try `kind` to match the node type
3. Add relational rules (`has`, `inside`) as needed
4. Combine with composite rules (`all`, `any`, `not`) for complex logic

### Use the Right Rule Type

- **Pattern**: For simple, direct code matching (e.g., `console.log($ARG)`)
- **Kind + Relational**: For complex structures (e.g., "function containing await")
- **Composite**: For logical combinations (e.g., "function with await but not in try-catch")

### Debug with AST Inspection

When rules don't match:
1. Use `--debug-query=cst` to see the actual AST structure
2. Check if metavariables are being detected correctly
3. Verify the node `kind` matches what you expect
4. Ensure relational rules are searching in the right direction

### Escaping in Inline Rules

When using `--inline-rules`, escape metavariables in shell commands:
- Use `\$VAR` instead of `$VAR` (shell interprets `$` as variable)
- Or use single quotes: `'$VAR'` works in most shells

**Example:**
```bash
# Correct: escaped $
ast-grep scan --inline-rules "rule: {pattern: 'console.log(\$ARG)'}" .

# Or use single quotes
ast-grep scan --inline-rules 'rule: {pattern: "console.log($ARG)"}' .
```

## Common Use Cases

### Find Functions with Specific Content

Find async functions that use await:
```bash
ast-grep scan --inline-rules "id: async-await
language: javascript
rule:
  all:
    - kind: function_declaration
    - has:
        pattern: await \$EXPR
        stopBy: end" /path/to/project
```

### Find Code Inside Specific Contexts

Find console.log inside class methods:
```bash
ast-grep scan --inline-rules "id: console-in-class
language: javascript
rule:
  pattern: console.log(\$\$\$)
  inside:
    kind: method_definition
    stopBy: end" /path/to/project
```

### Find Code Missing Expected Patterns

Find async functions without try-catch:
```bash
ast-grep scan --inline-rules "id: async-no-trycatch
language: javascript
rule:
  all:
    - kind: function_declaration
    - has:
        pattern: await \$EXPR
        stopBy: end
    - not:
        has:
          pattern: try { \$\$\$ } catch (\$E) { \$\$\$ }
          stopBy: end" /path/to/project
```

## Resources

### references/
Contains detailed documentation for ast-grep rule syntax:
- `rule_reference.md`: Comprehensive ast-grep rule documentation covering atomic rules, relational rules, composite rules, and metavariables

Load these references when detailed rule syntax information is needed.


================================================
FILE: ast-grep/skills/ast-grep/references/rule_reference.md
================================================
# ast-grep Rule Reference

This document provides comprehensive documentation for ast-grep rule syntax, covering all rule types and metavariables.

## Introduction to ast-grep Rules

ast-grep rules are declarative specifications for matching and filtering Abstract Syntax Tree (AST) nodes. They enable structural code search and analysis by defining conditions an AST node must meet to be matched.

### Rule Categories

ast-grep rules are categorized into three types:

* **Atomic Rules**: Match individual AST nodes based on intrinsic properties like code patterns (`pattern`), node type (`kind`), or text content (`regex`).
* **Relational Rules**: Define conditions based on a target node's position or relationship to other nodes (e.g., `inside`, `has`, `precedes`, `follows`).
* **Composite Rules**: Combine other rules using logical operations (AND, OR, NOT) to form complex matching criteria (e.g., `all`, `any`, `not`, `matches`).

## Anatomy of an ast-grep Rule Object

The ast-grep rule object is the core configuration unit defining how ast-grep identifies and filters AST nodes. It's typically written in YAML format.

### General Structure

Every field within an ast-grep Rule Object is optional, but at least one "positive" key (e.g., `kind`, `pattern`) must be present.

A node matches a rule if it satisfies all fields defined within that rule object, implying an implicit logical AND operation.

For rules using metavariables that depend on prior matching, explicit `all` composite rules are recommended to guarantee execution order.

### Rule Object Properties

| Property | Type | Category | Purpose | Example |
| :--- | :--- | :--- | :--- | :--- |
| `pattern` | String or Object | Atomic | Matches AST node by code pattern. | `pattern: console.log($ARG)` |
| `kind` | String | Atomic | Matches AST node by its kind name. | `kind: call_expression` |
| `regex` | String | Atomic | Matches node's text by Rust regex. | `regex: ^[a-z]+$` |
| `nthChild` | number, string, Object | Atomic | Matches nodes by their index within parent's children. | `nthChild: 1` |
| `range` | RangeObject | Atomic | Matches node by character-based start/end positions. | `range: { start: { line: 0, column: 0 }, end: { line: 0, column: 10 } }` |
| `inside` | Object | Relational | Target node must be inside node matching sub-rule. | `inside: { pattern: class $C { $$$ }, stopBy: end }` |
| `has` | Object | Relational | Target node must have descendant matching sub-rule. | `has: { pattern: await $EXPR, stopBy: end }` |
| `precedes` | Object | Relational | Target node must appear before node matching sub-rule. | `precedes: { pattern: return $VAL }` |
| `follows` | Object | Relational | Target node must appear after node matching sub-rule. | `follows: { pattern: import $M from '$P' }` |
| `all` | Array<Rule> | Composite | Matches if all sub-rules match. | `all: [ { kind: call_expression }, { pattern: foo($A) } ]` |
| `any` | Array<Rule> | Composite | Matches if any sub-rules match. | `any: [ { pattern: foo() }, { pattern: bar() } ]` |
| `not` | Object | Composite | Matches if sub-rule does not match. | `not: { pattern: console.log($ARG) }` |
| `matches` | String | Composite | Matches if predefined utility rule matches. | `matches: my-utility-rule-id` |

## Atomic Rules

Atomic rules match individual AST nodes based on their intrinsic properties.

### pattern: String and Object Forms

The `pattern` rule matches a single AST node based on a code pattern.

**String Pattern**: Directly matches using ast-grep's pattern syntax with metavariables.

```yaml
pattern: console.log($ARG)
```

**Object Pattern**: Offers granular control for ambiguous patterns or specific contexts.

* `selector`: Pinpoints a specific part of the parsed pattern to match.
  ```yaml
  pattern:
    selector: field_definition
    context: class { $F }
  ```

* `context`: Provides surrounding code context for correct parsing.

* `strictness`: Modifies the pattern's matching algorithm (`cst`, `smart`, `ast`, `relaxed`, `signature`).
  ```yaml
  pattern:
    context: foo($BAR)
    strictness: relaxed
  ```

### kind: Matching by Node Type

The `kind` rule matches an AST node by its `tree_sitter_node_kind` name, derived from the language's Tree-sitter grammar. Useful for targeting constructs like `call_expression` or `function_declaration`.

```yaml
kind: call_expression
```

### regex: Text-Based Node Matching

The `regex` rule matches the entire text content of an AST node using a Rust regular expression. It's not a "positive" rule, meaning it matches any node whose text satisfies the regex, regardless of its structural kind.

### nthChild: Positional Node Matching

The `nthChild` rule finds nodes by their 1-based index within their parent's children list, counting only named nodes by default.

* `number`: Matches the exact nth child. Example: `nthChild: 1`
* `string`: Matches positions using An+B formula. Example: `2n+1`
* `Object`: Provides granular control:
  * `position`: `number` or An+B string.
  * `reverse`: `true` to count from the end.
  * `ofRule`: An ast-grep rule to filter the sibling list before counting.

### range: Position-Based Node Matching

The `range` rule matches an AST node based on its character-based start and end positions. A `RangeObject` defines `start` and `end` fields, each with 0-based `line` and `column`. `start` is inclusive, `end` is exclusive.

## Relational Rules

Relational rules filter targets based on their position relative to other AST nodes. They can include `stopBy` and `field` options.

### inside: Matching Within a Parent Node

Requires the target node to be inside another node matching the `inside` sub-rule.

```yaml
inside:
  pattern: class $C { $$$ }
  stopBy: end
```

### has: Matching with a Descendant Node

Requires the target node to have a descendant node matching the `has` sub-rule.

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

### precedes and follows: Sequential Node Matching

* `precedes`: Target node must appear before a node matching the `precedes` sub-rule.
* `follows`: Target node must appear after a node matching the `follows` sub-rule.

Both include `stopBy` but not `field`.

### stopBy and field: Refining Relational Searches

**stopBy**: Controls search termination for relational rules.

* `"neighbor"` (default): Stops when immediate surrounding node doesn't match.
* `"end"`: Searches to the end of the direction (root for `inside`, leaf for `has`).
* `Rule object`: Stops when a surrounding node matches the provided rule (inclusive).

**field**: Specifies a sub-node within the target node that should match the relational rule. Only for `inside` and `has`.

**Best Practice**: When unsure, always use `stopBy: end` to ensure the search goes to the end of the direction.

## Composite Rules

Composite rules combine atomic and relational rules using logical operations.

### all: Conjunction (AND) of Rules

Matches a node only if all sub-rules in the list match. Guarantees order of rule matching, important for metavariables.

```yaml
all:
  - kind: call_expression
  - pattern: console.log($ARG)
```

### any: Disjunction (OR) of Rules

Matches a node if any sub-rules in the list match.

```yaml
any:
  - pattern: console.log($ARG)
  - pattern: console.warn($ARG)
  - pattern: console.error($ARG)
```

### not: Negation (NOT) of a Rule

Matches a node if the single sub-rule does not match.

```yaml
not:
  pattern: console.log($ARG)
```

### matches: Rule Reuse and Utility Rules

Takes a rule-id string, matching if the referenced utility rule matches. Enables rule reuse and recursive rules.

## Metavariables

Metavariables are placeholders in patterns to match dynamic content in the AST.

### $VAR: Single Named Node Capture

Captures a single named node in the AST.

* **Valid**: `$META`, `$META_VAR`, `$_`
* **Invalid**: `$invalid`, `$123`, `$KEBAB-CASE`
* **Example**: `console.log($GREETING)` matches `console.log('Hello World')`.
* **Reuse**: `$A == $A` matches `a == a` but not `a == b`.

### $$VAR: Single Unnamed Node Capture

Captures a single unnamed node (e.g., operators, punctuation).

**Example**: To match the operator in `a + b`, use `$$OP`.

```yaml
rule:
  kind: binary_expression
  has:
    field: operator
    pattern: $$OP
```

### $$$MULTI_META_VARIABLE: Multi-Node Capture

Matches zero or more AST nodes (non-greedy). Useful for variable numbers of arguments or statements.

* **Example**: `console.log($$$)` matches `console.log()`, `console.log('hello')`, and `console.log('debug:', key, value)`.
* **Example**: `function $FUNC($$$ARGS) { $$$ }` matches functions with varying parameters/statements.

### Non-Capturing Metavariables (_VAR)

Metavariables starting with an underscore (`_`) are not captured. They can match different content even if named identically, optimizing performance.

* **Example**: `$_FUNC($_FUNC)` matches `test(a)` and `testFunc(1 + 1)`.

### Important Considerations for Metavariable Detection

* **Syntax Matching**: Only exact metavariable syntax (e.g., `$A`, `$$B`, `$$$C`) is recognized.
* **Exclusive Content**: Metavariable text must be the only text within an AST node.
* **Non-working**: `obj.on$EVENT`, `"Hello $WORLD"`, `a $OP b`, `$jq`.

The ast-grep playground is useful for debugging patterns and visualizing metavariables.

## Common Patterns and Examples

### Finding Functions with Specific Content

Find functions that contain await expressions:

```yaml
rule:
  kind: function_declaration
  has:
    pattern: await $EXPR
    stopBy: end
```

### Finding Code Inside Specific Contexts

Find console.log calls inside class methods:

```yaml
rule:
  pattern: console.log($$$)
  inside:
    kind: method_definition
    stopBy: end
```

### Combining Multiple Conditions

Find async functions that use await but don't have try-catch:

```yaml
rule:
  all:
    - kind: function_declaration
    - has:
        pattern: await $EXPR
        stopBy: end
    - not:
        has:
          pattern: try { $$$ } catch ($E) { $$$ }
          stopBy: end
```

### Matching Multiple Alternatives

Find any type of console method call:

```yaml
rule:
  any:
    - pattern: console.log($$$)
    - pattern: console.warn($$$)
    - pattern: console.error($$$)
    - pattern: console.debug($$$)
```

## Troubleshooting Tips

1. **Rule doesn't match**: Use `dump_syntax_tree` to see the actual AST structure
2. **Relational rule issues**: Ensure `stopBy: end` is set for deep searches
3. **Wrong node kind**: Check the language's Tree-sitter grammar for correct kind names
4. **Metavariable not working**: Ensure it's the only content in its AST node
5. **Pattern too complex**: Break it down into simpler sub-rules using `all`
Download .txt
gitextract_enzl3012/

├── .claude-plugin/
│   └── marketplace.json
├── README.md
└── ast-grep/
    ├── .claude-plugin/
    │   └── plugin.json
    └── skills/
        └── ast-grep/
            ├── SKILL.md
            └── references/
                └── rule_reference.md
Condensed preview — 5 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (31K chars).
[
  {
    "path": ".claude-plugin/marketplace.json",
    "chars": 922,
    "preview": "{\n  \"name\": \"ast-grep-marketplace\",\n  \"owner\": {\n    \"name\": \"Herrington Darkholme\"\n  },\n  \"metadata\": {\n    \"descriptio"
  },
  {
    "path": "README.md",
    "chars": 7372,
    "preview": "# ast-grep Plugin Marketplace for AI Agents\n\nA Claude Code plugin marketplace containing the ast-grep skill for powerful"
  },
  {
    "path": "ast-grep/.claude-plugin/plugin.json",
    "chars": 655,
    "preview": "{\n  \"name\": \"ast-grep\",\n  \"version\": \"1.0.0\",\n  \"description\": \"Guide for writing ast-grep rules to perform structural c"
  },
  {
    "path": "ast-grep/skills/ast-grep/SKILL.md",
    "chars": 9434,
    "preview": "---\nname: ast-grep\ndescription: Guide for writing ast-grep rules to perform structural code search and analysis. Use whe"
  },
  {
    "path": "ast-grep/skills/ast-grep/references/rule_reference.md",
    "chars": 10697,
    "preview": "# ast-grep Rule Reference\n\nThis document provides comprehensive documentation for ast-grep rule syntax, covering all rul"
  }
]

About this extraction

This page contains the full source code of the ast-grep/claude-skill GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 5 files (28.4 KB), approximately 7.3k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!