ruby.rs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. stack_guard!();
  358. let kind = node.kind();
  359. if kind == "call" && node.child_by_field_name("receiver").is_none() {
  360. if let Some(method) = node.child_by_field_name("method") {
  361. if matches!(self.text(method), "include" | "extend" | "prepend") {
  362. let args = node.child_by_field_name("arguments").or_else(|| {
  363. (0..node.named_child_count())
  364. .filter_map(|i| node.named_child(i))
  365. .find(|c| c.kind() == "argument_list")
  366. });
  367. // (nodeStack is never empty — the file node is pushed.)
  368. if let Some(args) = args {
  369. let parent = self.top_row();
  370. let implements = edge_kind_index("implements").unwrap();
  371. let line = self.line_of(node);
  372. let col = self.col_of(node);
  373. for i in 0..args.named_child_count() {
  374. let Some(arg) = args.named_child(i) else { continue };
  375. // `Mod` is constant, `Foo::Bar` is scope_resolution;
  376. // `extend self` / dynamic args are skipped. Unlike
  377. // every other extraction ref, the hook sets
  378. // `filePath: ctx.filePath` — flagged on the wire.
  379. if matches!(arg.kind(), "constant" | "scope_resolution") {
  380. let name_ref = self.arena.put(self.text(arg));
  381. self.tables.push_ref_flagged(
  382. &RefRow {
  383. from_idx: parent,
  384. kind: implements,
  385. line,
  386. column: col,
  387. reference_name: name_ref,
  388. candidates: NONE_STR,
  389. from_id_str: NONE_STR,
  390. },
  391. REF_FLAG_FILE_PATH,
  392. );
  393. }
  394. }
  395. return true;
  396. }
  397. // no args node → hook declines (falls to extractImport → nothing)
  398. }
  399. }
  400. }
  401. if kind != "module" {
  402. return false;
  403. }
  404. let Some(name_node) = node.child_by_field_name("name") else { return false };
  405. // `module A::B` keeps the scope_resolution text verbatim as the name.
  406. let name = self.text(name_node).to_string();
  407. let Some(row) = self.create_node("module", &name, node, Extra::default()) else {
  408. return false;
  409. };
  410. self.stack.push(Scope { row, kind: "module", name });
  411. if let Some(body) = node.child_by_field_name("body") {
  412. for i in 0..body.named_child_count() {
  413. if let Some(c) = body.named_child(i) {
  414. self.visit_node(c);
  415. }
  416. }
  417. }
  418. self.stack.pop();
  419. true
  420. }
  421. // --- the dispatcher (visitNode, Ruby-relevant branches) ----------------------
  422. fn visit_node(&mut self, node: Node<'t>) {
  423. stack_guard!();
  424. // Language hook FIRST (tree-sitter.ts:943) — a handled subtree is
  425. // scanned for fn-ref candidates and never reaches the ladder (or the
  426. // maybeCaptureFnRefs call below).
  427. if self.try_visit_hook(node) {
  428. self.scan_fn_ref_subtree(node, 0);
  429. return;
  430. }
  431. let kind = node.kind();
  432. let mut skip_children = false;
  433. self.maybe_capture_fn_refs(node);
  434. if kind == "method" {
  435. // functionTypes ∩ methodTypes: inside class-like (module counts!)
  436. // ⇒ method, else function.
  437. if self.inside_class_like() {
  438. self.extract_method(node);
  439. } else {
  440. self.extract_function(node);
  441. }
  442. skip_children = true;
  443. } else if kind == "class" {
  444. self.extract_class(node);
  445. skip_children = true;
  446. } else if kind == "singleton_method" {
  447. // methodTypes-only: extractMethod's 1747 gate bounces a top-level
  448. // `def self.x` to extractFunction — a plain function named x (the
  449. // receiver object is ignored everywhere).
  450. if self.inside_class_like() {
  451. self.extract_method(node);
  452. } else {
  453. self.extract_function(node);
  454. }
  455. skip_children = true;
  456. } else if kind == "assignment"
  457. && (!self.inside_class_like() || self.is_class_scope_constant_assignment(node))
  458. {
  459. // File scope: identifier AND constant LHS extract; class/module
  460. // scope: ONLY constant LHS (isClassScopeConstantAssignment). The
  461. // RHS is never walked (no instantiates/calls from initializers).
  462. self.extract_variable(node);
  463. self.scan_fn_ref_subtree(node, 0);
  464. skip_children = true;
  465. } else if kind == "call" {
  466. // importTypes:['call'] — EVERY non-body call funnels here. Only
  467. // require/require_relative-with-string emit; everything else is
  468. // invisible (children still visited: no skipChildren).
  469. self.extract_import(node);
  470. }
  471. // `singleton_class` (`class << self`), `alias`, top-level `if`/`begin`,
  472. // `uninterpreted`, operator_assignment: no branch — recursed.
  473. if !skip_children {
  474. for i in 0..node.named_child_count() {
  475. if let Some(c) = node.named_child(i) {
  476. self.visit_node(c);
  477. }
  478. }
  479. }
  480. }
  481. /// isClassScopeConstantAssignment (tree-sitter.ts:1508).
  482. fn is_class_scope_constant_assignment(&self, node: Node) -> bool {
  483. let left = node.child_by_field_name("left").or_else(|| node.named_child(0));
  484. left.map(|l| l.kind() == "constant").unwrap_or(false)
  485. }
  486. // --- visitFunctionBody ------------------------------------------------------
  487. fn visit_function_body(&mut self, body: Node<'t>) {
  488. stack_guard!();
  489. self.visit_for_calls_and_structure(body);
  490. }
  491. fn visit_for_calls_and_structure(&mut self, node: Node<'t>) {
  492. stack_guard!();
  493. let kind = node.kind();
  494. self.maybe_capture_fn_refs(node);
  495. if kind == "call" {
  496. self.extract_call(node);
  497. } else if let Some(bare) = self.bare_call_name(node) {
  498. // extractBareCall: statement-level identifiers in BLOCK_PARENTS
  499. // bodies (`do…end` = body_statement; brace blocks are block_body —
  500. // NOT in the set, so `5.times { beep }` emits nothing for beep).
  501. let name = bare.to_string();
  502. let from = self.top_row();
  503. self.push_ref_at(from, &name, edge_kind_index("calls").unwrap(), node);
  504. }
  505. // (No INSTANTIATION_KINDS for ruby — `.new` is handled in extract_call;
  506. // extractStaticMemberRef is gated off; no type annotations.)
  507. // Nested NAMED defs become their own function nodes; classes extract.
  508. // A `module` inside a body mints NOTHING (the visitNode hook does not
  509. // run here) — its children just recurse. Same for singleton_method
  510. // (functionTypes-only check): unmatched, recursed.
  511. if kind == "method" {
  512. let name = self.extract_name(node);
  513. if name != "<anonymous>" {
  514. self.extract_function(node);
  515. return;
  516. }
  517. }
  518. if kind == "class" {
  519. self.extract_class(node);
  520. return;
  521. }
  522. for i in 0..node.named_child_count() {
  523. if let Some(c) = node.named_child(i) {
  524. self.visit_for_calls_and_structure(c);
  525. }
  526. }
  527. }
  528. /// rubyExtractor.extractBareCall (languages/ruby.ts:77-105).
  529. fn bare_call_name(&self, node: Node) -> Option<&'t str> {
  530. if node.kind() != "identifier" {
  531. return None;
  532. }
  533. let parent = node.parent()?;
  534. if !matches!(
  535. parent.kind(),
  536. "body_statement" | "then" | "else" | "do" | "begin" | "rescue" | "ensure" | "when"
  537. ) {
  538. return None;
  539. }
  540. let name = self.text(node);
  541. if matches!(
  542. name,
  543. "true" | "false" | "nil" | "self" | "super" | "__FILE__" | "__LINE__" | "__dir__"
  544. ) {
  545. return None;
  546. }
  547. // charCodeAt(0) in [65,90] — ASCII uppercase only (constants).
  548. let first = name.as_bytes().first().copied().unwrap_or(0);
  549. if (65..=90).contains(&first) {
  550. return None;
  551. }
  552. Some(name)
  553. }
  554. // --- extractors --------------------------------------------------------------
  555. fn extract_function(&mut self, node: Node<'t>) {
  556. stack_guard!();
  557. let name = self.extract_name(node);
  558. if name == "<anonymous>" {
  559. if let Some(body) = node.child_by_field_name("body") {
  560. self.visit_function_body(body);
  561. }
  562. return;
  563. }
  564. let extra = Extra {
  565. docstring: preceding_docstring(node, self.src),
  566. signature: None, // no getSignature hook
  567. visibility: Some(self.visibility_of(node)),
  568. };
  569. let Some(row) = self.create_node("function", &name, node, extra) else { return };
  570. // (ruby ∉ TYPE_ANNOTATION_LANGUAGES; decorators are a structural no-op)
  571. self.stack.push(Scope { row, kind: "function", name });
  572. if let Some(body) = node.child_by_field_name("body") {
  573. self.visit_function_body(body);
  574. }
  575. self.stack.pop();
  576. // `parameters` are NEVER walked — a call in a default value emits nothing.
  577. }
  578. fn extract_method(&mut self, node: Node<'t>) {
  579. stack_guard!();
  580. let name = self.extract_name(node);
  581. let extra = Extra {
  582. docstring: preceding_docstring(node, self.src),
  583. signature: None,
  584. visibility: Some(self.visibility_of(node)),
  585. };
  586. let Some(row) = self.create_node("method", &name, node, extra) else { return };
  587. self.stack.push(Scope { row, kind: "method", name });
  588. if let Some(body) = node.child_by_field_name("body") {
  589. self.visit_function_body(body);
  590. }
  591. self.stack.pop();
  592. }
  593. fn extract_class(&mut self, node: Node<'t>) {
  594. stack_guard!();
  595. let name = self.extract_name(node);
  596. let extra = Extra {
  597. docstring: preceding_docstring(node, self.src),
  598. signature: None,
  599. visibility: Some(self.visibility_of(node)),
  600. };
  601. let Some(row) = self.create_node("class", &name, node, extra) else { return };
  602. // extractInheritance: the `superclass` clause — ONE extends ref, FULL
  603. // text (scope_resolution / even `Struct.new(:a)` expressions verbatim),
  604. // positioned at the type child.
  605. let extends_kind = edge_kind_index("extends").unwrap();
  606. for i in 0..node.named_child_count() {
  607. let Some(child) = node.named_child(i) else { continue };
  608. if child.kind() == "superclass" {
  609. if let Some(target) = child.named_child(0) {
  610. let tname = self.text(target).to_string();
  611. self.push_ref_at(row, &tname, extends_kind, target);
  612. }
  613. }
  614. }
  615. self.stack.push(Scope { row, kind: "class", name });
  616. // Bodiless `class X; end` has no body field → the class node itself
  617. // is walked (name/superclass children revisit harmlessly).
  618. let body = node.child_by_field_name("body").unwrap_or(node);
  619. for i in 0..body.named_child_count() {
  620. if let Some(c) = body.named_child(i) {
  621. self.visit_node(c);
  622. }
  623. }
  624. self.stack.pop();
  625. }
  626. /// extractVariable — the python/ruby assignment branch (2709-2727):
  627. /// identifier or constant LHS mints a `variable` node (no isConst hook —
  628. /// `MAX = 3` is kind variable) at the ASSIGNMENT node's position, with the
  629. /// `= <first 100 utf16 units>` initializer signature.
  630. fn extract_variable(&mut self, node: Node<'t>) {
  631. let docstring = preceding_docstring(node, self.src);
  632. let left = node.child_by_field_name("left").or_else(|| node.named_child(0));
  633. let right = node.child_by_field_name("right").or_else(|| node.named_child(1));
  634. let Some(left) = left else { return };
  635. if !matches!(left.kind(), "identifier" | "constant") {
  636. return;
  637. }
  638. let name = self.text(left).to_string();
  639. let signature = right.map(|r| util::init_signature(self.text(r)));
  640. self.create_node("variable", &name, node, Extra { docstring, signature, visibility: None });
  641. }
  642. /// extractImport — every non-body `call` lands here (importTypes:['call']).
  643. /// Hook (languages/ruby.ts:123): FIRST identifier named child must read
  644. /// require/require_relative (a lowercase receiver is found first and
  645. /// declines; a constant receiver — `Kernel.require "x"` — reaches the
  646. /// method identifier and IS a require); argument_list → string →
  647. /// string_content → moduleName. Then the generic imports ref and
  648. /// emitRubyRequireRefs' path ref.
  649. fn extract_import(&mut self, node: Node<'t>) {
  650. let ident = (0..node.named_child_count())
  651. .filter_map(|i| node.named_child(i))
  652. .find(|c| c.kind() == "identifier");
  653. let Some(ident) = ident else { return };
  654. let mname = self.text(ident);
  655. if mname != "require" && mname != "require_relative" {
  656. return;
  657. }
  658. let arg_list = (0..node.named_child_count())
  659. .filter_map(|i| node.named_child(i))
  660. .find(|c| c.kind() == "argument_list");
  661. let Some(arg_list) = arg_list else { return };
  662. let string = (0..arg_list.named_child_count())
  663. .filter_map(|i| arg_list.named_child(i))
  664. .find(|c| c.kind() == "string");
  665. let Some(string) = string else { return };
  666. let content = (0..string.named_child_count())
  667. .filter_map(|i| string.named_child(i))
  668. .find(|c| c.kind() == "string_content");
  669. let Some(content) = content else { return };
  670. // Interpolated paths take the FIRST string_content only — moduleName
  671. // `interp/` (+ a garbage `interp/.rb` path ref) — deterministic quirk.
  672. let module_name = self.text(content).to_string();
  673. let import_text = self.text(node).trim().to_string();
  674. if module_name.is_empty() {
  675. return;
  676. }
  677. self.create_node(
  678. "import",
  679. &module_name,
  680. node,
  681. Extra { signature: Some(import_text), ..Extra::default() },
  682. );
  683. let parent = self.top_row();
  684. let imports_kind = edge_kind_index("imports").unwrap();
  685. self.push_ref_at(parent, &module_name.clone(), imports_kind, node);
  686. // emitRubyRequireRefs (3532): the file-path ref. Bare gem/stdlib
  687. // requires (no `/`) emit nothing; paths get `.rb` appended.
  688. let req = self.text(content).trim();
  689. if req.is_empty() {
  690. return;
  691. }
  692. let ref_path = if mname == "require_relative" {
  693. let dir = match self.file_path.rfind('/') {
  694. Some(i) => &self.file_path[..i],
  695. None => "",
  696. };
  697. let joined = if dir.is_empty() { req.to_string() } else { format!("{dir}/{req}") };
  698. posix_normalize(&joined)
  699. } else {
  700. req.to_string()
  701. };
  702. if !ref_path.contains('/') {
  703. return;
  704. }
  705. let ref_path =
  706. if ref_path.ends_with(".rb") { ref_path } else { format!("{ref_path}.rb") };
  707. self.push_ref_at(parent, &ref_path, imports_kind, node);
  708. }
  709. /// extractCall — the bespoke ruby branch (tree-sitter.ts:3905-3960),
  710. /// reached only from the body walker.
  711. fn extract_call(&mut self, node: Node<'t>) {
  712. if self.stack.is_empty() {
  713. return;
  714. }
  715. let caller = self.top_row();
  716. let method_name = match node.child_by_field_name("method") {
  717. Some(m) => self.text(m),
  718. None => "",
  719. };
  720. if method_name.is_empty() {
  721. return; // operator/element-reference call with no method name
  722. }
  723. let line = self.line_of(node);
  724. let col = self.col_of(node);
  725. let calls_kind = edge_kind_index("calls").unwrap();
  726. let Some(receiver) = node.child_by_field_name("receiver") else {
  727. // Bare `foo(...)` — just the method name.
  728. self.push_ref(caller, &method_name.to_string(), calls_kind, line, col);
  729. return;
  730. };
  731. let receiver_name = self.text(receiver);
  732. // `Foo.new` / `NS::Widget.new` → instantiates ref to the LAST `::`
  733. // segment; a non-capitalized receiver falls through to a calls ref
  734. // (`lower.new`).
  735. if method_name == "new" {
  736. let class_name = match receiver_name.rfind("::") {
  737. Some(i) => &receiver_name[i + 2..],
  738. None => receiver_name,
  739. };
  740. if class_name.as_bytes().first().map(|b| b.is_ascii_uppercase()).unwrap_or(false) {
  741. self.push_ref(
  742. caller,
  743. &class_name.to_string(),
  744. edge_kind_index("instantiates").unwrap(),
  745. line,
  746. col,
  747. );
  748. return;
  749. }
  750. }
  751. // SKIP_RECEIVERS by TEXT — ruby's set is {self, super} only. `&.`
  752. // joins with a plain `.`; chains/literals keep raw receiver text.
  753. let skip = matches!(receiver_name, "self" | "super");
  754. let callee = if skip {
  755. method_name.to_string()
  756. } else {
  757. format!("{receiver_name}.{method_name}")
  758. };
  759. self.push_ref(caller, &callee, calls_kind, line, col);
  760. // Capitalized constant receiver (`Klass.static_call`, `RETRY_MAX.times`)
  761. // → an ADDITIONAL references ref at the RECEIVER's position.
  762. // (scope_resolution receivers get none — type ≠ constant.)
  763. if !skip && receiver.kind() == "constant" {
  764. self.push_ref_at(
  765. caller,
  766. &receiver_name.to_string(),
  767. edge_kind_index("references").unwrap(),
  768. receiver,
  769. );
  770. }
  771. }
  772. // --- function-as-value refs (RUBY_SPEC, function-ref.ts:262) -----------------
  773. fn maybe_capture_fn_refs(&mut self, node: Node<'t>) {
  774. enum Mode {
  775. Args,
  776. PairValue,
  777. }
  778. let mode = match node.kind() {
  779. "argument_list" => Mode::Args,
  780. "pair" => Mode::PairValue,
  781. _ => return,
  782. };
  783. if self.stack.is_empty() {
  784. return;
  785. }
  786. let from = self.top_row();
  787. let mut values: Vec<Node> = Vec::new();
  788. match mode {
  789. Mode::Args => {
  790. for i in 0..node.named_child_count() {
  791. if let Some(c) = node.named_child(i) {
  792. values.push(c);
  793. }
  794. }
  795. }
  796. Mode::PairValue => {
  797. let value = node.child_by_field_name("value").or_else(|| {
  798. if node.named_child_count() > 0 {
  799. node.named_child(node.named_child_count() - 1)
  800. } else {
  801. None
  802. }
  803. });
  804. if let Some(v) = value {
  805. values.push(v);
  806. }
  807. }
  808. }
  809. for v in values {
  810. self.normalize_fn_ref_value(v, from, 0);
  811. }
  812. }
  813. /// normalizeValue for RUBY_SPEC: idTypes EMPTY (bare identifiers never
  814. /// qualify); `block_argument` is a transparent layer; specials are the
  815. /// `method(:sym)` call form and hook-DSL `simple_symbol`s.
  816. fn normalize_fn_ref_value(&mut self, v: Node<'t>, from: u32, depth: u32) {
  817. stack_guard!();
  818. if depth > 4 {
  819. return;
  820. }
  821. match v.kind() {
  822. "block_argument" => {
  823. for i in 0..v.named_child_count() {
  824. if let Some(c) = v.named_child(i) {
  825. self.normalize_fn_ref_value(c, from, depth + 1);
  826. }
  827. }
  828. }
  829. "call" => {
  830. // `method(:target_cb)` — method field literally `method`, one
  831. // simple_symbol argument. Candidate = bare symbol name at the
  832. // SYMBOL node (gated at flush on defined-in-file ∪ imports).
  833. let Some(method) = v.child_by_field_name("method") else { return };
  834. if self.text(method) != "method" {
  835. return;
  836. }
  837. let Some(args) = v.child_by_field_name("arguments") else { return };
  838. if args.named_child_count() != 1 {
  839. return;
  840. }
  841. let Some(sym) = args.named_child(0) else { return };
  842. if sym.kind() != "simple_symbol" {
  843. return;
  844. }
  845. let name = self.text(sym).strip_prefix(':').unwrap_or(self.text(sym));
  846. if !name.is_empty() {
  847. self.push_fn_ref_cand(from, name, sym);
  848. }
  849. }
  850. "simple_symbol" => {
  851. // Hook-DSL symbols (`before_action :authenticate`) → class-
  852. // scoped `this.<sym>` candidates (always flushed).
  853. let Some(call) = ruby_enclosing_call(v) else { return };
  854. let Some(method) = call.child_by_field_name("method") else { return };
  855. if !is_ruby_hook_call(self.text(method)) {
  856. return;
  857. }
  858. let sym = self.text(v).strip_prefix(':').unwrap_or(self.text(v));
  859. if !ruby_sym_re().is_match(sym) {
  860. return;
  861. }
  862. let name = format!("this.{sym}");
  863. self.push_fn_ref_cand(from, &name, v);
  864. }
  865. _ => {}
  866. }
  867. }
  868. fn push_fn_ref_cand(&mut self, from: u32, name: &str, node: Node) {
  869. if name.is_empty() || is_stoplisted(name) {
  870. return;
  871. }
  872. let p = node.start_position();
  873. self.fn_ref_cands.push(Cand {
  874. from,
  875. name: name.to_string(),
  876. line: p.row as u32 + 1,
  877. column_byte: node.start_byte(),
  878. row: p.row,
  879. });
  880. }
  881. fn scan_fn_ref_subtree(&mut self, node: Node<'t>, depth: u32) {
  882. stack_guard!();
  883. if depth > 12 {
  884. return;
  885. }
  886. // Halts at functionTypes (`method`) + the fixed arrow/lambda list;
  887. // NOT at class/module nodes — the module multiply-capture rides this.
  888. if depth > 0
  889. && matches!(
  890. node.kind(),
  891. "method" | "arrow_function" | "function_expression" | "lambda_literal"
  892. | "lambda_expression"
  893. )
  894. {
  895. return;
  896. }
  897. self.maybe_capture_fn_refs(node);
  898. for i in 0..node.named_child_count() {
  899. if let Some(c) = node.named_child(i) {
  900. self.scan_fn_ref_subtree(c, depth + 1);
  901. }
  902. }
  903. }
  904. fn flush_fn_ref_candidates(&mut self) {
  905. let cands = std::mem::take(&mut self.fn_ref_cands);
  906. if cands.is_empty() || util::is_generated_file(self.file_path) {
  907. return;
  908. }
  909. let mut seen: HashSet<(String, String)> = HashSet::new();
  910. for c in cands {
  911. // `this.`-prefixed candidates always flush (class-scoped resolver);
  912. // bare `method(:x)` names gate on defined-in-file ∪ imports (ruby's
  913. // path-shaped imports match neither name regex, so effectively
  914. // defined-in-file).
  915. if !c.name.starts_with("this.")
  916. && !c.name.contains("::")
  917. && !self.defined_fn_names.contains(&c.name)
  918. && !self.imported_names.contains(&c.name)
  919. {
  920. continue;
  921. }
  922. if !seen.insert((self.node_ids[c.from as usize].clone(), c.name.clone())) {
  923. continue;
  924. }
  925. let column = util::col16(self.src, &self.line_starts, c.row, c.column_byte);
  926. let name_ref = self.arena.put(&c.name);
  927. self.tables.push_ref(&RefRow {
  928. from_idx: c.from,
  929. kind: FUNCTION_REF_CODE,
  930. line: c.line,
  931. column,
  932. reference_name: name_ref,
  933. candidates: NONE_STR,
  934. from_id_str: NONE_STR,
  935. });
  936. }
  937. }
  938. // --- value references (crib of python.rs — same traversal, same cases) ------
  939. fn flush_value_refs(&mut self, root: Node<'t>) {
  940. let scopes = std::mem::take(&mut self.value_scopes);
  941. let mut targets = std::mem::take(&mut self.fs_values);
  942. let counts = std::mem::take(&mut self.fs_value_counts);
  943. if std::env::var("CODEGRAPH_VALUE_REFS").as_deref() == Ok("0") {
  944. return;
  945. }
  946. if targets.is_empty() || scopes.is_empty() || util::is_generated_file(self.file_path) {
  947. return;
  948. }
  949. // Shadow prune — ruby's declarator shape is `assignment`. A
  950. // constant-typed LHS has no named children → constants are never
  951. // counted (never pruned); identifier LHSes (and each identifier of a
  952. // multiple-assign left) bump.
  953. let mut decl_counts: HashMap<&str, u32> = HashMap::new();
  954. let mut dstack: Vec<Node> = vec![root];
  955. let mut dvisited = 0usize;
  956. while let Some(n) = dstack.pop() {
  957. if dvisited >= MAX_VALUE_REF_NODES {
  958. break;
  959. }
  960. dvisited += 1;
  961. if n.kind() == "assignment" {
  962. let left = n
  963. .child_by_field_name("left")
  964. .or_else(|| n.child_by_field_name("pattern"))
  965. .or_else(|| n.named_child(0));
  966. if let Some(left) = left {
  967. if left.kind() == "identifier" {
  968. let nm = self.text(left);
  969. if targets.contains_key(nm) {
  970. *decl_counts.entry(nm).or_insert(0) += 1;
  971. }
  972. } else {
  973. for i in 0..left.named_child_count() {
  974. if let Some(c) = left.named_child(i) {
  975. if c.kind() == "identifier" {
  976. let nm = self.text(c);
  977. if targets.contains_key(nm) {
  978. *decl_counts.entry(nm).or_insert(0) += 1;
  979. }
  980. }
  981. }
  982. }
  983. }
  984. }
  985. }
  986. for i in 0..n.named_child_count() {
  987. if let Some(c) = n.named_child(i) {
  988. dstack.push(c);
  989. }
  990. }
  991. }
  992. let shadowed: Vec<String> = decl_counts
  993. .iter()
  994. .filter(|(nm, c)| **c > counts.get(**nm).copied().unwrap_or(1))
  995. .map(|(nm, _)| nm.to_string())
  996. .collect();
  997. for nm in shadowed {
  998. targets.remove(&nm);
  999. }
  1000. if targets.is_empty() {
  1001. return;
  1002. }
  1003. let refs_kind = edge_kind_index("references").unwrap();
  1004. for scope in &scopes {
  1005. let mut seen: HashSet<&str> = HashSet::new();
  1006. let mut stack: Vec<Node> = vec![scope.node];
  1007. let mut visited = 0usize;
  1008. while let Some(n) = stack.pop() {
  1009. if visited >= MAX_VALUE_REF_NODES {
  1010. break;
  1011. }
  1012. visited += 1;
  1013. // `constant` is the ruby-live reader kind (a constant read IS
  1014. // a constant node); reverse-source-order traversal preserved.
  1015. if matches!(n.kind(), "identifier" | "constant" | "name" | "simple_identifier") {
  1016. let ref_name = self.text(n);
  1017. if let Some(&target_row) = targets.get(ref_name) {
  1018. let target_id = self.node_ids[target_row as usize].as_str();
  1019. if target_id != self.node_ids[scope.row as usize]
  1020. && ref_name != scope.name
  1021. && !seen.contains(&target_id)
  1022. {
  1023. seen.insert(target_id);
  1024. let meta = self.arena.put(r#"{"valueRef":true}"#);
  1025. self.tables.push_edge(&EdgeRow {
  1026. source_idx: scope.row,
  1027. target_idx: target_row,
  1028. kind: refs_kind,
  1029. provenance: 0,
  1030. line: NONE,
  1031. column: NONE,
  1032. metadata_json: meta,
  1033. source_id_str: NONE_STR,
  1034. target_id_str: NONE_STR,
  1035. });
  1036. }
  1037. }
  1038. }
  1039. for i in 0..n.named_child_count() {
  1040. if let Some(c) = n.named_child(i) {
  1041. stack.push(c);
  1042. }
  1043. }
  1044. }
  1045. }
  1046. }
  1047. }
  1048. /// The Ruby `call` node whose argument_list (or keyword pair) contains `node`
  1049. /// — nearest `call` ancestor within 4 parent hops (function-ref.ts:837).
  1050. fn ruby_enclosing_call(node: Node) -> Option<Node> {
  1051. let mut cur = node.parent();
  1052. for _ in 0..4 {
  1053. let c = cur?;
  1054. if c.kind() == "call" {
  1055. return Some(c);
  1056. }
  1057. cur = c.parent();
  1058. }
  1059. None
  1060. }
  1061. fn opt_str(arena: &mut Arena, s: Option<&str>) -> StrRef {
  1062. match s {
  1063. Some(s) => arena.put(s),
  1064. None => NONE_STR,
  1065. }
  1066. }
  1067. #[cfg(test)]
  1068. mod tests {
  1069. use super::posix_normalize;
  1070. #[test]
  1071. fn normalize_matches_node_posix() {
  1072. assert_eq!(posix_normalize("lib/foo/../bar/baz"), "lib/bar/baz");
  1073. assert_eq!(posix_normalize("../x/./y"), "../x/y");
  1074. assert_eq!(posix_normalize("a/../.."), "..");
  1075. assert_eq!(posix_normalize("./foo/bar"), "foo/bar");
  1076. assert_eq!(posix_normalize("a//b"), "a/b");
  1077. assert_eq!(posix_normalize("a/b/"), "a/b/");
  1078. }
  1079. }