1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::path::Path;
use tree_sitter::{Node, Parser};
use crate::semantic::adapter::LanguageAdapter;
use crate::semantic::common::{node_text, signature_first_line};
use crate::semantic::types::{ByteRange, ExtractedFile, Import, ImportKind, Symbol, SymbolKind};
/// Tree-sitter adapter for Ruby.
///
/// Ruby visibility (`public` / `private` / `protected`) is set by
/// keyword inside a class body, not at the method declaration site.
/// Without scanning the surrounding token stream we can't tell which
/// methods are private, so every method is reported as exported.
/// Users who want privacy-aware analysis should rely on LSP
/// (solargraph) for that signal.
///
/// `module` becomes `SymbolKind::Interface` since Ruby modules are
/// closest in spirit to an interface/mixin contract; `class`
/// becomes `SymbolKind::Class`; `def` becomes Function at top
/// level and Method inside a class/module.
pub struct RubyAdapter;
impl RubyAdapter {
fn signature(&self, n: Node, s: &[u8]) -> String {
signature_first_line(n, s)
}
fn method_name<'a>(&self, n: Node<'a>, s: &'a [u8]) -> Option<String> {
// `def name(args); body; end` — the name child is an
// `identifier` (instance method) or `constant` / scope-qualified
// for class methods (`def self.foo`).
for i in 0..n.named_child_count() {
let c = n.named_child(i)?;
match c.kind() {
"identifier" | "constant" | "operator" => return Some(node_text(c, s).to_string()),
_ => {}
}
}
None
}
fn class_or_module_name<'a>(&self, n: Node<'a>, s: &'a [u8]) -> Option<String> {
// First `constant` child is the class/module name. (Ruby
// allows reopening qualified names like `Foo::Bar` — we
// surface the leaf.)
for i in 0..n.named_child_count() {
let c = n.named_child(i)?;
if c.kind() == "constant" {
return Some(node_text(c, s).to_string());
}
if c.kind() == "scope_resolution" {
// Reach the rightmost `constant` inside.
let mut last: Option<&str> = None;
for j in 0..c.named_child_count() {
if let Some(sub) = c.named_child(j)
&& sub.kind() == "constant"
{
last = Some(node_text(sub, s));
}
}
return last.map(str::to_string);
}
}
None
}
/// Walk the body of a class/module and emit Method symbols
/// anchored to `parent`. Tracks visibility state across the
/// walk: a bare `private` / `protected` / `public` statement
/// flips the visibility for subsequent `def`s in this scope,
/// matching Ruby semantics (audit L4 — every method was
/// silently emitted as `is_exported: true` regardless).
fn walk_class_body(&self, n: Node, s: &[u8], symbols: &mut Vec<Symbol>, parent: &str) {
// Current visibility for the next method. `true` = public,
// `false` = non-public (private OR protected — we collapse
// both to "not exported" because dirge's Symbol shape only
// has a binary is_exported flag).
let mut visibility_public = true;
// The body is `body_statement`; iterate its named children
// and pick up `method` and `singleton_method` (class methods).
for i in 0..n.named_child_count() {
let Some(c) = n.named_child(i) else { continue };
// Detect visibility-toggle statements before per-kind
// handling. tree-sitter-ruby parses a bare `private` as
// an `identifier` node; `private :foo` and `private def`
// are `call` nodes — we ignore those (they only toggle
// the named target, not subsequent defs).
if c.kind() == "identifier" {
let txt = node_text(c, s);
match txt {
"public" => {
visibility_public = true;
continue;
}
"private" | "protected" => {
visibility_public = false;
continue;
}
_ => {}
}
}
match c.kind() {
"method" => {
if let Some(name) = self.method_name(c, s) {
symbols.push(Symbol {
kind: SymbolKind::Method,
name,
range: ByteRange::from(c),
signature: self.signature(c, s),
is_exported: visibility_public,
parent_class: Some(parent.to_string()),
});
}
}
"singleton_method" => {
// `def self.foo` — method name is in the
// `identifier` child after the `self` / receiver.
let mut name: Option<String> = None;
for j in 0..c.named_child_count() {
if let Some(sub) = c.named_child(j)
&& sub.kind() == "identifier"
{
name = Some(node_text(sub, s).to_string());
break;
}
}
if let Some(name) = name {
symbols.push(Symbol {
kind: SymbolKind::Method,
name,
range: ByteRange::from(c),
signature: self.signature(c, s),
// Class methods (`def self.foo`) don't
// participate in instance-method
// private/protected visibility — they're
// always public unless declared via
// `private_class_method`. Keep as public.
is_exported: true,
parent_class: Some(parent.to_string()),
});
}
}
// Nested class / module — recurse.
"class" | "module" => self.walk_top(c, s, symbols, Some(parent.to_string())),
// `class << self ... end` — singleton class block.
// Methods defined inside are class methods on the
// enclosing class. tree-sitter exposes the block as
// `singleton_class`; walk its body and emit each
// `method` as a Method on `parent`. Without this,
// the entire `class << self` block of methods was
// invisible to list_symbols.
"singleton_class" => {
for j in 0..c.named_child_count() {
if let Some(body) = c.named_child(j)
&& body.kind() == "body_statement"
{
self.walk_class_body(body, s, symbols, parent);
}
}
}
_ => {}
}
}
}
/// Top-level walker, also used for nested class/module
/// recursion. When `inside_parent` is `Some`, top-level `def`s
/// inside a class body still become Methods.
fn walk_top(
&self,
n: Node,
s: &[u8],
symbols: &mut Vec<Symbol>,
inside_parent: Option<String>,
) {
match n.kind() {
"class" => {
let Some(name) = self.class_or_module_name(n, s) else {
return;
};
symbols.push(Symbol {
kind: SymbolKind::Class,
name: name.clone(),
range: ByteRange::from(n),
signature: self.signature(n, s),
is_exported: true,
parent_class: inside_parent.clone(),
});
// Body statement is the children container.
for i in 0..n.named_child_count() {
if let Some(body) = n.named_child(i)
&& body.kind() == "body_statement"
{
self.walk_class_body(body, s, symbols, &name);
}
}
}
"module" => {
let Some(name) = self.class_or_module_name(n, s) else {
return;
};
symbols.push(Symbol {
kind: SymbolKind::Interface,
name: name.clone(),
range: ByteRange::from(n),
signature: self.signature(n, s),
is_exported: true,
parent_class: inside_parent.clone(),
});
for i in 0..n.named_child_count() {
if let Some(body) = n.named_child(i)
&& body.kind() == "body_statement"
{
self.walk_class_body(body, s, symbols, &name);
}
}
}
"method" => {
if let Some(name) = self.method_name(n, s) {
symbols.push(Symbol {
kind: SymbolKind::Function,
name,
range: ByteRange::from(n),
signature: self.signature(n, s),
is_exported: true,
parent_class: None,
});
}
}
_ => {}
}
}
/// `require '<name>'` and `require_relative '<name>'` — surfaced
/// as imports.
fn maybe_import(&self, n: Node, s: &[u8], imports: &mut Vec<Import>) {
if n.kind() != "call" {
return;
}
// First child is the called identifier.
let Some(id) = n.named_child(0).filter(|c| c.kind() == "identifier") else {
return;
};
let id_text = node_text(id, s);
if !matches!(id_text, "require" | "require_relative" | "load") {
return;
}
// Find the argument_list → string → string_content.
for i in 0..n.named_child_count() {
let Some(c) = n.named_child(i) else { continue };
if c.kind() != "argument_list" {
continue;
}
for j in 0..c.named_child_count() {
if let Some(arg) = c.named_child(j)
&& arg.kind() == "string"
{
let raw = node_text(arg, s);
let path = raw
.trim_matches(|c: char| c == '"' || c == '\'')
.to_string();
imports.push(Import {
names: vec![path.clone()],
source: path,
kind: ImportKind::Module,
});
}
}
}
}
}
impl LanguageAdapter for RubyAdapter {
fn extensions(&self) -> &[&str] {
&[".rb", ".rake", ".gemspec"]
}
fn extract(&self, file_path: &Path, source: &str) -> Result<ExtractedFile, String> {
let lang: tree_sitter::Language = tree_sitter_ruby::LANGUAGE.into();
let mut parser = Parser::new();
parser
.set_language(&lang)
.map_err(|e| format!("Failed to set language: {e}"))?;
let tree = parser.parse(source, None).ok_or("Failed to parse source")?;
let root = tree.root_node();
let bytes = source.as_bytes();
let mut symbols = Vec::new();
let mut imports = Vec::new();
let mut warnings = Vec::new();
if root.has_error() {
warnings.push("tree-sitter reported syntax errors".to_string());
}
for i in 0..root.named_child_count() {
let Some(c) = root.named_child(i) else {
continue;
};
self.walk_top(c, bytes, &mut symbols, None);
self.maybe_import(c, bytes, &mut imports);
}
let exports: Vec<String> = symbols
.iter()
.filter(|s| s.is_exported)
.map(|s| s.name.clone())
.collect();
Ok(ExtractedFile {
file_path: file_path.to_path_buf(),
symbols,
imports,
exports,
warnings,
mtime: std::time::SystemTime::now(),
size: 0,
head_hash: 0,
})
}
fn find_callees_in_range(
&self,
source: &str,
_file_path: &Path,
range: ByteRange,
) -> Result<Vec<String>, String> {
let lang: tree_sitter::Language = tree_sitter_ruby::LANGUAGE.into();
// `(call method: (identifier) @callee)` catches `obj.foo`
// and bare `foo()`. Identifiers in non-call position are
// skipped (otherwise we'd flag every variable reference).
let query_str = "(call method: (identifier) @callee)";
crate::semantic::common::run_callee_query(&lang, query_str, source, range)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn pb(n: &str) -> std::path::PathBuf {
std::path::PathBuf::from(n)
}
#[test]
fn extracts_class_with_methods() {
let src = "class User\n def initialize(name)\n @name = name\n end\n def greet\n puts @name\n end\nend\n";
let f = RubyAdapter.extract(&pb("a.rb"), src).unwrap();
let cls = f.symbols.iter().find(|s| s.name == "User").unwrap();
assert!(matches!(cls.kind, SymbolKind::Class));
let init = f.symbols.iter().find(|s| s.name == "initialize").unwrap();
assert!(matches!(init.kind, SymbolKind::Method));
assert_eq!(init.parent_class.as_deref(), Some("User"));
assert!(f.symbols.iter().any(|s| s.name == "greet"));
}
#[test]
fn extracts_module_as_interface() {
let src = "module Greetable\n def hello; end\nend\n";
let f = RubyAdapter.extract(&pb("a.rb"), src).unwrap();
let m = f.symbols.iter().find(|s| s.name == "Greetable").unwrap();
assert!(matches!(m.kind, SymbolKind::Interface));
let hello = f.symbols.iter().find(|s| s.name == "hello").unwrap();
assert_eq!(hello.parent_class.as_deref(), Some("Greetable"));
}
#[test]
fn extracts_top_level_def_as_function() {
let src = "def helper(x)\n x + 1\nend\n";
let f = RubyAdapter.extract(&pb("a.rb"), src).unwrap();
let h = f.symbols.iter().find(|s| s.name == "helper").unwrap();
assert!(matches!(h.kind, SymbolKind::Function));
assert!(h.parent_class.is_none());
}
#[test]
fn extracts_require_imports() {
let src = "require 'json'\nrequire_relative './helper'\n";
let f = RubyAdapter.extract(&pb("a.rb"), src).unwrap();
assert!(f.imports.iter().any(|i| i.source == "json"));
assert!(f.imports.iter().any(|i| i.source == "./helper"));
}
#[test]
fn find_callees_for_method_body() {
let src = "class A\n def run\n puts(\"x\")\n helper()\n end\nend\n";
let f = RubyAdapter.extract(&pb("a.rb"), src).unwrap();
let run = f.symbols.iter().find(|s| s.name == "run").unwrap();
let callees = RubyAdapter
.find_callees_in_range(src, &pb("a.rb"), run.range)
.unwrap();
assert!(callees.contains(&"puts".to_string()));
assert!(callees.contains(&"helper".to_string()));
}
/// `class << self` singleton class blocks define class methods.
/// Previously the entire block's methods were invisible.
#[test]
fn extracts_singleton_class_methods_as_class_methods() {
let src = "class A\n class << self\n def factory; new; end\n def banner; puts \"hi\"; end\n end\nend\n";
let f = RubyAdapter.extract(&pb("a.rb"), src).unwrap();
let factory = f.symbols.iter().find(|s| s.name == "factory").unwrap();
let banner = f.symbols.iter().find(|s| s.name == "banner").unwrap();
assert!(matches!(factory.kind, SymbolKind::Method));
assert_eq!(factory.parent_class.as_deref(), Some("A"));
assert_eq!(banner.parent_class.as_deref(), Some("A"));
}
}