ワンクリックで
ast-grep
语法感知的代码搜索、linting 和重写工具。支持基于 AST 的结构化代码搜索和批量代码转换。
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
语法感知的代码搜索、linting 和重写工具。支持基于 AST 的结构化代码搜索和批量代码转换。
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
Web search, online search, real-time search, internet search, Google alternative, Bing alternative, DuckDuckGo alternative, search the web, lookup online, find information, research,查询,搜索,搜索结果,网页搜索,联网搜索,实时搜索,网络查询,资料查找,信息检索,最新资讯,新闻搜索, Tavily Search API for optimized, real-time web search results for RAG. A pre-configured, cost-effective search tool.
在 macOS 上使用 osascript 执行 AppleScript 或 JavaScript for Automation (JXA) 实现系统自动化。当用户需要控制 macOS 应用(Finder、Safari、Mail、Calendar、Keynote、Numbers、Pages 等)、操作系统 UI、显示对话框/通知、读写剪贴板、自动化重复任务、或任何涉及 osascript/AppleScript/JXA 的需求时,必须使用本技能。即使用户只说"帮我自动化这个"或"在 Mac 上操作 XXX",也应优先考虑本技能。
Model configuration editor for ~/.pi/agent/models.json - 使用 Bun 脚本管理模型配置
读取和写入输出风格目录的工具技能。当用户需要:(1)读取输出风格文件,(2)写入/创建输出风格文件,(3)管理输出风格目录,(4)解析风格文件格式时使用。
Best practices for writing and maintaining high-quality role memories.
Periodic maintenance of role memory: dedup, tidy, consolidate, and pending management.
SOC 職業分類に基づく
| name | ast-grep |
| description | 语法感知的代码搜索、linting 和重写工具。支持基于 AST 的结构化代码搜索和批量代码转换。 |
AST-Grep (ast-grep/sg) 是一个快速且用户友好的工具,用于代码搜索、linting 和大规模代码重写。
| 路径类型 | 说明 |
|---|---|
| 使用方式 | 命令行工具,需要安装 ast-grep |
| 调用场景 | 语法感知的代码搜索、结构化代码分析、批量代码转换 |
| 工作目录 | 项目根目录 |
# macOS
brew install ast-grep
# Linux
cargo install ast-grep
# 验证安装
ast-grep --version
使用 AST(抽象语法树)进行代码搜索,而非简单的文本匹配。
# 搜索特定模式
ast-grep --lang python -p "def $FUNC($$$ARGS):"
# 搜索函数调用
ast-grep --lang ts -p "console.log($$$MSG)"
# 搜索条件语句
ast-grep --lang ts -p "if ($$COND) { $$$BODY }"
基于 AST 的代码检查,比传统 linter 更精确。
# 扫描项目
ast-grep scan -r sgconfig.yml
# 扫描特定目录
ast-grep scan src/
# 扫描特定文件
ast-grep scan src/main.ts
批量转换代码模式。
# 简单替换
ast-grep run -p "oldFunction($$$ARGS)" -r "newFunction($$ARGS)" --lang ts
# 复杂转换(使用 YAML 规则)
ast-grep run -r rule.yml
测试 ast-grep 规则是否正确。
# 测试规则
ast-grep test -r rule.yml
# 测试特定模式
ast-grep test -p "pattern" --lang ts
test_match_code_rule 验证规则
inside 或 has,确保使用 stopBy: end匹配单个 AST 节点,基于内在属性。
pattern - 模式匹配# 字符串模式
pattern: console.log($ARG)
# 对象模式(更精细的控制)
pattern:
selector: field_definition
context: class { $F }
strictness: relaxed
kind - 节点类型匹配kind: call_expression
regex - 正则表达式匹配regex: ^[a-z]+$
nthChild - 位置匹配# 数字
nthChild: 1
# An+B 公式
nthChild: 2n+1
# 对象形式
nthChild:
position: 1
reverse: true
ofRule:
kind: function_declaration
range - 范围匹配range:
start:
line: 0
column: 0
end:
line: 0
column: 10
基于目标节点与其他节点的关系过滤目标。
inside - 在父节点内inside:
pattern: class $C { $$$ }
stopBy: end
has - 包含子节点has:
pattern: await $EXPR
stopBy: end
precedes - 在节点之前precedes:
pattern: return $VAL
follows - 在节点之后follows:
pattern: import $M from '$P'
stopBy - 搜索终止控制# neighbor(默认):在直接周围节点不匹配时停止
stopBy: neighbor
# end:搜索到方向末尾(inside 为根,has 为叶子)
stopBy: end
# 规则对象:在周围节点匹配提供的规则时停止(包含)
stopBy:
pattern: class $END
field - 字段匹配has:
field: operator
pattern: $$OP
使用逻辑操作组合其他规则。
all - 逻辑与(AND)all:
- kind: call_expression
- pattern: console.log($ARG)
any - 逻辑或(OR)any:
- pattern: console.log($ARG)
- pattern: console.warn($ARG)
- pattern: console.error($ARG)
not - 逻辑非(NOT)not:
pattern: console.log($ARG)
matches - 规则重用matches: my-utility-rule-id
$VAR - 单个命名节点捕获# 有效
pattern: console.log($GREETING)
# 匹配
console.log('Hello World')
$$VAR - 单个未命名节点捕获# 匹配操作符
rule:
kind: binary_expression
has:
field: operator
pattern: $$OP
$$$VAR - 多节点捕获# 匹配零个或多个节点(非贪婪)
pattern: console.log($$$)
# 匹配可变参数
pattern: function $FUNC($$$ARGS) { $$$ }
_VAR - 非捕获元变量# 不捕获,可以匹配不同内容
pattern: $_FUNC($_FUNC)
# 匹配
test(a)
testFunc(1 + 1)
stopBy: end:对于关系规则,确保搜索到方向末尾has:
pattern: await $EXPR
stopBy: end
pattern: console.log($ARG)
rule:
kind: function_declaration
has:
pattern: await $EXPR
# 先确认节点类型
kind: call_expression
# 再使用 has 或 inside
has:
pattern: console.log($ARG)
inside:
pattern: class $C { $$$ }
stopBy: end
# rule.yml
id: no-console-log
language: ts
rule:
pattern: console.log($$$MSG)
# complex-rule.yml
id: prefer-const
language: ts
rule:
all:
- pattern: let $VAR = $INIT
- not:
pattern: $VAR = $EXPR
# fix-rule.yml
id: no-var
language: ts
rule:
pattern: var $VAR = $INIT
fix: const $VAR = $INIT
# async-without-await.yml
id: async-without-await
language: ts
rule:
all:
- pattern: async function $FUNC($$$ARGS) { $$$BODY }
- not:
has:
pattern: await
stopBy: end
# 查找所有 console.log
ast-grep -p "console.log($$$MSG)" --lang ts
# 查找所有未使用的变量
ast-grep -p "let $VAR = $INIT" --lang ts | grep -v "$VAR ="
# 替换 var 为 const
ast-grep run -p "var $VAR = $INIT" -r "const $VAR = $INIT" --lang ts
# 重命名函数
ast-grep run -p "oldFunc($$$ARGS)" -r "newFunc($$ARGS)" --lang ts
# 查找潜在的安全问题
ast-grep -p "eval($$$EXPR)" --lang js
# 查找 SQL 注入风险
ast-grep -p "query('$$SQL')" --lang py
# 迁移旧 API 到新 API
ast-grep run -p "oldApi($$$ARGS)" -r "newApi($$ARGS)" --lang ts
# 批量更新导入
ast-grep run -p "from 'old-lib' import $$IMPORT" -r "from 'new-lib' import $$IMPORT" --lang ts
| 特性 | ast-grep | grep | ripgrep |
|---|---|---|---|
| 语法感知 | 是 | 否 | 否 |
| 代码结构理解 | 是 | 否 | 否 |
| 跨语言支持 | 是 | 否 | 否 |
| 性能 | 快 | 快 | 最快 |
| 重写功能 | 是 | 否 | 否 |
当需要理解代码结构时,使用 ast-grep 进行语法感知搜索:
# 查找特定模式
ast-grep -p "pattern" --lang <language>
使用 ast-grep 进行批量代码转换:
# 应用重构规则
ast-grep run -r rule.yml
使用 ast-grep 验证代码质量:
# 扫描潜在问题
ast-grep scan -r sgconfig.yml
stopBy: end 确保关系规则搜索完整