ast-grep 結構化程式碼搜尋與改寫 是做什麼的?
使用 ast-grep (sg) 透過 AST 模式比對,在大型程式碼庫中進行搜尋、檢查與改寫。比起以正規表達式為基礎的程式碼轉換,此方法速度快且能理解語法結構。
安裝
請使用與您環境相符的上游安裝或設定方式:
- npm install --global @ast-grep/cli
- pip install ast-grep-cli
- brew install ast-grep
- cargo install ast-grep --locked
來自上游的需求與注意事項:
- 您可以使用
$符號加上大寫字母作為萬用字元,例如$MATCH,來匹配任何單一 AST 節點。可將其視為正規表達式中的點號(英文)。 - 您可以從 npm、pip、cargo、cargo-binstall 安裝。
基本使用或入門說明:
-
bash
-
可能需要執行 pnpm approve-builds
-
摘自上游文件:https://raw.githubusercontent.com/ast-grep/ast-grep/HEAD/README.md