Files
tldr/pages.zh/common/sg.md

20 lines
691 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# sg
> Ast-grep 是一个用于代码结构搜索、代码检查和重写的工具。
> 更多信息:<https://ast-grep.github.io/guide/introduction.html>。
- 使用交互模式扫描可能的查询:
`sg scan --interactive`
- 使用模式在当前目录中重写代码:
`sg run --pattern '{{foo}}' --rewrite '{{bar}}' --lang {{python}}`
- 可视化可能的更改而不应用它们:
`sg run --pattern '{{useState<number>($A)}}' --rewrite '{{useState($A)}}' --lang {{typescript}}`
- 将结果输出为 JSON使用 `jq` 提取信息,并使用 `jless` 进行交互式查看:
`sg run --pattern '{{Some($A)}}' --rewrite '{{None}}' --json | jq '{{.[].replacement}}' | jless`