ast-grep

张开发
2026/4/8 16:39:54 15 分钟阅读

分享文章

ast-grep
一、功能分析开源地址https://github.com/ast-grep/ast-grep支持的广泛语言范围1.后端/服务端语言Java:pattern:public class $CLASSGo:pattern:func $NAME($ARGS)Python:pattern:def $FUNC($ARGS):Ruby:pattern:def $METHODPHP:pattern:function $FN($PARAMS)C#: pattern: public void $METHOD()Rust:pattern:fn $function($params)2.系统/底层语言C:pattern:int $func(int $arg)C:pattern:class $CLASS {Rust:pattern:impl $TRAIT for $STRUCT3.脚本语言Bash/Shell:pattern:function $NAME()PowerShell:pattern:function Get-$CMDLua:pattern:function $FUNC()4.数据/配置语言YAML:pattern:$KEY: $VALUEJSON:pattern:\$KEY\: $VALUETOML:pattern:$KEY \$VALUE\XML:pattern:$TAG$CONTENT/$TAGSQL:pattern:SELECT $COLUMNS FROM $TABLE5.移动/桌面开发Swift:pattern:func $function()Kotlin:pattern:fun $FUNCTION($ARGS)Dart:pattern:void $main()基于 Tree-sitter 的完整支持列表ast-grep 使用 Tree-sitter 作为解析器后端支持50种编程语言官方支持的语言# 常用后端语言 python, java, go, rust, cpp, c, csharp, ruby, php, scala, kotlin, swift # 前端语言 javascript, typescript, jsx, tsx, vue, svelte # 函数式语言 haskell, ocaml, fsharp, elixir, clojure # 脚本语言 lua, bash, powershell, perl, r # 数据/配置 yaml, json, toml, xml, html, css, sql, graphql, protobuf # 其他 dockerfile, make, cmake, ini, markdown, regex, solidity实际使用示例1.查找所有 Go 函数# 搜索Go项目中所有函数定义sg scan-pfunc $NAME($ARGS)--langgo ./src2.查找 Python 类方法# 查找Python类中的方法sg scan-pdef $METHOD(self, $ARGS):--langpython.3.检查 Java 异常处理# 查找没有处理异常的方法sg scan-ppublic void $METHOD() throws $EXCEPTION--langjava./src4.SQL 查询分析# 查找所有SELECT语句sg scan-pSELECT $COLUMNS FROM $TABLE--langsql ./queries语言特定功能支持语言特定的语法特性Java注解GetMapping(/api)Python装饰器app.route(/)C#属性[HttpGet]Rust宏#[derive(Debug)]Go接口type $INTERFACE interface {配置文件扫描示例# Dockerfile 检查sg scan-pFROM $IMAGE:latest--langdockerfile.# Kubernetes YAMLsg scan-pimage: $NAME:$TAG--langyaml ./k8s为什么支持这么广泛Tree-sitter 生态有丰富的语法解析器统一模式语法同一套模式语法适用于所有语言可扩展架构可以轻松添加新语言支持跨语言搜索示例查找所有打印/日志语句# JavaScript/TypeScriptpattern:console.log($ARG)# Pythonpattern:print($ARG)# Javapattern:System.out.println($ARG)# Gopattern:fmt.Println($ARG)# Rustpattern:println!($ARG)实际应用场景1.多语言代码库扫描# 扫描整个包含多种语言的项目sg scan-rsecurity-rules/ ./monorepo# 可同时检查JS、Python、Go等2.API端点统一查找# 查找各种语言中的API路由定义# Express (Node.js)sg scan-papp.get($PATH,$HANDLER)--langtypescript# Flask (Python)sg scan-papp.route($PATH)--langpython# Spring Boot (Java)sg scan-pGetMapping(\$PATH\)--langjava总结ast-grep 是一个真正的多语言工具不仅限于前端。它的强大之处在于语言无关同一套工具和模式语法适用于50语言深度分析基于AST而非文本能理解代码结构企业级应用适合大型、多语言代码库的代码质量、安全扫描和重构无论是微服务架构包含多种语言还是全栈项目ast-grep 都能提供一致的代码分析体验。二、安装方式根据ast-grep的GitHub页面信息这里提供几种安装方法官方推荐安装方式1.使用Cargo安装推荐# 安装Rust工具链如果尚未安装curl--protohttps--tlsv1.2-sSfhttps://sh.rustup.rs|sh# 安装ast-grepcargoinstallast-grep# 验证安装sg--version2.使用npm安装# 全局安装npminstall-gast-grep/cli# 或作为开发依赖npminstall--save-dev ast-grep/cli3.使用Homebrew安装macOS/Linux# 添加tapbrew tap ast-grep/ast-grep# 安装brewinstallast-grep4.从源代码构建# 克隆仓库gitclone https://github.com/ast-grep/ast-grep.gitcdast-grep# 构建并安装cargoinstall--path.快速验证安装安装完成后运行以下命令验证# 查看版本sg--version# 查看帮助sg--help# 运行简单测试sg scan-pconsole.log($MSG)--langjavascript各平台安装详情Windows用户# 1. 安装Rust使用rustup-init.exe# 2. 然后运行cargo install ast-grep# 或使用WSL按照Linux方法安装Linux用户# Ubuntu/Debiansudoaptupdatesudoaptinstallbuild-essentialcurl--protohttps--tlsv1.2-sSfhttps://sh.rustup.rs|shsource$HOME/.cargo/envcargoinstallast-grepmacOS用户# 方法1使用Homebrew最简单brewinstallast-grep# 方法2使用Cargoxcode-select--install# 安装命令行工具curl--protohttps--tlsv1.2-sSfhttps://sh.rustup.rs|shcargoinstallast-grep安装后配置1.添加到PATH如果需要# 对于Cargo安装通常自动添加# 如果找不到sg命令手动添加exportPATH$HOME/.cargo/bin:$PATH2.安装语言支持ast-grep依赖tree-sitter语法文件首次使用时会自动下载# 手动安装常用语言支持sg new typescript# 安装TypeScript支持sg new python# 安装Python支持sg new go# 安装Go支持3.验证完整功能# 创建测试文件echoconsole.log(hello)test.js# 运行扫描sg scan-pconsole.log($MSG)--langjavascript test.js# 应该输出匹配结果升级ast-grep# Cargo安装cargoinstallast-grep--force# Homebrew安装brew upgrade ast-grep# npm安装npmupdate-gast-grep/cli故障排除常见问题命令未找到确保$HOME/.cargo/bin在PATH中构建失败更新Rust工具链rustup update权限问题使用sudo或修改目录权限获取帮助# 查看详细文档sg--help# 访问GitHub Issues# https://github.com/ast-grep/ast-grep/issues选择最适合你开发环境的安装方式即可开始使用ast-grep进行代码结构搜索和重构。

更多文章