Shopify/rubydex

GitHub: Shopify/rubydex

Rubydex 是一个高性能 Ruby 静态分析工具包,将代码索引为可查询的语义图,支撑类型检查、代码检查和语言服务器等开发者工具的构建。

Stars: 282 | Forks: 15

# Rubydex 本项目是一个针对 Ruby 语言的高性能静态分析工具包。其目标是成为一个坚实的基础, 以支持各种工具的开发,例如类型检查器、代码检查工具、语言服务器等。 [Ruby API 文档](https://shopify.github.io/rubydex/) ## 用法 Ruby 和 Rust API 分别通过 gem 和 crate 提供。以下是使用 Ruby API 的一个简单示例: ``` # 创建一个代表当前 workspace 的新 graph graph = Rubydex::Graph.new # 配置 graph LSP encoding graph.encoding = "utf16" # 使用所有依赖项索引整个 workspace graph.index_workspace # 或者索引特定的文件路径 graph.index_all(["path/to/file.rb"]) # 通过运行 resolution 将最初收集的信息转换为其语义理解 graph.resolve # 获取分析期间采集到的所有 diagnostics graph.diagnostics # 遍历 graph nodes graph.declarations graph.documents graph.constant_references graph.method_references # 分析 require paths graph.resolve_require_path("rails/engine", load_paths) # => document pointed by `rails/engine` graph.require_paths(load_paths) # => array of all indexed require paths # 查询 graph["Foo"] # Get declaration by fully qualified name graph.search("Foo#b") # Name search graph.resolve_constant("Bar", ["Foo", "Baz::Qux"]) # Resolve constant reference based on nesting # Declarations declaration = graph["Foo"] # 所有 declarations 包括 declaration.name declaration.unqualified_name declaration.definitions declaration.owner # Namespace declarations 包括 declaration.member("bar()") declaration.member("@ivar") declaration.singleton_class declaration.ancestors declaration.descendants # Documents document = graph.documents.first document.uri document.definitions # => list of definitions discovered in this document # Definitions definition = declaration.definitions.first definition.location definition.comments definition.name definition.deprecated? definition.name_location # Locations location = definition.location location.path # Diagnostics diagnostic = graph.diagnostics.first diagnostic.rule diagnostic.message diagnostic.location ``` ## 使用 Cypher 进行查询 Rubydex 通过 [Cypher](https://opencypher.org/) 查询语言的只读子集来暴露索引图。 仅支持读取子句(`MATCH`、`WHERE`、`RETURN` 等);无法对图进行修改。 从命令行运行: ``` # 对当前 workspace 运行查询 bundle exec rdx query "MATCH (c:Class)-[:DEFINES]->(m:Method) RETURN c.name, m.name" # 将结果渲染为 JSON 而不是 table bundle exec rdx query "MATCH (c:Class) RETURN c.name" --format json # 描述可查询的 schema(node labels 和 relationship types)而无需索引 bundle exec rdx query --schema ``` 从 Ruby 运行: ``` graph = Rubydex::Graph.new graph.index_workspace graph.resolve # 解析一次,针对 graph 渲染为 table 或 JSON 字符串 query = Rubydex::Query.parse("MATCH (c:Class) RETURN c.name") puts query.render(graph, "table") puts query.render(graph, "json") # 描述 schema puts Rubydex::Query.schema("table") ``` ## MCP Server(实验性) Rubydex 可以作为 MCP (Model Context Protocol) server 运行,使 AI 助手 (如 Claude)能够对您的 Ruby 代码库进行语义查询。 ### 设置 1. 将 Rubydex 添加到您想要索引的 Ruby 项目中: gem "rubydex" 2. 安装 bundle: bundle install 3. 配置您的 MCP client 运行 `bundle exec rdx mcp`。 以 Claude Code 为例: claude mcp add --scope project rubydex -- bundle exec rdx mcp 以 Codex 为例: codex mcp add rubydex -- bundle exec rdx mcp 从该项目目录启动您的 MCP client。MCP server 会在启动时对项目进行索引, 并通过以下工具提供语义代码智能功能。 ### 可用的 MCP 工具 | 工具 | 描述 | |------|-------------| | `search_declarations` | 模糊搜索类、模块、方法和常量 | | `get_declaration` | 根据全限定名获取完整详细信息,包括文档、祖先和成员 | | `get_descendants` | 查看有哪些类/模块继承自或包含此项 | | `find_constant_references` | 在整个代码库中查找所有精确且已解析的常量引用 | | `get_file_declarations` | 列出在特定文件中定义的声明 | | `codebase_stats` | 有关已索引代码库的概要统计信息 | ## 贡献 请参阅[贡献文档](CONTRIBUTING.md)。
标签:odt, Ruby, Rust, 云安全监控, 代码质量检查, 可视化界面, 威胁情报, 安全专业人员, 开发者工具, 抽象语法树, 知识库, 网络流量审计, 语言服务器, 通知系统, 静态分析