ruby.rs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. //! Ruby extraction — a faithful Rust port of `TreeSitterExtractor`'s Ruby
  2. //! paths (src/extraction/tree-sitter.ts) plus languages/ruby.ts.
  3. //!
  4. //! Same porting contract as the other walkers: behavior parity, bug-for-bug.
  5. //! The authoritative quirk list is docs/design/ruby-kernel-port-checklist.md —
  6. //! including the load-bearing oddities this file preserves on purpose:
  7. //! `importTypes: ['call']` funnels EVERY non-body call into extractImport (so
  8. //! class-body DSL like `attr_accessor`, `has_many`, `define_method` and its
  9. //! whole block emit NOTHING), hook-handled modules MULTIPLY-CAPTURE fn-ref
  10. //! containers (each nesting level re-scans its subtree after popping), the
  11. //! sibling-scan visibility trio (bare `private` is invisible; `private :sym` /
  12. //! `private def x` poison every later sibling def; the def inside
  13. //! `private def` stays public), brace-block bodies (`block_body`) are
  14. //! invisible to bare-call extraction while `do…end` bodies emit, and the
  15. //! value-ref DFS visits statements in REVERSE source order. Positions in
  16. //! UTF-16 code units. Files with parse errors defer to wasm (~0% incidence).
  17. use crate::buffers::{
  18. build_meta, edge_kind_index, node_kind_index, Arena, BoolFlags, EdgeRow, EmitOut, NodeRow,
  19. RefRow, StrRef, Tables, FLAG_IS_EXPORTED, FUNCTION_REF_CODE, NONE, NONE_STR,
  20. REF_FLAG_FILE_PATH,
  21. };
  22. use crate::docstring::preceding_docstring;
  23. use crate::ids;
  24. use crate::textutil as util;
  25. use regex::Regex;
  26. use std::collections::{HashMap, HashSet};
  27. use std::sync::OnceLock;
  28. use tree_sitter::{Node, Parser};
  29. const MAX_VALUE_REF_NODES: usize = 20_000;
  30. /// NAME_STOPLIST (function-ref.ts).
  31. fn is_stoplisted(name: &str) -> bool {
  32. matches!(
  33. name,
  34. "this" | "self" | "super" | "null" | "nil" | "true" | "false" | "undefined" | "new"
  35. | "NULL" | "nullptr" | "None"
  36. )
  37. }
  38. /// isRubyHookCall (function-ref.ts:282-286).
  39. fn is_ruby_hook_call(name: &str) -> bool {
  40. static RE: OnceLock<Regex> = OnceLock::new();
  41. let re = RE.get_or_init(|| Regex::new(r"^(skip_)?(before|after|around)_[a-z_]+$").unwrap());
  42. re.is_match(name)
  43. || matches!(name, "validate" | "set_callback" | "helper_method" | "rescue_from")
  44. }
  45. /// The hook-DSL symbol shape (`/^[A-Za-z_][A-Za-z0-9_?!]*$/`).
  46. fn ruby_sym_re() -> &'static Regex {
  47. static RE: OnceLock<Regex> = OnceLock::new();
  48. RE.get_or_init(|| Regex::new(r"^[A-Za-z_][A-Za-z0-9_?!]*$").unwrap())
  49. }
  50. /// Node `path.posix.normalize` for the require_relative path join
  51. /// (emitRubyRequireRefs): resolve `.`/`..` lexically, collapse `//`, keep
  52. /// unresolvable leading `..`s, preserve a trailing slash.
  53. fn posix_normalize(p: &str) -> String {
  54. let is_abs = p.starts_with('/');
  55. let had_trailing = p.len() > 1 && p.ends_with('/');
  56. let mut out: Vec<&str> = Vec::new();
  57. for seg in p.split('/') {
  58. match seg {
  59. "" | "." => {}
  60. ".." => {
  61. if matches!(out.last(), Some(&last) if last != "..") {
  62. out.pop();
  63. } else if !is_abs {
  64. out.push("..");
  65. }
  66. }
  67. s => out.push(s),
  68. }
  69. }
  70. let mut joined = out.join("/");
  71. if is_abs {
  72. joined = format!("/{joined}");
  73. }
  74. if joined.is_empty() || joined == "/" {
  75. return if is_abs { "/".to_string() } else { ".".to_string() };
  76. }
  77. if had_trailing {
  78. joined.push('/');
  79. }
  80. joined
  81. }
  82. struct Scope {
  83. row: u32,
  84. kind: &'static str,
  85. name: String,
  86. }
  87. #[derive(Default)]
  88. struct Extra {
  89. docstring: Option<String>,
  90. signature: Option<String>,
  91. visibility: Option<u8>,
  92. }
  93. struct ValueScope<'t> {
  94. row: u32,
  95. node: Node<'t>,
  96. name: String,
  97. }
  98. struct Cand {
  99. from: u32,
  100. name: String,
  101. line: u32,
  102. column_byte: usize,
  103. row: usize,
  104. }
  105. pub struct Walker<'t> {
  106. src: &'t str,
  107. file_path: &'t str,
  108. line_starts: Vec<usize>,
  109. arena: Arena,
  110. tables: Tables,
  111. stack: Vec<Scope>,
  112. node_ids: Vec<String>,
  113. defined_fn_names: HashSet<String>,
  114. imported_names: HashSet<String>,
  115. fn_ref_cands: Vec<Cand>,
  116. fs_values: HashMap<String, u32>,
  117. fs_value_counts: HashMap<String, u32>,
  118. value_scopes: Vec<ValueScope<'t>>,
  119. }
  120. pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
  121. let grammar = crate::langs::grammar_for("ruby").ok_or("no ruby grammar")?;
  122. let t0 = std::time::Instant::now();
  123. let mut parser = Parser::new();
  124. parser
  125. .set_language(&grammar)
  126. .map_err(|e| format!("set_language(ruby) failed: {e}"))?;
  127. let tree = parser
  128. .parse(source, None)
  129. .ok_or_else(|| "parser returned null tree".to_string())?;
  130. if tree.root_node().has_error() {
  131. return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
  132. }
  133. let mut w = Walker {
  134. src: source,
  135. file_path,
  136. line_starts: util::line_starts(source),
  137. arena: Arena::default(),
  138. tables: Tables::default(),
  139. stack: Vec::new(),
  140. node_ids: Vec::new(),
  141. defined_fn_names: HashSet::new(),
  142. imported_names: HashSet::new(),
  143. fn_ref_cands: Vec::new(),
  144. fs_values: HashMap::new(),
  145. fs_value_counts: HashMap::new(),
  146. value_scopes: Vec::new(),
  147. };
  148. let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
  149. let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
  150. let mut flags = BoolFlags::default();
  151. flags.set(FLAG_IS_EXPORTED, false);
  152. let file_id = w.arena.put(&ids::file_node_id(file_path));
  153. let name_ref = w.arena.put(base_name);
  154. let qn_ref = w.arena.put(file_path);
  155. w.tables.push_node(&NodeRow {
  156. kind: node_kind_index("file").unwrap(),
  157. visibility: 0,
  158. flags,
  159. start_line: 1,
  160. end_line: line_count,
  161. start_column: 0,
  162. end_column: 0,
  163. name: name_ref,
  164. qualified_name: qn_ref,
  165. id: file_id,
  166. docstring: NONE_STR,
  167. signature: NONE_STR,
  168. decorators: NONE_STR,
  169. type_parameters: NONE_STR,
  170. return_type: NONE_STR,
  171. extra_json: NONE_STR,
  172. });
  173. w.node_ids.push(ids::file_node_id(file_path));
  174. w.stack.push(Scope { row: 0, kind: "file", name: base_name.to_string() });
  175. w.visit_node(tree.root_node());
  176. w.flush_fn_ref_candidates();
  177. w.flush_value_refs(tree.root_node());
  178. w.stack.pop();
  179. let duration_ms = t0.elapsed().as_secs_f64() * 1000.0;
  180. let meta = build_meta(&w.tables, w.arena.len(), NONE_STR, duration_ms);
  181. Ok(EmitOut {
  182. meta,
  183. nodes: w.tables.nodes,
  184. edges: w.tables.edges,
  185. refs: w.tables.refs,
  186. arena: w.arena.into_vec(),
  187. })
  188. }
  189. impl<'t> Walker<'t> {
  190. fn text(&self, node: Node) -> &'t str {
  191. &self.src[node.byte_range()]
  192. }
  193. fn line_of(&self, node: Node) -> u32 {
  194. node.start_position().row as u32 + 1
  195. }
  196. fn col_of(&self, node: Node) -> u32 {
  197. util::col16(self.src, &self.line_starts, node.start_position().row, node.start_byte())
  198. }
  199. fn end_col_of(&self, node: Node) -> u32 {
  200. util::col16(self.src, &self.line_starts, node.end_position().row, node.end_byte())
  201. }
  202. fn top_row(&self) -> u32 {
  203. self.stack.last().map(|s| s.row).unwrap_or(0)
  204. }
  205. fn inside_class_like(&self) -> bool {
  206. self.stack
  207. .last()
  208. .map(|s| matches!(s.kind, "class" | "struct" | "interface" | "trait" | "enum" | "module"))
  209. .unwrap_or(false)
  210. }
  211. fn push_ref(&mut self, from_row: u32, name: &str, kind_code: u8, line: u32, column: u32) {
  212. let name_ref = self.arena.put(name);
  213. self.tables.push_ref(&RefRow {
  214. from_idx: from_row,
  215. kind: kind_code,
  216. line,
  217. column,
  218. reference_name: name_ref,
  219. candidates: NONE_STR,
  220. from_id_str: NONE_STR,
  221. });
  222. if kind_code == edge_kind_index("imports").unwrap() {
  223. if util::simple_name().is_match(name) {
  224. self.imported_names.insert(name.to_string());
  225. } else if let Some(c) = util::qualified_import().captures(name) {
  226. self.imported_names.insert(c[1].to_string());
  227. }
  228. }
  229. }
  230. fn push_ref_at(&mut self, from_row: u32, name: &str, kind_code: u8, node: Node) {
  231. self.push_ref(from_row, name, kind_code, self.line_of(node), self.col_of(node));
  232. }
  233. // --- createNode ------------------------------------------------------------
  234. fn create_node(&mut self, kind: &'static str, name: &str, node: Node<'t>, extra: Extra) -> Option<u32> {
  235. if name.is_empty() {
  236. return None;
  237. }
  238. let start_line = self.line_of(node);
  239. let id = ids::node_id(self.file_path, kind, name, start_line);
  240. let end_line = node.end_position().row as u32 + 1; // no resolveBody for ruby
  241. let qualified = {
  242. let mut parts: Vec<&str> = Vec::new();
  243. for s in &self.stack {
  244. if s.kind != "file" {
  245. parts.push(&s.name);
  246. }
  247. }
  248. let mut qn = parts.join("::");
  249. if !qn.is_empty() {
  250. qn.push_str("::");
  251. }
  252. qn.push_str(name);
  253. qn
  254. };
  255. let name_ref = self.arena.put(name);
  256. let qn_ref = self.arena.put(&qualified);
  257. let id_ref = self.arena.put(&id);
  258. let doc_ref = opt_str(&mut self.arena, extra.docstring.as_deref());
  259. let sig_ref = opt_str(&mut self.arena, extra.signature.as_deref());
  260. let row = self.tables.push_node(&NodeRow {
  261. kind: node_kind_index(kind).unwrap(),
  262. visibility: extra.visibility.unwrap_or(0),
  263. flags: BoolFlags::default(), // no isExported/isAsync/isStatic hooks
  264. start_line,
  265. end_line,
  266. start_column: self.col_of(node),
  267. end_column: self.end_col_of(node),
  268. name: name_ref,
  269. qualified_name: qn_ref,
  270. id: id_ref,
  271. docstring: doc_ref,
  272. signature: sig_ref,
  273. decorators: NONE_STR, // ruby has no decorator node kinds — always a no-op
  274. type_parameters: NONE_STR,
  275. return_type: NONE_STR,
  276. extra_json: NONE_STR,
  277. });
  278. self.node_ids.push(id);
  279. let parent_row = self.top_row();
  280. self.tables.push_edge(&EdgeRow {
  281. source_idx: parent_row,
  282. target_idx: row,
  283. kind: edge_kind_index("contains").unwrap(),
  284. provenance: 0,
  285. line: NONE,
  286. column: NONE,
  287. metadata_json: NONE_STR,
  288. source_id_str: NONE_STR,
  289. target_id_str: NONE_STR,
  290. });
  291. if kind == "function" || kind == "method" {
  292. self.defined_fn_names.insert(name.to_string());
  293. }
  294. // captureValueRefScope
  295. let target_kind_ok = kind == "constant" || kind == "variable";
  296. if target_kind_ok
  297. && util::utf16_len(name) >= 3
  298. && util::has_upper_or_underscore().is_match(name)
  299. {
  300. let parent_ok = self
  301. .stack
  302. .last()
  303. .map(|s| matches!(s.kind, "file" | "class" | "module" | "struct" | "enum"))
  304. .unwrap_or(false);
  305. if parent_ok {
  306. self.fs_values.insert(name.to_string(), row);
  307. *self.fs_value_counts.entry(name.to_string()).or_insert(0) += 1;
  308. }
  309. }
  310. if matches!(kind, "function" | "method" | "constant" | "variable") {
  311. self.value_scopes.push(ValueScope { row, node, name: name.to_string() });
  312. }
  313. Some(row)
  314. }
  315. fn extract_name(&self, node: Node) -> String {
  316. if let Some(name_node) = node.child_by_field_name("name") {
  317. return self.text(name_node).to_string();
  318. }
  319. for i in 0..node.named_child_count() {
  320. if let Some(c) = node.named_child(i) {
  321. if matches!(c.kind(), "identifier" | "type_identifier" | "simple_identifier" | "constant") {
  322. return self.text(c).to_string();
  323. }
  324. }
  325. }
  326. "<anonymous>".to_string()
  327. }
  328. /// rubyExtractor.getVisibility — walk the previousNamedSibling chain
  329. /// (unbounded, through non-matching siblings); the first `call` sibling
  330. /// whose `method` text is private/protected/public decides; else public.
  331. /// Bug-for-bug: a BARE `private` line parses as identifier (invisible),
  332. /// and a matching call poisons every later sibling def regardless of
  333. /// ruby's actual arg-scoped semantics.
  334. fn visibility_of(&self, node: Node) -> u8 {
  335. let mut sibling = node.prev_named_sibling();
  336. while let Some(s) = sibling {
  337. if s.kind() == "call" {
  338. if let Some(method) = s.child_by_field_name("method") {
  339. match self.text(method) {
  340. "private" => return 2,
  341. "protected" => return 3,
  342. "public" => return 1,
  343. _ => {}
  344. }
  345. }
  346. }
  347. sibling = s.prev_named_sibling();
  348. }
  349. 1 // public
  350. }
  351. // --- the visitNode hook (languages/ruby.ts:19-76) ---------------------------
  352. /// Returns true when the hook handled the node (mixin call or module).
  353. /// The DISPATCHER then runs scan_fn_ref_subtree on the handled subtree —
  354. /// the source of the module multiply-capture quirk (scan runs with the
  355. /// module already POPPED, so candidates re-attribute to the outer scope).
  356. fn try_visit_hook(&mut self, node: Node<'t>) -> bool {
  357. let kind = node.kind();
  358. if kind == "call" && node.child_by_field_name("receiver").is_none() {
  359. if let Some(method) = node.child_by_field_name("method") {
  360. if matches!(self.text(method), "include" | "extend" | "prepend") {
  361. let args = node.child_by_field_name("arguments").or_else(|| {
  362. (0..node.named_child_count())
  363. .filter_map(|i| node.named_child(i))
  364. .find(|c| c.kind() == "argument_list")
  365. });
  366. // (nodeStack is never empty — the file node is pushed.)
  367. if let Some(args) = args {
  368. let parent = self.top_row();
  369. let implements = edge_kind_index("implements").unwrap();
  370. let line = self.line_of(node);
  371. let col = self.col_of(node);
  372. for i in 0..args.named_child_count() {
  373. let Some(arg) = args.named_child(i) else { continue };
  374. // `Mod` is constant, `Foo::Bar` is scope_resolution;
  375. // `extend self` / dynamic args are skipped. Unlike
  376. // every other extraction ref, the hook sets
  377. // `filePath: ctx.filePath` — flagged on the wire.
  378. if matches!(arg.kind(), "constant" | "scope_resolution") {
  379. let name_ref = self.arena.put(self.text(arg));
  380. self.tables.push_ref_flagged(
  381. &RefRow {
  382. from_idx: parent,
  383. kind: implements,
  384. line,
  385. column: col,
  386. reference_name: name_ref,
  387. candidates: NONE_STR,
  388. from_id_str: NONE_STR,
  389. },
  390. REF_FLAG_FILE_PATH,
  391. );
  392. }
  393. }
  394. return true;
  395. }
  396. // no args node → hook declines (falls to extractImport → nothing)
  397. }
  398. }
  399. }
  400. if kind != "module" {
  401. return false;
  402. }
  403. let Some(name_node) = node.child_by_field_name("name") else { return false };
  404. // `module A::B` keeps the scope_resolution text verbatim as the name.
  405. let name = self.text(name_node).to_string();
  406. let Some(row) = self.create_node("module", &name, node, Extra::default()) else {
  407. return false;
  408. };
  409. self.stack.push(Scope { row, kind: "module", name });
  410. if let Some(body) = node.child_by_field_name("body") {
  411. for i in 0..body.named_child_count() {
  412. if let Some(c) = body.named_child(i) {
  413. self.visit_node(c);
  414. }
  415. }
  416. }
  417. self.stack.pop();
  418. true
  419. }
  420. // --- the dispatcher (visitNode, Ruby-relevant branches) ----------------------
  421. fn visit_node(&mut self, node: Node<'t>) {
  422. // Language hook FIRST (tree-sitter.ts:943) — a handled subtree is
  423. // scanned for fn-ref candidates and never reaches the ladder (or the
  424. // maybeCaptureFnRefs call below).
  425. if self.try_visit_hook(node) {
  426. self.scan_fn_ref_subtree(node, 0);
  427. return;
  428. }
  429. let kind = node.kind();
  430. let mut skip_children = false;
  431. self.maybe_capture_fn_refs(node);
  432. if kind == "method" {
  433. // functionTypes ∩ methodTypes: inside class-like (module counts!)
  434. // ⇒ method, else function.
  435. if self.inside_class_like() {
  436. self.extract_method(node);
  437. } else {
  438. self.extract_function(node);
  439. }
  440. skip_children = true;
  441. } else if kind == "class" {
  442. self.extract_class(node);
  443. skip_children = true;
  444. } else if kind == "singleton_method" {
  445. // methodTypes-only: extractMethod's 1747 gate bounces a top-level
  446. // `def self.x` to extractFunction — a plain function named x (the
  447. // receiver object is ignored everywhere).
  448. if self.inside_class_like() {
  449. self.extract_method(node);
  450. } else {
  451. self.extract_function(node);
  452. }
  453. skip_children = true;
  454. } else if kind == "assignment"
  455. && (!self.inside_class_like() || self.is_class_scope_constant_assignment(node))
  456. {
  457. // File scope: identifier AND constant LHS extract; class/module
  458. // scope: ONLY constant LHS (isClassScopeConstantAssignment). The
  459. // RHS is never walked (no instantiates/calls from initializers).
  460. self.extract_variable(node);
  461. self.scan_fn_ref_subtree(node, 0);
  462. skip_children = true;
  463. } else if kind == "call" {
  464. // importTypes:['call'] — EVERY non-body call funnels here. Only
  465. // require/require_relative-with-string emit; everything else is
  466. // invisible (children still visited: no skipChildren).
  467. self.extract_import(node);
  468. }
  469. // `singleton_class` (`class << self`), `alias`, top-level `if`/`begin`,
  470. // `uninterpreted`, operator_assignment: no branch — recursed.
  471. if !skip_children {
  472. for i in 0..node.named_child_count() {
  473. if let Some(c) = node.named_child(i) {
  474. self.visit_node(c);
  475. }
  476. }
  477. }
  478. }
  479. /// isClassScopeConstantAssignment (tree-sitter.ts:1508).
  480. fn is_class_scope_constant_assignment(&self, node: Node) -> bool {
  481. let left = node.child_by_field_name("left").or_else(|| node.named_child(0));
  482. left.map(|l| l.kind() == "constant").unwrap_or(false)
  483. }
  484. // --- visitFunctionBody ------------------------------------------------------
  485. fn visit_function_body(&mut self, body: Node<'t>) {
  486. self.visit_for_calls_and_structure(body);
  487. }
  488. fn visit_for_calls_and_structure(&mut self, node: Node<'t>) {
  489. let kind = node.kind();
  490. self.maybe_capture_fn_refs(node);
  491. if kind == "call" {
  492. self.extract_call(node);
  493. } else if let Some(bare) = self.bare_call_name(node) {
  494. // extractBareCall: statement-level identifiers in BLOCK_PARENTS
  495. // bodies (`do…end` = body_statement; brace blocks are block_body —
  496. // NOT in the set, so `5.times { beep }` emits nothing for beep).
  497. let name = bare.to_string();
  498. let from = self.top_row();
  499. self.push_ref_at(from, &name, edge_kind_index("calls").unwrap(), node);
  500. }
  501. // (No INSTANTIATION_KINDS for ruby — `.new` is handled in extract_call;
  502. // extractStaticMemberRef is gated off; no type annotations.)
  503. // Nested NAMED defs become their own function nodes; classes extract.
  504. // A `module` inside a body mints NOTHING (the visitNode hook does not
  505. // run here) — its children just recurse. Same for singleton_method
  506. // (functionTypes-only check): unmatched, recursed.
  507. if kind == "method" {
  508. let name = self.extract_name(node);
  509. if name != "<anonymous>" {
  510. self.extract_function(node);
  511. return;
  512. }
  513. }
  514. if kind == "class" {
  515. self.extract_class(node);
  516. return;
  517. }
  518. for i in 0..node.named_child_count() {
  519. if let Some(c) = node.named_child(i) {
  520. self.visit_for_calls_and_structure(c);
  521. }
  522. }
  523. }
  524. /// rubyExtractor.extractBareCall (languages/ruby.ts:77-105).
  525. fn bare_call_name(&self, node: Node) -> Option<&'t str> {
  526. if node.kind() != "identifier" {
  527. return None;
  528. }
  529. let parent = node.parent()?;
  530. if !matches!(
  531. parent.kind(),
  532. "body_statement" | "then" | "else" | "do" | "begin" | "rescue" | "ensure" | "when"
  533. ) {
  534. return None;
  535. }
  536. let name = self.text(node);
  537. if matches!(
  538. name,
  539. "true" | "false" | "nil" | "self" | "super" | "__FILE__" | "__LINE__" | "__dir__"
  540. ) {
  541. return None;
  542. }
  543. // charCodeAt(0) in [65,90] — ASCII uppercase only (constants).
  544. let first = name.as_bytes().first().copied().unwrap_or(0);
  545. if (65..=90).contains(&first) {
  546. return None;
  547. }
  548. Some(name)
  549. }
  550. // --- extractors --------------------------------------------------------------
  551. fn extract_function(&mut self, node: Node<'t>) {
  552. let name = self.extract_name(node);
  553. if name == "<anonymous>" {
  554. if let Some(body) = node.child_by_field_name("body") {
  555. self.visit_function_body(body);
  556. }
  557. return;
  558. }
  559. let extra = Extra {
  560. docstring: preceding_docstring(node, self.src),
  561. signature: None, // no getSignature hook
  562. visibility: Some(self.visibility_of(node)),
  563. };
  564. let Some(row) = self.create_node("function", &name, node, extra) else { return };
  565. // (ruby ∉ TYPE_ANNOTATION_LANGUAGES; decorators are a structural no-op)
  566. self.stack.push(Scope { row, kind: "function", name });
  567. if let Some(body) = node.child_by_field_name("body") {
  568. self.visit_function_body(body);
  569. }
  570. self.stack.pop();
  571. // `parameters` are NEVER walked — a call in a default value emits nothing.
  572. }
  573. fn extract_method(&mut self, node: Node<'t>) {
  574. let name = self.extract_name(node);
  575. let extra = Extra {
  576. docstring: preceding_docstring(node, self.src),
  577. signature: None,
  578. visibility: Some(self.visibility_of(node)),
  579. };
  580. let Some(row) = self.create_node("method", &name, node, extra) else { return };
  581. self.stack.push(Scope { row, kind: "method", name });
  582. if let Some(body) = node.child_by_field_name("body") {
  583. self.visit_function_body(body);
  584. }
  585. self.stack.pop();
  586. }
  587. fn extract_class(&mut self, node: Node<'t>) {
  588. let name = self.extract_name(node);
  589. let extra = Extra {
  590. docstring: preceding_docstring(node, self.src),
  591. signature: None,
  592. visibility: Some(self.visibility_of(node)),
  593. };
  594. let Some(row) = self.create_node("class", &name, node, extra) else { return };
  595. // extractInheritance: the `superclass` clause — ONE extends ref, FULL
  596. // text (scope_resolution / even `Struct.new(:a)` expressions verbatim),
  597. // positioned at the type child.
  598. let extends_kind = edge_kind_index("extends").unwrap();
  599. for i in 0..node.named_child_count() {
  600. let Some(child) = node.named_child(i) else { continue };
  601. if child.kind() == "superclass" {
  602. if let Some(target) = child.named_child(0) {
  603. let tname = self.text(target).to_string();
  604. self.push_ref_at(row, &tname, extends_kind, target);
  605. }
  606. }
  607. }
  608. self.stack.push(Scope { row, kind: "class", name });
  609. // Bodiless `class X; end` has no body field → the class node itself
  610. // is walked (name/superclass children revisit harmlessly).
  611. let body = node.child_by_field_name("body").unwrap_or(node);
  612. for i in 0..body.named_child_count() {
  613. if let Some(c) = body.named_child(i) {
  614. self.visit_node(c);
  615. }
  616. }
  617. self.stack.pop();
  618. }
  619. /// extractVariable — the python/ruby assignment branch (2709-2727):
  620. /// identifier or constant LHS mints a `variable` node (no isConst hook —
  621. /// `MAX = 3` is kind variable) at the ASSIGNMENT node's position, with the
  622. /// `= <first 100 utf16 units>` initializer signature.
  623. fn extract_variable(&mut self, node: Node<'t>) {
  624. let docstring = preceding_docstring(node, self.src);
  625. let left = node.child_by_field_name("left").or_else(|| node.named_child(0));
  626. let right = node.child_by_field_name("right").or_else(|| node.named_child(1));
  627. let Some(left) = left else { return };
  628. if !matches!(left.kind(), "identifier" | "constant") {
  629. return;
  630. }
  631. let name = self.text(left).to_string();
  632. let signature = right.map(|r| util::init_signature(self.text(r)));
  633. self.create_node("variable", &name, node, Extra { docstring, signature, visibility: None });
  634. }
  635. /// extractImport — every non-body `call` lands here (importTypes:['call']).
  636. /// Hook (languages/ruby.ts:123): FIRST identifier named child must read
  637. /// require/require_relative (a lowercase receiver is found first and
  638. /// declines; a constant receiver — `Kernel.require "x"` — reaches the
  639. /// method identifier and IS a require); argument_list → string →
  640. /// string_content → moduleName. Then the generic imports ref and
  641. /// emitRubyRequireRefs' path ref.
  642. fn extract_import(&mut self, node: Node<'t>) {
  643. let ident = (0..node.named_child_count())
  644. .filter_map(|i| node.named_child(i))
  645. .find(|c| c.kind() == "identifier");
  646. let Some(ident) = ident else { return };
  647. let mname = self.text(ident);
  648. if mname != "require" && mname != "require_relative" {
  649. return;
  650. }
  651. let arg_list = (0..node.named_child_count())
  652. .filter_map(|i| node.named_child(i))
  653. .find(|c| c.kind() == "argument_list");
  654. let Some(arg_list) = arg_list else { return };
  655. let string = (0..arg_list.named_child_count())
  656. .filter_map(|i| arg_list.named_child(i))
  657. .find(|c| c.kind() == "string");
  658. let Some(string) = string else { return };
  659. let content = (0..string.named_child_count())
  660. .filter_map(|i| string.named_child(i))
  661. .find(|c| c.kind() == "string_content");
  662. let Some(content) = content else { return };
  663. // Interpolated paths take the FIRST string_content only — moduleName
  664. // `interp/` (+ a garbage `interp/.rb` path ref) — deterministic quirk.
  665. let module_name = self.text(content).to_string();
  666. let import_text = self.text(node).trim().to_string();
  667. if module_name.is_empty() {
  668. return;
  669. }
  670. self.create_node(
  671. "import",
  672. &module_name,
  673. node,
  674. Extra { signature: Some(import_text), ..Extra::default() },
  675. );
  676. let parent = self.top_row();
  677. let imports_kind = edge_kind_index("imports").unwrap();
  678. self.push_ref_at(parent, &module_name.clone(), imports_kind, node);
  679. // emitRubyRequireRefs (3532): the file-path ref. Bare gem/stdlib
  680. // requires (no `/`) emit nothing; paths get `.rb` appended.
  681. let req = self.text(content).trim();
  682. if req.is_empty() {
  683. return;
  684. }
  685. let ref_path = if mname == "require_relative" {
  686. let dir = match self.file_path.rfind('/') {
  687. Some(i) => &self.file_path[..i],
  688. None => "",
  689. };
  690. let joined = if dir.is_empty() { req.to_string() } else { format!("{dir}/{req}") };
  691. posix_normalize(&joined)
  692. } else {
  693. req.to_string()
  694. };
  695. if !ref_path.contains('/') {
  696. return;
  697. }
  698. let ref_path =
  699. if ref_path.ends_with(".rb") { ref_path } else { format!("{ref_path}.rb") };
  700. self.push_ref_at(parent, &ref_path, imports_kind, node);
  701. }
  702. /// extractCall — the bespoke ruby branch (tree-sitter.ts:3905-3960),
  703. /// reached only from the body walker.
  704. fn extract_call(&mut self, node: Node<'t>) {
  705. if self.stack.is_empty() {
  706. return;
  707. }
  708. let caller = self.top_row();
  709. let method_name = match node.child_by_field_name("method") {
  710. Some(m) => self.text(m),
  711. None => "",
  712. };
  713. if method_name.is_empty() {
  714. return; // operator/element-reference call with no method name
  715. }
  716. let line = self.line_of(node);
  717. let col = self.col_of(node);
  718. let calls_kind = edge_kind_index("calls").unwrap();
  719. let Some(receiver) = node.child_by_field_name("receiver") else {
  720. // Bare `foo(...)` — just the method name.
  721. self.push_ref(caller, &method_name.to_string(), calls_kind, line, col);
  722. return;
  723. };
  724. let receiver_name = self.text(receiver);
  725. // `Foo.new` / `NS::Widget.new` → instantiates ref to the LAST `::`
  726. // segment; a non-capitalized receiver falls through to a calls ref
  727. // (`lower.new`).
  728. if method_name == "new" {
  729. let class_name = match receiver_name.rfind("::") {
  730. Some(i) => &receiver_name[i + 2..],
  731. None => receiver_name,
  732. };
  733. if class_name.as_bytes().first().map(|b| b.is_ascii_uppercase()).unwrap_or(false) {
  734. self.push_ref(
  735. caller,
  736. &class_name.to_string(),
  737. edge_kind_index("instantiates").unwrap(),
  738. line,
  739. col,
  740. );
  741. return;
  742. }
  743. }
  744. // SKIP_RECEIVERS by TEXT — ruby's set is {self, super} only. `&.`
  745. // joins with a plain `.`; chains/literals keep raw receiver text.
  746. let skip = matches!(receiver_name, "self" | "super");
  747. let callee = if skip {
  748. method_name.to_string()
  749. } else {
  750. format!("{receiver_name}.{method_name}")
  751. };
  752. self.push_ref(caller, &callee, calls_kind, line, col);
  753. // Capitalized constant receiver (`Klass.static_call`, `RETRY_MAX.times`)
  754. // → an ADDITIONAL references ref at the RECEIVER's position.
  755. // (scope_resolution receivers get none — type ≠ constant.)
  756. if !skip && receiver.kind() == "constant" {
  757. self.push_ref_at(
  758. caller,
  759. &receiver_name.to_string(),
  760. edge_kind_index("references").unwrap(),
  761. receiver,
  762. );
  763. }
  764. }
  765. // --- function-as-value refs (RUBY_SPEC, function-ref.ts:262) -----------------
  766. fn maybe_capture_fn_refs(&mut self, node: Node<'t>) {
  767. enum Mode {
  768. Args,
  769. PairValue,
  770. }
  771. let mode = match node.kind() {
  772. "argument_list" => Mode::Args,
  773. "pair" => Mode::PairValue,
  774. _ => return,
  775. };
  776. if self.stack.is_empty() {
  777. return;
  778. }
  779. let from = self.top_row();
  780. let mut values: Vec<Node> = Vec::new();
  781. match mode {
  782. Mode::Args => {
  783. for i in 0..node.named_child_count() {
  784. if let Some(c) = node.named_child(i) {
  785. values.push(c);
  786. }
  787. }
  788. }
  789. Mode::PairValue => {
  790. let value = node.child_by_field_name("value").or_else(|| {
  791. if node.named_child_count() > 0 {
  792. node.named_child(node.named_child_count() - 1)
  793. } else {
  794. None
  795. }
  796. });
  797. if let Some(v) = value {
  798. values.push(v);
  799. }
  800. }
  801. }
  802. for v in values {
  803. self.normalize_fn_ref_value(v, from, 0);
  804. }
  805. }
  806. /// normalizeValue for RUBY_SPEC: idTypes EMPTY (bare identifiers never
  807. /// qualify); `block_argument` is a transparent layer; specials are the
  808. /// `method(:sym)` call form and hook-DSL `simple_symbol`s.
  809. fn normalize_fn_ref_value(&mut self, v: Node<'t>, from: u32, depth: u32) {
  810. if depth > 4 {
  811. return;
  812. }
  813. match v.kind() {
  814. "block_argument" => {
  815. for i in 0..v.named_child_count() {
  816. if let Some(c) = v.named_child(i) {
  817. self.normalize_fn_ref_value(c, from, depth + 1);
  818. }
  819. }
  820. }
  821. "call" => {
  822. // `method(:target_cb)` — method field literally `method`, one
  823. // simple_symbol argument. Candidate = bare symbol name at the
  824. // SYMBOL node (gated at flush on defined-in-file ∪ imports).
  825. let Some(method) = v.child_by_field_name("method") else { return };
  826. if self.text(method) != "method" {
  827. return;
  828. }
  829. let Some(args) = v.child_by_field_name("arguments") else { return };
  830. if args.named_child_count() != 1 {
  831. return;
  832. }
  833. let Some(sym) = args.named_child(0) else { return };
  834. if sym.kind() != "simple_symbol" {
  835. return;
  836. }
  837. let name = self.text(sym).strip_prefix(':').unwrap_or(self.text(sym));
  838. if !name.is_empty() {
  839. self.push_fn_ref_cand(from, name, sym);
  840. }
  841. }
  842. "simple_symbol" => {
  843. // Hook-DSL symbols (`before_action :authenticate`) → class-
  844. // scoped `this.<sym>` candidates (always flushed).
  845. let Some(call) = ruby_enclosing_call(v) else { return };
  846. let Some(method) = call.child_by_field_name("method") else { return };
  847. if !is_ruby_hook_call(self.text(method)) {
  848. return;
  849. }
  850. let sym = self.text(v).strip_prefix(':').unwrap_or(self.text(v));
  851. if !ruby_sym_re().is_match(sym) {
  852. return;
  853. }
  854. let name = format!("this.{sym}");
  855. self.push_fn_ref_cand(from, &name, v);
  856. }
  857. _ => {}
  858. }
  859. }
  860. fn push_fn_ref_cand(&mut self, from: u32, name: &str, node: Node) {
  861. if name.is_empty() || is_stoplisted(name) {
  862. return;
  863. }
  864. let p = node.start_position();
  865. self.fn_ref_cands.push(Cand {
  866. from,
  867. name: name.to_string(),
  868. line: p.row as u32 + 1,
  869. column_byte: node.start_byte(),
  870. row: p.row,
  871. });
  872. }
  873. fn scan_fn_ref_subtree(&mut self, node: Node<'t>, depth: u32) {
  874. if depth > 12 {
  875. return;
  876. }
  877. // Halts at functionTypes (`method`) + the fixed arrow/lambda list;
  878. // NOT at class/module nodes — the module multiply-capture rides this.
  879. if depth > 0
  880. && matches!(
  881. node.kind(),
  882. "method" | "arrow_function" | "function_expression" | "lambda_literal"
  883. | "lambda_expression"
  884. )
  885. {
  886. return;
  887. }
  888. self.maybe_capture_fn_refs(node);
  889. for i in 0..node.named_child_count() {
  890. if let Some(c) = node.named_child(i) {
  891. self.scan_fn_ref_subtree(c, depth + 1);
  892. }
  893. }
  894. }
  895. fn flush_fn_ref_candidates(&mut self) {
  896. let cands = std::mem::take(&mut self.fn_ref_cands);
  897. if cands.is_empty() || util::is_generated_file(self.file_path) {
  898. return;
  899. }
  900. let mut seen: HashSet<(String, String)> = HashSet::new();
  901. for c in cands {
  902. // `this.`-prefixed candidates always flush (class-scoped resolver);
  903. // bare `method(:x)` names gate on defined-in-file ∪ imports (ruby's
  904. // path-shaped imports match neither name regex, so effectively
  905. // defined-in-file).
  906. if !c.name.starts_with("this.")
  907. && !c.name.contains("::")
  908. && !self.defined_fn_names.contains(&c.name)
  909. && !self.imported_names.contains(&c.name)
  910. {
  911. continue;
  912. }
  913. if !seen.insert((self.node_ids[c.from as usize].clone(), c.name.clone())) {
  914. continue;
  915. }
  916. let column = util::col16(self.src, &self.line_starts, c.row, c.column_byte);
  917. let name_ref = self.arena.put(&c.name);
  918. self.tables.push_ref(&RefRow {
  919. from_idx: c.from,
  920. kind: FUNCTION_REF_CODE,
  921. line: c.line,
  922. column,
  923. reference_name: name_ref,
  924. candidates: NONE_STR,
  925. from_id_str: NONE_STR,
  926. });
  927. }
  928. }
  929. // --- value references (crib of python.rs — same traversal, same cases) ------
  930. fn flush_value_refs(&mut self, root: Node<'t>) {
  931. let scopes = std::mem::take(&mut self.value_scopes);
  932. let mut targets = std::mem::take(&mut self.fs_values);
  933. let counts = std::mem::take(&mut self.fs_value_counts);
  934. if std::env::var("CODEGRAPH_VALUE_REFS").as_deref() == Ok("0") {
  935. return;
  936. }
  937. if targets.is_empty() || scopes.is_empty() || util::is_generated_file(self.file_path) {
  938. return;
  939. }
  940. // Shadow prune — ruby's declarator shape is `assignment`. A
  941. // constant-typed LHS has no named children → constants are never
  942. // counted (never pruned); identifier LHSes (and each identifier of a
  943. // multiple-assign left) bump.
  944. let mut decl_counts: HashMap<&str, u32> = HashMap::new();
  945. let mut dstack: Vec<Node> = vec![root];
  946. let mut dvisited = 0usize;
  947. while let Some(n) = dstack.pop() {
  948. if dvisited >= MAX_VALUE_REF_NODES {
  949. break;
  950. }
  951. dvisited += 1;
  952. if n.kind() == "assignment" {
  953. let left = n
  954. .child_by_field_name("left")
  955. .or_else(|| n.child_by_field_name("pattern"))
  956. .or_else(|| n.named_child(0));
  957. if let Some(left) = left {
  958. if left.kind() == "identifier" {
  959. let nm = self.text(left);
  960. if targets.contains_key(nm) {
  961. *decl_counts.entry(nm).or_insert(0) += 1;
  962. }
  963. } else {
  964. for i in 0..left.named_child_count() {
  965. if let Some(c) = left.named_child(i) {
  966. if c.kind() == "identifier" {
  967. let nm = self.text(c);
  968. if targets.contains_key(nm) {
  969. *decl_counts.entry(nm).or_insert(0) += 1;
  970. }
  971. }
  972. }
  973. }
  974. }
  975. }
  976. }
  977. for i in 0..n.named_child_count() {
  978. if let Some(c) = n.named_child(i) {
  979. dstack.push(c);
  980. }
  981. }
  982. }
  983. let shadowed: Vec<String> = decl_counts
  984. .iter()
  985. .filter(|(nm, c)| **c > counts.get(**nm).copied().unwrap_or(1))
  986. .map(|(nm, _)| nm.to_string())
  987. .collect();
  988. for nm in shadowed {
  989. targets.remove(&nm);
  990. }
  991. if targets.is_empty() {
  992. return;
  993. }
  994. let refs_kind = edge_kind_index("references").unwrap();
  995. for scope in &scopes {
  996. let mut seen: HashSet<&str> = HashSet::new();
  997. let mut stack: Vec<Node> = vec![scope.node];
  998. let mut visited = 0usize;
  999. while let Some(n) = stack.pop() {
  1000. if visited >= MAX_VALUE_REF_NODES {
  1001. break;
  1002. }
  1003. visited += 1;
  1004. // `constant` is the ruby-live reader kind (a constant read IS
  1005. // a constant node); reverse-source-order traversal preserved.
  1006. if matches!(n.kind(), "identifier" | "constant" | "name" | "simple_identifier") {
  1007. let ref_name = self.text(n);
  1008. if let Some(&target_row) = targets.get(ref_name) {
  1009. let target_id = self.node_ids[target_row as usize].as_str();
  1010. if target_id != self.node_ids[scope.row as usize]
  1011. && ref_name != scope.name
  1012. && !seen.contains(&target_id)
  1013. {
  1014. seen.insert(target_id);
  1015. let meta = self.arena.put(r#"{"valueRef":true}"#);
  1016. self.tables.push_edge(&EdgeRow {
  1017. source_idx: scope.row,
  1018. target_idx: target_row,
  1019. kind: refs_kind,
  1020. provenance: 0,
  1021. line: NONE,
  1022. column: NONE,
  1023. metadata_json: meta,
  1024. source_id_str: NONE_STR,
  1025. target_id_str: NONE_STR,
  1026. });
  1027. }
  1028. }
  1029. }
  1030. for i in 0..n.named_child_count() {
  1031. if let Some(c) = n.named_child(i) {
  1032. stack.push(c);
  1033. }
  1034. }
  1035. }
  1036. }
  1037. }
  1038. }
  1039. /// The Ruby `call` node whose argument_list (or keyword pair) contains `node`
  1040. /// — nearest `call` ancestor within 4 parent hops (function-ref.ts:837).
  1041. fn ruby_enclosing_call(node: Node) -> Option<Node> {
  1042. let mut cur = node.parent();
  1043. for _ in 0..4 {
  1044. let c = cur?;
  1045. if c.kind() == "call" {
  1046. return Some(c);
  1047. }
  1048. cur = c.parent();
  1049. }
  1050. None
  1051. }
  1052. fn opt_str(arena: &mut Arena, s: Option<&str>) -> StrRef {
  1053. match s {
  1054. Some(s) => arena.put(s),
  1055. None => NONE_STR,
  1056. }
  1057. }
  1058. #[cfg(test)]
  1059. mod tests {
  1060. use super::posix_normalize;
  1061. #[test]
  1062. fn normalize_matches_node_posix() {
  1063. assert_eq!(posix_normalize("lib/foo/../bar/baz"), "lib/bar/baz");
  1064. assert_eq!(posix_normalize("../x/./y"), "../x/y");
  1065. assert_eq!(posix_normalize("a/../.."), "..");
  1066. assert_eq!(posix_normalize("./foo/bar"), "foo/bar");
  1067. assert_eq!(posix_normalize("a//b"), "a/b");
  1068. assert_eq!(posix_normalize("a/b/"), "a/b/");
  1069. }
  1070. }