skills/dwsy/ast-grep/SKILL.md
语法感知的代码搜索、linting 和重写工具。支持基于 AST 的结构化代码搜索和批量代码转换。
npx skillsauth add aiskillstore/marketplace ast-grepInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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 确保关系规则搜索完整development
Apple Human Interface Guidelines for content display components. Use this skill when the user asks about charts component, collection view, image view, web view, color well, image well, activity view, lockup, data visualization, content display, displaying images, rendering web content, color pickers, or presenting collections of items in Apple apps. Also use when the user says how should I display charts, what's the best way to show images, should I use a web view, how do I build a grid of items, what component shows media, or how do I present a share sheet. Cross-references: hig-foundations for color/typography/accessibility, hig-patterns for data visualization patterns, hig-components-layout for structural containers, hig-platforms for platform-specific component behavior.
tools
Automate HelpDesk tasks via Rube MCP (Composio): list tickets, manage views, use canned responses, and configure custom fields. Always search tools first for current schemas.
testing
Expert Haskell engineer specializing in advanced type systems, pure functional design, and high-reliability software. Use PROACTIVELY for type-level programming, concurrency, and architecture guidance.
tools
GraphQL gives clients exactly the data they need - no more, no less. One endpoint, typed schema, introspection. But the flexibility that makes it powerful also makes it dangerous. Without proper controls, clients can craft queries that bring down your server. This skill covers schema design, resolvers, DataLoader for N+1 prevention, federation for microservices, and client integration with Apollo/urql. Key insight: GraphQL is a contract. The schema is the API documentation. Design it carefully.