gritql
v0.1.0-alpha.1732682283
遊樂場|教程 |文件
GritQL 是一種用於搜尋和修改原始程式碼的聲明性查詢語言。
閱讀文件、互動式教學或執行grit --help
。
安裝 Grit CLI:
curl -fsSL https://docs.grit.io/install | bash
透過將所需的模式放在反引號中來搜尋所有console.log
呼叫:
grit apply '`console.log($_)`'
將console.log
替換為winston.log
,使用=>
建立重寫:
grit apply '`console.log($msg)` => `winston.log($msg)`'
將模式儲存到grit.yaml
檔案並排除 where 子句中的測試案例:
cat << 'EOF' > .grit/grit.yaml
patterns:
- name: use_winston
level: error
body: |
`console.log($msg)` => `winston.log($msg)` where {
$msg <: not within or { `it($_, $_)`, `test($_, $_)`, `describe($_, $_)` }
}
EOF
grit apply use_winston
執行grit check
以將您的模式強制執行為自訂 lint。
grit check
console.log
調用,除非它們位於 try-catch 區塊內 `console.log($log)` => . where {
$log <: not within `try { $_ } catch { $_ }`
}
`$instance.oldMethod($args)` => `$instance.newMethod($args)` where {
$program <: contains `$instance = new TargetClass($_)`
}
在 GritQL 標準庫中可以找到更多範例。
可以組合模式來建立複雜的查詢,包括大型重構。
GritQL 來自我們進行大規模重構和遷移的經驗。
通常,遷移從探索性工作開始,以找出問題的範圍——通常使用簡單的 grep 搜尋。這些很容易開始,但大多數遷移最終都會累積額外的要求,例如確保匯入正確的套件以及排除沒有可行遷移路徑的情況。
最終,任何複雜的遷移最終都會成為使用 jscodeshift 等工具編寫的完整 codemod 程式。這也有其自身的問題:
GritQL 是我們發展強大中間立場的嘗試:
$metavariables
來表示您想要表示的漏洞。GritQL 對所有語言解析器使用樹守護者,並從 Rust 生態系統中受益匪淺。
GritQL 在 MIT 許可證下發布。
歡迎貢獻。首先,請查看貢獻指南。
您也可以加入我們的Discord 。