Community程式設計與開發github.com

jlholm/noodle

基於 ast-grep 的結構化程式碼搜尋——當程式碼形狀與元素關係比純文字更重要時使用(例如:「尋找沒有錯誤處理的非同步函式」、「將 foo(a, b) 重構為 foo({ a, b })」)。純文字名稱查詢請用 Grep。

noodle 是什麼?

此技能整合了 ast-grep,提供強大的結構化程式碼搜尋與重構能力。與基於文字模式的傳統 grep 不同,它讓開發者能根據抽象語法樹(AST)節點關係(如「非同步函式」、「條件陳述式」)來匹配程式碼模式,而非僅靠字串。使用者可定義 YAML 規則,使用 `pattern`、`kind`、`has`、`inside` 等模式,精確定位複雜的程式碼結構。支援以 `--stdin` 測試規則,並透過 `--debug-query` 檢視 AST 以除錯。此工具在 Claude Code 或 Cursor 等 AI 代理平台中特別有用,能精準執行依賴程式碼語意的重構與分析任務。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/jlholm/noodle/tree/HEAD/.agents/skills/ast-grep

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

ast-grep

工作流程

  1. 寫一個代表目標程式碼的測試片段
  2. 撰寫規則(先從 pattern 開始,必要時升級到 kind + has / inside)
  3. 在搜尋程式碼庫之前,先用 --stdin 測試規則
  4. 規則符合後再搜尋整個程式碼庫

關鍵注意事項

在關係規則上務必使用 stopBy: end

如果不使用,has / inside 會在第一個不符合的節點處停止,而非遍歷整個子樹:

# 錯誤——會遺漏深層巢狀的匹配
has:
  pattern: await $EXPR

# 正確
has:
  pattern: await $EXPR
  stopBy: end

在 shell 中跳脫元變數

$VAR 會被 shell 解譯。請使用跳脫或單引號:

# 雙引號:用反斜線跳脫
ast-grep scan --inline-rules "id: test
language: javascript
rule:
  pattern: await \$EXPR" .

# 單引號:無需跳脫
ast-grep scan --inline-rules 'id: test
language: javascript
rule:
  pattern: await $EXPR' .

元變數必須是 AST 節點的唯一內容

以下語法無效:obj.on$EVENT、"Hello $WORLD"、a $OP b、$jq

對未命名節點(運算子、標點符號)請使用 $$OP。對零或多個節點請使用 $$$ARGS。

使用 --stdin 測試

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

使用 --debug-query 除錯

當規則不匹配時,檢查 AST 以找出正確的 kind 值:

ast-grep run --pattern 'your code here' --lang javascript --debug-query=cst

格式:cst(所有節點)、ast(僅命名節點)、pattern(ast-grep 如何解讀你的模式)。

規則語法

完整規則參考(原子規則、關係規則、複合規則及元變數)請參閱 references/rule_reference.md。

Individual skills in this repo

This repo contains 3 individual skills — each has its own dedicated page.

相關技能