一键导入
ast-grep
// AST-based code search and rewrite via tree-sitter patterns. Use instead of Grep/Edit for structural matching, batch rewrites, or context-aware queries (e.g. "unwrap inside impl blocks").
// AST-based code search and rewrite via tree-sitter patterns. Use instead of Grep/Edit for structural matching, batch rewrites, or context-aware queries (e.g. "unwrap inside impl blocks").
Query the GitLab Knowledge Graph (Orbit) via `glab orbit remote` CLI subcommands or run a local copy with `glab orbit local`. Use for code-structure questions (who calls this function, where is this symbol defined), cross-project dependency and blast-radius analysis, merge-request and contributor queries, and any question answerable by traversing GitLab's unified entity graph (projects, users, MRs, issues, pipelines, files, definitions, vulnerabilities).
Audit and update documentation after code changes. Use when architecture, APIs, or behavior changed and docs may have drifted.
Investigate query evaluation failures in the Knowledge Graph synthetic data pipeline. Use when queries fail or return unexpected results after running the evaluate binary.
Profile GKG queries against ClickHouse with the query-profiler CLI. For optimizing query performance, comparing query plans, investigating slow queries, or checking ClickHouse resource usage.
Investigate the history, usage, and liveness of code using search and git blame/log. Use when determining if code is dead, understanding why something exists, finding all callers before refactoring, or deciding whether something is safe to remove. Also useful for answering "who added this and why" or "is anything still using this".
GitLab Pajamas Design System expert for building UIs with Pajamas components and patterns. Use when: (1) implementing UI that should follow GitLab's Pajamas design system, (2) selecting or configuring Pajamas/GlComponent components (GlButton, GlAlert, GlModal, etc.), (3) translating Figma designs into Pajamas-compliant code, (4) questions about Pajamas component usage, variants, categories, or accessibility, (5) building GitLab-style interfaces, or (6) the user mentions "Pajamas", "GitLab UI", "Gl components", or "design system" in a GitLab context. Works hand-in-hand with the implement-design skill and Figma MCP tools.
| name | ast-grep |
| description | AST-based code search and rewrite via tree-sitter patterns. Use instead of Grep/Edit for structural matching, batch rewrites, or context-aware queries (e.g. "unwrap inside impl blocks"). |
| allowed-tools | Bash(mise exec -- ast-grep *), Read, Glob |
Matches code by parsed AST, not text. Supports metavariable capture, relational rules, and in-place rewrite.
All commands must be run through mise exec -- since ast-grep is installed via mise:
# Pattern search
mise exec -- ast-grep run -p '$X.unwrap()' -l rust .
# Rewrite: always preview first, then apply with -U
mise exec -- ast-grep run -p '$X.lock().unwrap()' -l rust .
mise exec -- ast-grep run -p '$X.lock().unwrap()' -r '$X.lock().expect("lock poisoned")' -l rust -U .
# Structural search with YAML rules (for relational/composite logic)
mise exec -- ast-grep scan --inline-rules 'id: name
language: rust
rule:
pattern: $X.unwrap()
inside:
kind: impl_item
stopBy: end' .
# JSON output for programmatic use
mise exec -- ast-grep run -p '$X.unwrap()' -l rust --json .
# Debug: see how ast-grep parses your pattern or target code
mise exec -- ast-grep run -p 'PATTERN' -l rust --debug-query=cst # concrete syntax tree
mise exec -- ast-grep run -p 'PATTERN' -l rust --debug-query=pattern # metavar detection
| Syntax | Meaning |
|---|---|
$VAR | Single named node. Reuse = must match identically ($A == $A matches x == x only) |
$$VAR | Single unnamed node (operators, punctuation) |
$$$ | Zero or more nodes (variadic args, statements) |
$_ prefix | Non-capturing (each occurrence matches independently) |
Patterns must be valid parseable code. Bare .unwrap() is an ERROR — use $X.unwrap().
Use --selector KIND to disambiguate patterns that parse as the wrong node type. Provide surrounding context in the pattern and select the sub-node you actually want: mise exec -- ast-grep run -p 'struct S { pub $N: $T }' --selector field_declaration -l rust .
Rules combine three categories:
pattern, kind, regex, nthChildhas, inside, precedes, follows — always add stopBy: endall, any, not, matchesrule:
all:
- kind: function_item # atomic: match by node type
- has: # relational: must contain
kind: await_expression
stopBy: end # required: search entire subtree
- not: # composite: exclude
has:
kind: try_expression
stopBy: end
Use constraints in YAML rules to filter metavariable text by regex:
rule:
pattern: $X.$METHOD()
constraints:
METHOD:
regex: "^(unwrap|expect)$"
Use --debug-query=cst to discover kinds at runtime. Non-obvious mappings:
| Kind | Note |
|---|---|
function_item | All fn declarations (pub, async, const, etc.) |
impl_item | Both inherent and trait impls |
method_call_expression | x.foo() — distinct from call_expression (foo()) |
macro_invocation | println!(...), vec![...] |
await_expression | expr.await |
try_expression | The ? operator |
function_modifiers | Contains async, unsafe, const keywords |
visibility_modifier | pub, pub(crate), etc. |
Use single-quoted strings for inline rules to avoid $ expansion. If you must double-quote, escape as \$VAR, \$\$\$.