scala.rs 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. //! Scala extraction — a faithful Rust port of the scala paths of
  2. //! `TreeSitterExtractor` (src/extraction/tree-sitter.ts) plus
  3. //! languages/scala.ts.
  4. //!
  5. //! Same porting contract as the other walkers: behavior parity, bug-for-bug.
  6. //! The authoritative quirk list is docs/design/scala-kernel-port-checklist.md —
  7. //! including the load-bearing oddities this file preserves on purpose:
  8. //! functionTypes is EMPTY so every def routes through extractMethod (top level
  9. //! falls back to a `function` node); NO namespace node ever (package headers
  10. //! ignored, QNs bare); imports are named the FIRST path segment (`import
  11. //! com.example.C` → `com`); the val/var hook keys on the enclosing-definition
  12. //! NODE TYPE (object vals → constants, class/trait/enum/given vals → fields)
  13. //! and consumes the initializer (no calls/instantiates from hook-consumed
  14. //! initializers); extension methods mint NO nodes (the first def's body calls
  15. //! leak to the enclosing scope, every later def is invisible, and the braced
  16. //! form resolves its `body` field to the `{` TOKEN — whole extension
  17. //! invisible); anonymous `new T { … }` bodies leak their defs to the
  18. //! enclosing scope (findAnonymousClassBody misses template_body); nested
  19. //! defs in bodies mint NOTHING (inverse of kotlin); the bodied-vs-bodiless
  20. //! class asymmetry (bodiless headers walk class_parameters → default-value
  21. //! calls emit from the class; bodied ones never see them); curried signatures
  22. //! keep only the FIRST parameter list and type params win the `parameters`
  23. //! field; static-member WRITES emit (unlike kotlin); infix calls are
  24. //! invisible; `derives` emits nothing; value-ref same-name targets take the
  25. //! LAST registration. Positions in UTF-16 code units. Files with parse errors
  26. //! defer to wasm — including scala-3 PHANTOM hasError files (flag-true, zero
  27. //! ERROR nodes): trust the flag.
  28. use crate::buffers::{
  29. build_meta, edge_kind_index, node_kind_index, Arena, BoolFlags, EdgeRow, EmitOut, NodeRow,
  30. RefRow, StrRef, Tables, FLAG_IS_ASYNC, FLAG_IS_EXPORTED, FLAG_IS_STATIC, FUNCTION_REF_CODE,
  31. NONE, NONE_STR,
  32. };
  33. use crate::docstring::preceding_docstring;
  34. use crate::ids;
  35. use crate::textutil as util;
  36. use regex::Regex;
  37. use std::collections::{HashMap, HashSet};
  38. use std::sync::OnceLock;
  39. use tree_sitter::{Node, Parser};
  40. const MAX_VALUE_REF_NODES: usize = 20_000;
  41. /// NAME_STOPLIST (function-ref.ts).
  42. fn is_stoplisted(name: &str) -> bool {
  43. matches!(
  44. name,
  45. "this" | "self" | "super" | "null" | "nil" | "true" | "false" | "undefined" | "new"
  46. | "NULL" | "nullptr" | "None"
  47. )
  48. }
  49. /// LITERAL_RECEIVER_TYPES (tree-sitter.ts:373-388).
  50. fn is_literal_receiver(kind: &str) -> bool {
  51. matches!(
  52. kind,
  53. "string" | "string_literal" | "interpreted_string_literal" | "raw_string_literal"
  54. | "template_string" | "concatenated_string" | "formatted_string" | "f_string"
  55. | "line_string_literal" | "string_content" | "heredoc_body"
  56. | "number" | "number_literal" | "integer" | "integer_literal" | "float"
  57. | "float_literal" | "int_literal" | "decimal_integer_literal" | "real_literal"
  58. | "char_literal" | "character_literal" | "rune_literal" | "regex" | "regex_literal"
  59. | "true" | "false" | "boolean_literal" | "bool_literal" | "none" | "null" | "nil"
  60. | "null_literal" | "undefined"
  61. | "list" | "list_literal" | "array" | "array_literal" | "array_creation_expression"
  62. | "dictionary" | "dict_literal" | "object" | "tuple" | "set"
  63. )
  64. }
  65. /// BUILTIN_TYPES (tree-sitter.ts:5768-5782) — the shared cross-language table.
  66. fn is_builtin_type(name: &str) -> bool {
  67. matches!(
  68. name,
  69. "string" | "number" | "boolean" | "void" | "null" | "undefined" | "never" | "any"
  70. | "unknown" | "object" | "symbol" | "bigint" | "true" | "false"
  71. | "str" | "bool" | "i8" | "i16" | "i32" | "i64" | "i128" | "isize"
  72. | "u8" | "u16" | "u32" | "u64" | "u128" | "usize" | "f32" | "f64" | "char"
  73. | "int" | "long" | "short" | "byte" | "float" | "double"
  74. | "int8" | "int16" | "int32" | "int64" | "uint8" | "uint16" | "uint32" | "uint64"
  75. | "float32" | "float64" | "complex64" | "complex128" | "rune" | "error"
  76. | "Int" | "Long" | "Short" | "Byte" | "Float" | "Double" | "Boolean" | "Char"
  77. | "Unit" | "String" | "Any" | "AnyRef" | "AnyVal" | "Nothing" | "Null"
  78. )
  79. }
  80. /// SCALA_BUILTIN_TYPES (languages/scala.ts:14-17) — the hook's OWN smaller set.
  81. fn is_scala_builtin(name: &str) -> bool {
  82. matches!(
  83. name,
  84. "Int" | "Long" | "Short" | "Byte" | "Float" | "Double" | "Boolean" | "Char" | "Unit"
  85. | "String" | "Any" | "AnyRef" | "AnyVal" | "Nothing" | "Null"
  86. )
  87. }
  88. /// extractScalaReturnType's simple-name gate (`/^[A-Za-z_]\w*$/`).
  89. fn simple_type_name_re() -> &'static Regex {
  90. static RE: OnceLock<Regex> = OnceLock::new();
  91. RE.get_or_init(|| Regex::new(r"^[A-Za-z_]\w*$").unwrap())
  92. }
  93. /// extractScalaReturnType's generic-args strip (`/\[[^\]]*\]/g`).
  94. fn bracket_args_re() -> &'static Regex {
  95. static RE: OnceLock<Regex> = OnceLock::new();
  96. RE.get_or_init(|| Regex::new(r"\[[^\]]*\]").unwrap())
  97. }
  98. /// Static-member receiver gate (`/^[A-Z][A-Za-z0-9_]*$/`).
  99. fn cap_ident_re() -> &'static Regex {
  100. static RE: OnceLock<Regex> = OnceLock::new();
  101. RE.get_or_init(|| Regex::new(r"^[A-Z][A-Za-z0-9_]*$").unwrap())
  102. }
  103. /// The #750 re-encode gate (`/^[A-Z]/`).
  104. fn starts_upper_re() -> &'static Regex {
  105. static RE: OnceLock<Regex> = OnceLock::new();
  106. RE.get_or_init(|| Regex::new(r"^[A-Z]").unwrap())
  107. }
  108. /// JS `\s+` for the re-encode/return-type strips (Unicode whitespace).
  109. fn ws_re() -> &'static Regex {
  110. static RE: OnceLock<Regex> = OnceLock::new();
  111. RE.get_or_init(|| Regex::new(r"\s+").unwrap())
  112. }
  113. struct Scope {
  114. row: u32,
  115. kind: &'static str,
  116. name: String,
  117. }
  118. struct Cand {
  119. from: u32,
  120. name: String,
  121. line: u32,
  122. column_byte: usize,
  123. row: usize,
  124. }
  125. struct ValueScope<'t> {
  126. row: u32,
  127. node: Node<'t>,
  128. name: String,
  129. }
  130. #[derive(Default)]
  131. struct Extra {
  132. docstring: Option<String>,
  133. signature: Option<String>,
  134. /// 0 = absent; 1 public, 2 private, 3 protected.
  135. visibility: u8,
  136. /// (present, value) — isAsync/isStatic are literal-false hooks for scala.
  137. is_async: Option<bool>,
  138. is_static: Option<bool>,
  139. return_type: Option<String>,
  140. }
  141. pub struct Walker<'t> {
  142. src: &'t str,
  143. file_path: &'t str,
  144. line_starts: Vec<usize>,
  145. arena: Arena,
  146. tables: Tables,
  147. stack: Vec<Scope>,
  148. node_ids: Vec<String>,
  149. defined_fn_names: HashSet<String>,
  150. imported_names: HashSet<String>,
  151. fn_ref_cands: Vec<Cand>,
  152. fs_values: HashMap<String, u32>,
  153. fs_value_counts: HashMap<String, u32>,
  154. value_scopes: Vec<ValueScope<'t>>,
  155. }
  156. pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
  157. let grammar = crate::langs::grammar_for("scala").ok_or("no scala grammar")?;
  158. let t0 = std::time::Instant::now();
  159. let mut parser = Parser::new();
  160. parser
  161. .set_language(&grammar)
  162. .map_err(|e| format!("set_language(scala) failed: {e}"))?;
  163. let tree = parser
  164. .parse(source, None)
  165. .ok_or_else(|| "parser returned null tree".to_string())?;
  166. if tree.root_node().has_error() {
  167. // Includes scala-3 PHANTOMS (flag-true, zero ERROR nodes) — the flag
  168. // is the policy, never node-scanning.
  169. return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
  170. }
  171. let mut w = Walker {
  172. src: source,
  173. file_path,
  174. line_starts: util::line_starts(source),
  175. arena: Arena::default(),
  176. tables: Tables::default(),
  177. stack: Vec::new(),
  178. node_ids: Vec::new(),
  179. defined_fn_names: HashSet::new(),
  180. imported_names: HashSet::new(),
  181. fn_ref_cands: Vec::new(),
  182. fs_values: HashMap::new(),
  183. fs_value_counts: HashMap::new(),
  184. value_scopes: Vec::new(),
  185. };
  186. // File node (tree-sitter.ts:508-521).
  187. let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
  188. let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
  189. let mut flags = BoolFlags::default();
  190. flags.set(FLAG_IS_EXPORTED, false);
  191. let file_id = w.arena.put(&ids::file_node_id(file_path));
  192. let name_ref = w.arena.put(base_name);
  193. let qn_ref = w.arena.put(file_path);
  194. w.tables.push_node(&NodeRow {
  195. kind: node_kind_index("file").unwrap(),
  196. visibility: 0,
  197. flags,
  198. start_line: 1,
  199. end_line: line_count,
  200. start_column: 0,
  201. end_column: 0,
  202. name: name_ref,
  203. qualified_name: qn_ref,
  204. id: file_id,
  205. docstring: NONE_STR,
  206. signature: NONE_STR,
  207. decorators: NONE_STR,
  208. type_parameters: NONE_STR,
  209. return_type: NONE_STR,
  210. extra_json: NONE_STR,
  211. });
  212. w.node_ids.push(ids::file_node_id(file_path));
  213. w.stack.push(Scope { row: 0, kind: "file", name: base_name.to_string() });
  214. // No packageTypes → no namespace node, ever.
  215. w.visit(tree.root_node());
  216. w.flush_fn_ref_candidates();
  217. w.flush_value_refs(tree.root_node());
  218. w.stack.pop();
  219. let duration_ms = t0.elapsed().as_secs_f64() * 1000.0;
  220. let meta = build_meta(&w.tables, w.arena.len(), NONE_STR, duration_ms);
  221. Ok(EmitOut {
  222. meta,
  223. nodes: w.tables.nodes,
  224. edges: w.tables.edges,
  225. refs: w.tables.refs,
  226. arena: w.arena.into_vec(),
  227. })
  228. }
  229. impl<'t> Walker<'t> {
  230. fn text(&self, node: Node) -> &'t str {
  231. &self.src[node.byte_range()]
  232. }
  233. fn line_of(&self, node: Node) -> u32 {
  234. node.start_position().row as u32 + 1
  235. }
  236. fn col_of(&self, node: Node) -> u32 {
  237. util::col16(self.src, &self.line_starts, node.start_position().row, node.start_byte())
  238. }
  239. fn end_col_of(&self, node: Node) -> u32 {
  240. util::col16(self.src, &self.line_starts, node.end_position().row, node.end_byte())
  241. }
  242. fn top_row(&self) -> u32 {
  243. self.stack.last().map(|s| s.row).unwrap_or(0)
  244. }
  245. /// isInsideClassLikeNode (:1486) — stack-top kind only.
  246. fn inside_class_like(&self) -> bool {
  247. self.stack
  248. .last()
  249. .map(|s| matches!(s.kind, "class" | "struct" | "interface" | "trait" | "enum" | "module"))
  250. .unwrap_or(false)
  251. }
  252. fn push_ref_at(&mut self, from_row: u32, name: &str, kind: &str, node: Node) {
  253. let name_ref = self.arena.put(name);
  254. self.tables.push_ref(&RefRow {
  255. from_idx: from_row,
  256. kind: edge_kind_index(kind).unwrap(),
  257. line: self.line_of(node),
  258. column: self.col_of(node),
  259. reference_name: name_ref,
  260. candidates: NONE_STR,
  261. from_id_str: NONE_STR,
  262. });
  263. // flushFnRefCandidates' importedNames (tree-sitter.ts:661-675). Scala
  264. // import refs are named the FIRST path segment — always SIMPLE_NAME.
  265. if kind == "imports" {
  266. if util::simple_name().is_match(name) {
  267. self.imported_names.insert(name.to_string());
  268. } else if let Some(c) = util::qualified_import().captures(name) {
  269. self.imported_names.insert(c[1].to_string());
  270. }
  271. }
  272. }
  273. // --- createNode (tree-sitter.ts:1308) ---------------------------------
  274. fn create_node(&mut self, kind: &'static str, name: &str, node: Node<'t>, extra: Extra) -> Option<u32> {
  275. if name.is_empty() {
  276. return None;
  277. }
  278. let start_line = self.line_of(node);
  279. let id = ids::node_id(self.file_path, kind, name, start_line);
  280. // buildQualifiedName (:1447-1460) — non-file stack names, `::`-joined;
  281. // namespacePrefix always empty (no C++ namespaces, no scala namespace).
  282. let qualified = {
  283. let mut parts: Vec<&str> = Vec::new();
  284. for s in &self.stack {
  285. if s.kind != "file" {
  286. parts.push(&s.name);
  287. }
  288. }
  289. let mut qn = parts.join("::");
  290. if !qn.is_empty() {
  291. qn.push_str("::");
  292. }
  293. qn.push_str(name);
  294. qn
  295. };
  296. let name_ref = self.arena.put(name);
  297. let qn_ref = self.arena.put(&qualified);
  298. let id_ref = self.arena.put(&id);
  299. let doc_ref = opt_str(&mut self.arena, extra.docstring.as_deref());
  300. let sig_ref = opt_str(&mut self.arena, extra.signature.as_deref());
  301. let ret_ref = opt_str(&mut self.arena, extra.return_type.as_deref());
  302. let mut flags = BoolFlags::default();
  303. if let Some(v) = extra.is_async {
  304. flags.set(FLAG_IS_ASYNC, v);
  305. }
  306. if let Some(v) = extra.is_static {
  307. flags.set(FLAG_IS_STATIC, v);
  308. }
  309. let row = self.tables.push_node(&NodeRow {
  310. kind: node_kind_index(kind).unwrap(),
  311. visibility: extra.visibility,
  312. flags,
  313. start_line,
  314. end_line: node.end_position().row as u32 + 1, // no resolveBody hook
  315. start_column: self.col_of(node),
  316. end_column: self.end_col_of(node),
  317. name: name_ref,
  318. qualified_name: qn_ref,
  319. id: id_ref,
  320. docstring: doc_ref,
  321. signature: sig_ref,
  322. decorators: NONE_STR,
  323. type_parameters: NONE_STR,
  324. return_type: ret_ref,
  325. extra_json: NONE_STR,
  326. });
  327. self.node_ids.push(id.clone());
  328. if kind == "function" || kind == "method" {
  329. self.defined_fn_names.insert(name.to_string());
  330. }
  331. let parent_row = self.top_row();
  332. self.tables.push_edge(&EdgeRow {
  333. source_idx: parent_row,
  334. target_idx: row,
  335. kind: edge_kind_index("contains").unwrap(),
  336. provenance: 0,
  337. line: NONE,
  338. column: NONE,
  339. metadata_json: NONE_STR,
  340. source_id_str: NONE_STR,
  341. target_id_str: NONE_STR,
  342. });
  343. // captureValueRefScope (:735-767).
  344. if (kind == "constant" || kind == "variable")
  345. && util::utf16_len(name) >= 3
  346. && util::has_upper_or_underscore().is_match(name)
  347. {
  348. let parent_ok = self
  349. .stack
  350. .last()
  351. .map(|s| matches!(s.kind, "file" | "class" | "module" | "struct" | "enum"))
  352. .unwrap_or(false);
  353. if parent_ok {
  354. self.fs_values.insert(name.to_string(), row); // LAST wins
  355. *self.fs_value_counts.entry(name.to_string()).or_insert(0) += 1;
  356. }
  357. }
  358. if matches!(kind, "function" | "method" | "constant" | "variable") {
  359. self.value_scopes.push(ValueScope { row, node, name: name.to_string() });
  360. }
  361. Some(row)
  362. }
  363. // --- languages/scala.ts helper transcriptions -------------------------
  364. /// getValVarName (scala.ts:5-11).
  365. fn val_var_name(&self, node: Node<'t>) -> Option<&'t str> {
  366. let pattern = node.child_by_field_name("pattern")?;
  367. if pattern.kind() == "identifier" {
  368. return Some(self.text(pattern));
  369. }
  370. let mut cursor = pattern.walk();
  371. for c in pattern.named_children(&mut cursor) {
  372. if c.kind() == "identifier" {
  373. return Some(self.text(c));
  374. }
  375. }
  376. None
  377. }
  378. /// extractVisibility (scala.ts:69-80) → wire byte (1 public default).
  379. fn visibility_of(&self, node: Node<'t>) -> u8 {
  380. let mut cursor = node.walk();
  381. for c in node.named_children(&mut cursor) {
  382. if c.kind() == "modifiers" || c.kind() == "access_modifier" {
  383. let t = self.text(c);
  384. if t.contains("private") {
  385. return 2;
  386. }
  387. if t.contains("protected") {
  388. return 3;
  389. }
  390. }
  391. }
  392. 1
  393. }
  394. /// isStatic (scala.ts:123-129) — text scan, effectively always false.
  395. fn is_static_of(&self, node: Node<'t>) -> bool {
  396. let mut cursor = node.walk();
  397. for c in node.named_children(&mut cursor) {
  398. if c.kind() == "modifiers" && self.text(c).contains("static") {
  399. return true;
  400. }
  401. }
  402. false
  403. }
  404. /// getSignature (scala.ts:110-117) — first-match-wins fields: curried
  405. /// defs keep only the first list; a type_parameters node carrying field
  406. /// `parameters` wins over the value list.
  407. fn signature_of(&self, node: Node<'t>) -> Option<String> {
  408. let params = node.child_by_field_name("parameters");
  409. let ret = node.child_by_field_name("return_type");
  410. if params.is_none() && ret.is_none() {
  411. return None;
  412. }
  413. let mut sig = params.map(|p| self.text(p).to_string()).unwrap_or_default();
  414. if let Some(r) = ret {
  415. sig.push_str(": ");
  416. sig.push_str(self.text(r));
  417. }
  418. if sig.is_empty() {
  419. None
  420. } else {
  421. Some(sig)
  422. }
  423. }
  424. /// extractScalaReturnType (scala.ts:56-67).
  425. fn return_type_of(&self, node: Node<'t>) -> Option<String> {
  426. let rt = node.child_by_field_name("return_type")?;
  427. let raw = self.text(rt).trim();
  428. if raw.starts_with("this.") {
  429. return None;
  430. }
  431. let base = bracket_args_re().replace_all(raw, "");
  432. let base = ws_re().replace_all(&base, "");
  433. let last = base.split('.').next_back()?;
  434. if last.is_empty() || !simple_type_name_re().is_match(last) {
  435. return None;
  436. }
  437. Some(last.to_string())
  438. }
  439. /// scalaBaseTypeName (tree-sitter.ts:201-224).
  440. fn scala_base_type_name(&self, node: Option<Node<'t>>) -> Option<String> {
  441. let node = node?;
  442. match node.kind() {
  443. "type_identifier" | "identifier" => Some(self.text(node).to_string()),
  444. "generic_type" => self.scala_base_type_name(node.named_child(0)),
  445. "stable_type_identifier" | "stable_identifier" => {
  446. let mut cursor = node.walk();
  447. let last = node
  448. .named_children(&mut cursor)
  449. .filter(|c| c.kind() == "type_identifier" || c.kind() == "identifier")
  450. .last();
  451. last.map(|n| self.text(n).to_string())
  452. }
  453. _ => {
  454. let mut cursor = node.walk();
  455. let id = node
  456. .named_children(&mut cursor)
  457. .find(|c| c.kind() == "type_identifier");
  458. id.map(|n| self.text(n).to_string())
  459. }
  460. }
  461. }
  462. /// emitScalaTypeRefs (scala.ts:27-45) — the hook's own builtin set.
  463. fn emit_scala_type_refs(&mut self, type_node: Node<'t>, from_row: u32) {
  464. if type_node.kind() == "type_identifier" {
  465. let name = self.text(type_node);
  466. if !name.is_empty() && !is_scala_builtin(name) {
  467. let name = name.to_string();
  468. self.push_ref_at(from_row, &name, "references", type_node);
  469. }
  470. return;
  471. }
  472. let mut cursor = type_node.walk();
  473. let kids: Vec<Node<'t>> = type_node.named_children(&mut cursor).collect();
  474. for c in kids {
  475. self.emit_scala_type_refs(c, from_row);
  476. }
  477. }
  478. /// extractName (tree-sitter.ts:98-192) — scala-reachable branches: the
  479. /// `name` field's raw text (operator glyphs and backticks kept), else the
  480. /// first identifier-ish child, else `<anonymous>`.
  481. fn extract_name(&self, node: Node<'t>) -> String {
  482. if let Some(name_node) = node.child_by_field_name("name") {
  483. return self.text(name_node).to_string();
  484. }
  485. let mut cursor = node.walk();
  486. for c in node.named_children(&mut cursor) {
  487. if matches!(c.kind(), "identifier" | "type_identifier" | "simple_identifier" | "constant") {
  488. return self.text(c).to_string();
  489. }
  490. }
  491. "<anonymous>".to_string()
  492. }
  493. // --- the main walk (visitNode, tree-sitter.ts:936-1303) ---------------
  494. fn visit(&mut self, node: Node<'t>) {
  495. // The visitNode hook (scala.ts:131-198) runs FIRST.
  496. if self.hook(node) {
  497. self.scan_fn_ref_subtree(node, 0);
  498. return;
  499. }
  500. // maybeCaptureFnRefs (:990).
  501. self.maybe_capture_fn_refs(node);
  502. let kind = node.kind();
  503. match kind {
  504. // methodTypes (functionTypes is EMPTY — :994 never fires).
  505. "function_definition" | "function_declaration" => {
  506. self.extract_method_or_function(node);
  507. return; // skipChildren
  508. }
  509. "class_definition" | "object_definition" => {
  510. self.extract_class(node, "class");
  511. return;
  512. }
  513. "trait_definition" => {
  514. self.extract_class(node, "trait");
  515. return;
  516. }
  517. "enum_definition" => {
  518. self.extract_enum(node);
  519. return;
  520. }
  521. "type_definition" => {
  522. let skip = self.extract_type_alias(node);
  523. if skip {
  524. return;
  525. }
  526. // plain path → false → children re-visited (nothing matches).
  527. }
  528. "import_declaration" => {
  529. self.extract_import(node);
  530. return; // skipChildren
  531. }
  532. "call_expression" => {
  533. self.extract_call(node);
  534. // no skipChildren — chains/args re-visited
  535. }
  536. "instance_expression" => {
  537. // INSTANTIATION_KINDS (:1255). findAnonymousClassBody looks
  538. // for class_body/declaration_list — scala's template_body is
  539. // neither → extractAnonymousClass never runs → children
  540. // recursed: anon-body defs LEAK to the enclosing scope.
  541. self.extract_instantiation(node);
  542. }
  543. _ => {}
  544. }
  545. let mut cursor = node.walk();
  546. let children: Vec<Node<'t>> = node.named_children(&mut cursor).collect();
  547. for child in children {
  548. self.visit(child);
  549. }
  550. }
  551. /// The visitNode hook (scala.ts:131-198). Returns true when consumed.
  552. fn hook(&mut self, node: Node<'t>) -> bool {
  553. match node.kind() {
  554. "val_definition" | "var_definition" => {
  555. let is_val = node.kind() == "val_definition";
  556. let name = match self.val_var_name(node) {
  557. Some(n) => n.to_string(),
  558. None => return false,
  559. };
  560. // Enclosing-definition NODE-TYPE walk (scala.ts:146-156).
  561. let mut enclosing: Option<&'static str> = None;
  562. let mut p = node.parent();
  563. while let Some(parent) = p {
  564. match parent.kind() {
  565. "class_definition" => {
  566. enclosing = Some("class_definition");
  567. break;
  568. }
  569. "trait_definition" => {
  570. enclosing = Some("trait_definition");
  571. break;
  572. }
  573. "enum_definition" => {
  574. enclosing = Some("enum_definition");
  575. break;
  576. }
  577. "given_definition" => {
  578. enclosing = Some("given_definition");
  579. break;
  580. }
  581. "object_definition" => {
  582. enclosing = Some("object_definition");
  583. break;
  584. }
  585. _ => p = parent.parent(),
  586. }
  587. }
  588. let is_instance_field = matches!(
  589. enclosing,
  590. Some("class_definition") | Some("trait_definition") | Some("enum_definition")
  591. | Some("given_definition")
  592. );
  593. let kind: &'static str = if is_instance_field {
  594. "field"
  595. } else if is_val {
  596. "constant"
  597. } else {
  598. "variable"
  599. };
  600. let type_node = node.child_by_field_name("type");
  601. let signature = type_node.map(|t| {
  602. format!("{} {}: {}", if is_val { "val" } else { "var" }, name, self.text(t))
  603. });
  604. let visibility = self.visibility_of(node);
  605. let created = self.create_node(
  606. kind,
  607. &name,
  608. node,
  609. Extra { signature, visibility, ..Default::default() },
  610. );
  611. if let (Some(row), Some(t)) = (created, type_node) {
  612. self.emit_scala_type_refs(t, row);
  613. }
  614. true
  615. }
  616. "enum_case_definitions" => {
  617. let mut cursor = node.walk();
  618. let cases: Vec<Node<'t>> = node.named_children(&mut cursor).collect();
  619. for case in cases {
  620. if case.kind() == "simple_enum_case" || case.kind() == "full_enum_case" {
  621. if let Some(name_node) = case.child_by_field_name("name") {
  622. let name = self.text(name_node).to_string();
  623. // ctx.createNode('enum_member', name, child) — no
  624. // extras: no docstring/visibility/flags.
  625. self.create_node("enum_member", &name, case, Extra::default());
  626. }
  627. }
  628. }
  629. true
  630. }
  631. "extension_definition" => {
  632. // childForFieldName('body') is FIRST-MATCH-WINS over the full
  633. // (named + anonymous) child list: paren/indent form → the
  634. // first function_definition (its children visited — no node
  635. // minted, later defs invisible); braced form → the `{` TOKEN
  636. // (namedChildCount 0 — whole extension invisible).
  637. if let Some(body) = node.child_by_field_name("body") {
  638. let mut cursor = body.walk();
  639. let kids: Vec<Node<'t>> = body.named_children(&mut cursor).collect();
  640. for child in kids {
  641. self.visit(child);
  642. }
  643. }
  644. true
  645. }
  646. _ => false,
  647. }
  648. }
  649. // --- extractMethod → extractFunction routing (:1737 / :1517) ----------
  650. fn extract_method_or_function(&mut self, node: Node<'t>) {
  651. // No receiver hook, no methodsAreTopLevel: inside class-like → method,
  652. // else → function (the object/object_expression parent check never
  653. // matches scala node kinds).
  654. let is_method = self.inside_class_like();
  655. let name = self.extract_name(node);
  656. if name == "<anonymous>" {
  657. // Unreachable for scala defs (name field required) — preserved:
  658. // walk the body with nothing pushed.
  659. if let Some(body) = node.child_by_field_name("body") {
  660. self.visit_body(body);
  661. }
  662. return;
  663. }
  664. let docstring = preceding_docstring(node, self.src);
  665. let signature = self.signature_of(node);
  666. let visibility = self.visibility_of(node);
  667. let is_static = self.is_static_of(node);
  668. let return_type = self.return_type_of(node);
  669. let row = self.create_node(
  670. if is_method { "method" } else { "function" },
  671. &name,
  672. node,
  673. Extra {
  674. docstring,
  675. signature,
  676. visibility,
  677. is_async: Some(false),
  678. is_static: Some(is_static),
  679. return_type,
  680. },
  681. );
  682. let Some(row) = row else { return };
  683. self.extract_type_annotations(node, row);
  684. self.extract_decorators_for(node, row);
  685. self.stack.push(Scope { row, kind: if is_method { "method" } else { "function" }, name });
  686. if let Some(body) = node.child_by_field_name("body") {
  687. self.visit_body(body);
  688. }
  689. self.stack.pop();
  690. }
  691. // --- extractClass (:1679) — classes, objects, traits ------------------
  692. fn extract_class(&mut self, node: Node<'t>, kind: &'static str) {
  693. let resolved_body = node.child_by_field_name("body"); // template_body
  694. // No skipBodilessClass — bodiless mints (scala-complete).
  695. let name = self.extract_name(node);
  696. let docstring = preceding_docstring(node, self.src);
  697. let visibility = self.visibility_of(node);
  698. let row = self.create_node(
  699. kind,
  700. &name,
  701. node,
  702. Extra { docstring, visibility, ..Default::default() },
  703. );
  704. let Some(row) = row else { return };
  705. self.extract_inheritance(node, row);
  706. self.extract_decorators_for(node, row);
  707. self.stack.push(Scope { row, kind, name });
  708. // THE ASYMMETRY: bodiless classes walk the node ITSELF — header
  709. // children (class_parameters defaults, extends args) reach the
  710. // ladder; bodied classes walk only template_body children.
  711. let body = resolved_body.unwrap_or(node);
  712. let mut cursor = body.walk();
  713. let children: Vec<Node<'t>> = body.named_children(&mut cursor).collect();
  714. for child in children {
  715. self.visit(child);
  716. }
  717. self.stack.pop();
  718. }
  719. // --- extractEnum (:1914) ----------------------------------------------
  720. fn extract_enum(&mut self, node: Node<'t>) {
  721. let body = match node.child_by_field_name("body") {
  722. Some(b) => b,
  723. None => return, // bodiless enum mints nothing
  724. };
  725. let name = self.extract_name(node);
  726. let docstring = preceding_docstring(node, self.src);
  727. let visibility = self.visibility_of(node);
  728. let row = self.create_node(
  729. "enum",
  730. &name,
  731. node,
  732. Extra { docstring, visibility, ..Default::default() },
  733. );
  734. let Some(row) = row else { return };
  735. self.extract_inheritance(node, row);
  736. // No extractDecoratorsFor on the enum path (annotated enums emit no
  737. // decorates — shared-pipeline behavior).
  738. self.stack.push(Scope { row, kind: "enum", name });
  739. // enumMemberTypes is EMPTY → every body child goes through visitNode
  740. // (enum_case_definitions hits the hook; defs become methods).
  741. let mut cursor = body.walk();
  742. let children: Vec<Node<'t>> = body.named_children(&mut cursor).collect();
  743. for child in children {
  744. self.visit(child);
  745. }
  746. self.stack.pop();
  747. }
  748. // --- extractTypeAlias (:2890, plain path :2967-2991) ------------------
  749. /// Returns skipChildren — always false on the scala plain path.
  750. fn extract_type_alias(&mut self, node: Node<'t>) -> bool {
  751. let name = self.extract_name(node);
  752. if name == "<anonymous>" {
  753. return false;
  754. }
  755. let docstring = preceding_docstring(node, self.src);
  756. // isExported hook absent; visibility not read on this path. The
  757. // alias-value ref walk reads field 'value' — scala's field is 'type'
  758. // → no reference to the aliased type, ever.
  759. self.create_node("type_alias", &name, node, Extra { docstring, ..Default::default() });
  760. false
  761. }
  762. // --- extractImport (:3170-3236) ---------------------------------------
  763. fn extract_import(&mut self, node: Node<'t>) {
  764. let import_text = self.text(node).trim();
  765. // extractImport hook (scala.ts:200-211): `path` field is FIRST-MATCH-
  766. // WINS → the FIRST dotted segment names the import.
  767. let module = if let Some(path) = node.child_by_field_name("path") {
  768. Some(self.text(path))
  769. } else {
  770. let mut cursor = node.walk();
  771. let mut found = None;
  772. for c in node.named_children(&mut cursor) {
  773. if c.kind() == "identifier" || c.kind() == "stable_identifier" {
  774. found = Some(self.text(c));
  775. break;
  776. }
  777. }
  778. found
  779. };
  780. let Some(module) = module else { return };
  781. let module = module.to_string();
  782. let signature = import_text.to_string();
  783. let created = self.create_node(
  784. "import",
  785. &module,
  786. node,
  787. Extra { signature: Some(signature), ..Default::default() },
  788. );
  789. // Generic imports ref (:3183-3194) — hook sets no handledRefs.
  790. if created.is_some() && !module.is_empty() && !self.stack.is_empty() {
  791. let parent_row = self.top_row();
  792. self.push_ref_at(parent_row, &module, "imports", node);
  793. }
  794. }
  795. // --- extractCall (:3684) ----------------------------------------------
  796. fn extract_call(&mut self, node: Node<'t>) {
  797. if self.stack.is_empty() {
  798. return;
  799. }
  800. let caller_row = self.top_row();
  801. let func = node
  802. .child_by_field_name("function")
  803. .or_else(|| node.named_child(0));
  804. let Some(func) = func else { return };
  805. let mut callee: Option<String> = None;
  806. if func.kind() == "field_expression" {
  807. // Member branch (:4364): property = `field` field for scala.
  808. let property = func
  809. .child_by_field_name("property")
  810. .or_else(|| func.child_by_field_name("field"))
  811. .or_else(|| func.named_child(1));
  812. if let Some(property) = property {
  813. let method_name = self.text(property);
  814. let receiver = func
  815. .child_by_field_name("object")
  816. .or_else(|| func.child_by_field_name("operand"))
  817. .or_else(|| func.child_by_field_name("argument"))
  818. .or_else(|| func.named_child(0));
  819. if let Some(receiver) = receiver {
  820. if is_literal_receiver(receiver.kind()) {
  821. return; // literal receivers emit NOTHING (#1230)
  822. }
  823. if matches!(receiver.kind(), "identifier" | "simple_identifier" | "field_identifier") {
  824. let recv_name = self.text(receiver);
  825. if matches!(recv_name, "self" | "this" | "cls" | "super") {
  826. callee = Some(method_name.to_string());
  827. } else {
  828. callee = Some(format!("{recv_name}.{method_name}"));
  829. }
  830. } else if receiver.kind() == "call_expression" {
  831. // The #750 re-encode, scala arm (:4443-4464): inner
  832. // callee via the REAL `function` field; re-encode only
  833. // capitalized (companion-factory / apply) chains.
  834. let inner_fn = receiver.child_by_field_name("function");
  835. let inner_callee = inner_fn
  836. .map(|f| {
  837. let t = self.text(f).replace("->", ".");
  838. ws_re().replace_all(&t, "").into_owned()
  839. })
  840. .unwrap_or_default();
  841. let reencode = starts_upper_re().is_match(&inner_callee);
  842. callee = Some(if reencode {
  843. format!("{inner_callee}().{method_name}")
  844. } else {
  845. method_name.to_string()
  846. });
  847. } else {
  848. callee = Some(method_name.to_string());
  849. }
  850. } else {
  851. callee = Some(method_name.to_string());
  852. }
  853. }
  854. } else {
  855. // Else branch (:4518-4520): RAW func text (apply-sugar `WidgetS`,
  856. // `genericCall[Int]` type args kept, curried `curried(1)` inners).
  857. callee = Some(self.text(func).to_string());
  858. }
  859. let Some(mut callee) = callee else { return };
  860. // Parenthesized-conversion (:4529-4532).
  861. if let Some(caps) = util::paren_conversion().captures(&callee) {
  862. if let Some(inner) = caps.get(1) {
  863. callee = inner.as_str().to_string();
  864. }
  865. }
  866. if callee.is_empty() {
  867. return;
  868. }
  869. self.push_ref_at(caller_row, &callee, "calls", node);
  870. }
  871. // --- extractInstantiation (:4610, scala arm :4647-4662) ---------------
  872. fn extract_instantiation(&mut self, node: Node<'t>) {
  873. if self.stack.is_empty() {
  874. return;
  875. }
  876. let from_row = self.top_row();
  877. let ctor = node
  878. .child_by_field_name("constructor")
  879. .or_else(|| node.child_by_field_name("type"))
  880. .or_else(|| node.child_by_field_name("name"))
  881. .or_else(|| node.named_child(0));
  882. let Some(ctor) = ctor else { return };
  883. if let Some(name) = self.scala_base_type_name(Some(ctor)) {
  884. self.push_ref_at(from_row, &name, "instantiates", node);
  885. }
  886. }
  887. // --- extractStaticMemberRef (:4750-4808) ------------------------------
  888. fn extract_static_member_ref(&mut self, node: Node<'t>) {
  889. if self.stack.is_empty() {
  890. return;
  891. }
  892. let owner_row = self.top_row();
  893. // MEMBER_ACCESS_TYPES — only field_expression occurs in scala trees.
  894. if !matches!(
  895. node.kind(),
  896. "field_access" | "member_access_expression" | "navigation_expression"
  897. | "field_expression" | "class_constant_access_expression"
  898. | "scoped_property_access_expression" | "qualified_identifier"
  899. ) {
  900. return;
  901. }
  902. // Callee-of-call skip: `Type.method()`'s callee access is already a
  903. // calls ref.
  904. if let Some(parent) = node.parent() {
  905. if parent.kind() == "call_expression" {
  906. let callee = parent
  907. .child_by_field_name("function")
  908. .or_else(|| parent.child_by_field_name("method"))
  909. .or_else(|| parent.named_child(0));
  910. if let Some(callee) = callee {
  911. if callee.start_byte() == node.start_byte() {
  912. return;
  913. }
  914. }
  915. }
  916. }
  917. let recv = node
  918. .child_by_field_name("object")
  919. .or_else(|| node.child_by_field_name("expression"))
  920. .or_else(|| node.child_by_field_name("scope"))
  921. .or_else(|| node.named_child(0));
  922. let Some(recv) = recv else { return };
  923. if matches!(
  924. recv.kind(),
  925. "identifier" | "type_identifier" | "simple_identifier" | "name" | "scoped_type_identifier"
  926. ) {
  927. let text = self.text(recv);
  928. if cap_ident_re().is_match(text) {
  929. let text = text.to_string();
  930. self.push_ref_at(owner_row, &text, "references", recv);
  931. }
  932. }
  933. }
  934. // --- extractDecoratorsFor (:4897-5024) --------------------------------
  935. fn extract_decorators_for(&mut self, decl: Node<'t>, decorated_row: u32) {
  936. // consider(): scala annotations are `annotation` nodes; the name is
  937. // the first identifier-ish child (type_identifier for scala), with
  938. // call_expression unwrap for invoked decorators.
  939. // Scan 1: direct children (+ modifiers descent — inert for scala,
  940. // annotations aren't inside modifiers in this grammar, but ported).
  941. let mut cursor = decl.walk();
  942. let kids: Vec<Node<'t>> = decl.named_children(&mut cursor).collect();
  943. for child in kids {
  944. self.consider_decorator(child, decorated_row);
  945. if child.kind() == "modifiers" {
  946. let mut mc = child.walk();
  947. let inner: Vec<Node<'t>> = child.named_children(&mut mc).collect();
  948. for m in inner {
  949. self.consider_decorator(m, decorated_row);
  950. }
  951. }
  952. }
  953. // Scan 2: preceding siblings (TS class style — inert for scala where
  954. // annotations are children, ported for fidelity).
  955. if let Some(parent) = decl.parent() {
  956. let decl_start = decl.start_byte();
  957. let mut decl_idx: Option<usize> = None;
  958. for i in 0..parent.named_child_count() {
  959. if let Some(sib) = parent.named_child(i) {
  960. if sib.start_byte() == decl_start {
  961. decl_idx = Some(i);
  962. break;
  963. }
  964. }
  965. }
  966. if let Some(di) = decl_idx {
  967. for j in (0..di).rev() {
  968. let Some(sib) = parent.named_child(j) else { continue };
  969. if !matches!(sib.kind(), "decorator" | "annotation" | "marker_annotation") {
  970. break;
  971. }
  972. self.consider_decorator(sib, decorated_row);
  973. }
  974. }
  975. }
  976. }
  977. fn consider_decorator(&mut self, n: Node<'t>, decorated_row: u32) {
  978. if !matches!(n.kind(), "decorator" | "annotation" | "marker_annotation" | "attribute") {
  979. return;
  980. }
  981. let mut target: Option<Node<'t>> = None;
  982. let mut cursor = n.walk();
  983. let kids: Vec<Node<'t>> = n.named_children(&mut cursor).collect();
  984. for child in kids {
  985. if child.kind() == "call_expression" {
  986. let fnn = child.child_by_field_name("function").or_else(|| child.named_child(0));
  987. if let Some(f) = fnn {
  988. target = Some(f);
  989. }
  990. if target.is_some() {
  991. break;
  992. }
  993. }
  994. if matches!(
  995. child.kind(),
  996. "identifier" | "member_expression" | "scoped_identifier" | "navigation_expression"
  997. | "user_type" | "type_identifier"
  998. ) {
  999. target = Some(child);
  1000. break;
  1001. }
  1002. }
  1003. let Some(target) = target else { return };
  1004. let mut name = self.text(target).to_string();
  1005. if let Some(lt) = name.find('<') {
  1006. if lt > 0 {
  1007. name.truncate(lt);
  1008. }
  1009. }
  1010. let last_dot = name.rfind('.').map(|i| i as i64).unwrap_or(-1);
  1011. let last_colons = name.rfind("::").map(|i| (i + 1) as i64).unwrap_or(-1);
  1012. let last = last_dot.max(last_colons);
  1013. if last >= 0 {
  1014. name = name[(last as usize + 1)..].to_string();
  1015. name = name.trim_start_matches([':', '.']).to_string();
  1016. }
  1017. let name = name.trim().to_string();
  1018. if name.is_empty() {
  1019. return;
  1020. }
  1021. self.push_ref_at(decorated_row, &name, "decorates", n);
  1022. }
  1023. // --- extractInheritance — the scala branch (:5339-5360) ---------------
  1024. fn extract_inheritance(&mut self, node: Node<'t>, class_row: u32) {
  1025. let mut cursor = node.walk();
  1026. let kids: Vec<Node<'t>> = node.named_children(&mut cursor).collect();
  1027. for child in kids {
  1028. if matches!(
  1029. child.kind(),
  1030. "extends_clause" | "superclass" | "base_clause" | "extends_interfaces"
  1031. ) {
  1032. // Iterate ALL supertypes (with-chains, comma form); unwrap
  1033. // each via scalaBaseTypeName; `arguments` children → None →
  1034. // skipped. `derives_clause` is a different kind — silent.
  1035. let mut cc = child.walk();
  1036. let targets: Vec<Node<'t>> = child.named_children(&mut cc).collect();
  1037. for target in targets {
  1038. if let Some(name) = self.scala_base_type_name(Some(target)) {
  1039. self.push_ref_at(class_row, &name, "extends", target);
  1040. }
  1041. }
  1042. }
  1043. }
  1044. }
  1045. // --- extractTypeAnnotations (:5788-5880) ------------------------------
  1046. fn extract_type_annotations(&mut self, node: Node<'t>, row: u32) {
  1047. // Scala walks EVERY `parameters`-TYPE child (all curried lists; the
  1048. // type_parameters node is a different kind, matched by walk 3).
  1049. let mut cursor = node.walk();
  1050. let kids: Vec<Node<'t>> = node.named_children(&mut cursor).collect();
  1051. for pc in &kids {
  1052. if pc.kind() == "parameters" {
  1053. self.type_refs_from_subtree(*pc, row);
  1054. }
  1055. }
  1056. if let Some(rt) = node.child_by_field_name("return_type") {
  1057. self.type_refs_from_subtree(rt, row);
  1058. }
  1059. // Context/upper bounds: the first type_parameters child.
  1060. if let Some(tp) = kids.iter().find(|c| c.kind() == "type_parameters") {
  1061. self.type_refs_from_subtree(*tp, row);
  1062. }
  1063. // Direct type_annotation child — no such scala kind; ported cheaply.
  1064. if let Some(ta) = kids.iter().find(|c| c.kind() == "type_annotation") {
  1065. self.type_refs_from_subtree(*ta, row);
  1066. }
  1067. }
  1068. fn type_refs_from_subtree(&mut self, node: Node<'t>, from_row: u32) {
  1069. if node.kind() == "type_identifier" {
  1070. let name = self.text(node);
  1071. if !name.is_empty() && !is_builtin_type(name) {
  1072. let name = name.to_string();
  1073. self.push_ref_at(from_row, &name, "references", node);
  1074. }
  1075. return;
  1076. }
  1077. let mut cursor = node.walk();
  1078. let kids: Vec<Node<'t>> = node.named_children(&mut cursor).collect();
  1079. for c in kids {
  1080. self.type_refs_from_subtree(c, from_row);
  1081. }
  1082. }
  1083. // --- visitFunctionBody (:5129-5286) — scala rows ----------------------
  1084. fn visit_body(&mut self, node: Node<'t>) {
  1085. self.maybe_capture_fn_refs(node);
  1086. let kind = node.kind();
  1087. if kind == "call_expression" {
  1088. self.extract_call(node);
  1089. // falls through to recursion
  1090. } else if kind == "instance_expression" {
  1091. // instantiates + recursion (findAnonymousClassBody null): anon
  1092. // template_body defs are NOT dispatched here (functionTypes
  1093. // empty; methodTypes not checked in this walker) — their calls
  1094. // attribute to the enclosing method.
  1095. self.extract_instantiation(node);
  1096. }
  1097. self.extract_static_member_ref(node);
  1098. // Nested named defs mint NOTHING (:5245 checks functionTypes — EMPTY;
  1099. // the inverse of kotlin). Body-local classes/objects/traits/enums DO
  1100. // extract fully.
  1101. match kind {
  1102. "class_definition" | "object_definition" => {
  1103. self.extract_class(node, "class");
  1104. return;
  1105. }
  1106. "trait_definition" => {
  1107. self.extract_class(node, "trait");
  1108. return;
  1109. }
  1110. "enum_definition" => {
  1111. self.extract_enum(node);
  1112. return;
  1113. }
  1114. _ => {}
  1115. }
  1116. let mut cursor = node.walk();
  1117. let children: Vec<Node<'t>> = node.named_children(&mut cursor).collect();
  1118. for child in children {
  1119. self.visit_body(child);
  1120. }
  1121. }
  1122. // --- function-as-value capture (#756) — SCALA_SPEC --------------------
  1123. fn maybe_capture_fn_refs(&mut self, node: Node<'t>) {
  1124. let (mode, field): (&str, &str) = match node.kind() {
  1125. "arguments" => ("args", ""),
  1126. "assignment_expression" => ("rhs", "right"),
  1127. "val_definition" => ("varinit", "value"),
  1128. _ => return,
  1129. };
  1130. if self.stack.is_empty() {
  1131. return;
  1132. }
  1133. let from = self.top_row();
  1134. let mut values: Vec<Node<'t>> = Vec::new();
  1135. match mode {
  1136. "args" => {
  1137. let mut cursor = node.walk();
  1138. for c in node.named_children(&mut cursor) {
  1139. values.push(c);
  1140. }
  1141. }
  1142. "rhs" => {
  1143. if let Some(rhs) = node.child_by_field_name(field) {
  1144. // Param-storage skip: lhs tail == rhs text.
  1145. let lhs = node
  1146. .child_by_field_name("left")
  1147. .or_else(|| node.child_by_field_name("lhs"))
  1148. .or_else(|| node.child_by_field_name("target"))
  1149. .or_else(|| {
  1150. if node.named_child_count() >= 2 {
  1151. node.named_child(0)
  1152. } else {
  1153. None
  1154. }
  1155. });
  1156. let lhs_text = lhs.map(|l| self.text(l)).unwrap_or("");
  1157. let lhs_last = util::lhs_last_name()
  1158. .captures(lhs_text)
  1159. .and_then(|c| c.get(1))
  1160. .map(|m| m.as_str());
  1161. if !(lhs_last.is_some() && lhs_last == Some(self.text(rhs).trim())) {
  1162. values.push(rhs);
  1163. }
  1164. }
  1165. }
  1166. _ => {
  1167. // varinit — destructuring patterns capture nothing.
  1168. let name_node = node
  1169. .child_by_field_name("name")
  1170. .or_else(|| node.child_by_field_name("pattern"));
  1171. if let Some(nn) = name_node {
  1172. if matches!(
  1173. nn.kind(),
  1174. "object_pattern" | "array_pattern" | "tuple_pattern" | "struct_pattern"
  1175. ) {
  1176. return;
  1177. }
  1178. }
  1179. if let Some(v) = node.child_by_field_name(field) {
  1180. values.push(v);
  1181. }
  1182. }
  1183. }
  1184. for v in values {
  1185. self.normalize_fn_ref_value(v, from, 0);
  1186. }
  1187. }
  1188. /// normalizeValue with SCALA_SPEC's unwrap (postfix_expression → first
  1189. /// named child — eta-expansion `handler _`). No layers.
  1190. fn normalize_fn_ref_value(&mut self, v: Node<'t>, from: u32, depth: u32) {
  1191. if depth > 4 {
  1192. return;
  1193. }
  1194. match v.kind() {
  1195. "identifier" => {
  1196. let name = self.text(v).to_string();
  1197. if name.is_empty() || is_stoplisted(&name) {
  1198. return;
  1199. }
  1200. let p = v.start_position();
  1201. self.fn_ref_cands.push(Cand {
  1202. from,
  1203. name,
  1204. line: p.row as u32 + 1,
  1205. column_byte: v.start_byte(),
  1206. row: p.row,
  1207. });
  1208. }
  1209. "postfix_expression" => {
  1210. if let Some(inner) = v.named_child(0) {
  1211. self.normalize_fn_ref_value(inner, from, depth + 1);
  1212. }
  1213. }
  1214. _ => {}
  1215. }
  1216. }
  1217. fn scan_fn_ref_subtree(&mut self, node: Node<'t>, depth: u32) {
  1218. if depth > 12 {
  1219. return;
  1220. }
  1221. // Halt list: functionTypes is EMPTY for scala, so only the literal
  1222. // lambda kinds halt — the scan descends into nested
  1223. // function_definitions inside hook-consumed vals.
  1224. if depth > 0
  1225. && matches!(
  1226. node.kind(),
  1227. "arrow_function" | "function_expression" | "lambda_literal" | "lambda_expression"
  1228. )
  1229. {
  1230. return;
  1231. }
  1232. self.maybe_capture_fn_refs(node);
  1233. let mut cursor = node.walk();
  1234. let children: Vec<Node<'t>> = node.named_children(&mut cursor).collect();
  1235. for c in children {
  1236. self.scan_fn_ref_subtree(c, depth + 1);
  1237. }
  1238. }
  1239. fn flush_fn_ref_candidates(&mut self) {
  1240. let cands = std::mem::take(&mut self.fn_ref_cands);
  1241. if cands.is_empty() || util::is_generated_file(self.file_path) {
  1242. return;
  1243. }
  1244. let mut seen: HashSet<(String, String)> = HashSet::new();
  1245. for c in cands {
  1246. if !c.name.starts_with("this.")
  1247. && !c.name.contains("::")
  1248. && !self.defined_fn_names.contains(&c.name)
  1249. && !self.imported_names.contains(&c.name)
  1250. {
  1251. continue;
  1252. }
  1253. if !seen.insert((self.node_ids[c.from as usize].clone(), c.name.clone())) {
  1254. continue;
  1255. }
  1256. let column = util::col16(self.src, &self.line_starts, c.row, c.column_byte);
  1257. let name_ref = self.arena.put(&c.name);
  1258. self.tables.push_ref(&RefRow {
  1259. from_idx: c.from,
  1260. kind: FUNCTION_REF_CODE,
  1261. line: c.line,
  1262. column,
  1263. reference_name: name_ref,
  1264. candidates: NONE_STR,
  1265. from_id_str: NONE_STR,
  1266. });
  1267. }
  1268. }
  1269. // --- value-reference edges (:398-931) ---------------------------------
  1270. fn flush_value_refs(&mut self, root: Node<'t>) {
  1271. let scopes = std::mem::take(&mut self.value_scopes);
  1272. let mut targets = std::mem::take(&mut self.fs_values);
  1273. let counts = std::mem::take(&mut self.fs_value_counts);
  1274. if std::env::var("CODEGRAPH_VALUE_REFS").as_deref() == Ok("0") {
  1275. return;
  1276. }
  1277. if targets.is_empty() || scopes.is_empty() || util::is_generated_file(self.file_path) {
  1278. return;
  1279. }
  1280. // Shadow prune — the scala declarator shape: val_definition /
  1281. // var_definition with an `identifier` pattern (tuple/case-class
  1282. // patterns bump nothing).
  1283. let mut decl_counts: HashMap<&str, u32> = HashMap::new();
  1284. let mut dstack: Vec<Node> = vec![root];
  1285. let mut dvisited = 0usize;
  1286. while let Some(n) = dstack.pop() {
  1287. if dvisited >= MAX_VALUE_REF_NODES {
  1288. break;
  1289. }
  1290. dvisited += 1;
  1291. if matches!(n.kind(), "val_definition" | "var_definition") {
  1292. if let Some(pat) = n.child_by_field_name("pattern") {
  1293. if pat.kind() == "identifier" {
  1294. let nm = self.text(pat);
  1295. if targets.contains_key(nm) {
  1296. *decl_counts.entry(nm).or_insert(0) += 1;
  1297. }
  1298. }
  1299. }
  1300. }
  1301. for i in 0..n.named_child_count() {
  1302. if let Some(c) = n.named_child(i) {
  1303. dstack.push(c);
  1304. }
  1305. }
  1306. }
  1307. let shadowed: Vec<String> = decl_counts
  1308. .iter()
  1309. .filter(|(nm, c)| **c > counts.get(**nm).copied().unwrap_or(1))
  1310. .map(|(nm, _)| nm.to_string())
  1311. .collect();
  1312. for nm in shadowed {
  1313. targets.remove(&nm);
  1314. }
  1315. if targets.is_empty() {
  1316. return;
  1317. }
  1318. let refs_kind = edge_kind_index("references").unwrap();
  1319. for scope in &scopes {
  1320. let mut seen: HashSet<&str> = HashSet::new();
  1321. let mut stack: Vec<Node> = vec![scope.node];
  1322. // The Dart/Pascal sibling-body pull (:891) — a next sibling of
  1323. // kind function_body/block joins the scan. Effectively inert for
  1324. // scala (bodies nest) but ported for fidelity.
  1325. if let Some(sib) = scope.node.next_named_sibling() {
  1326. if matches!(sib.kind(), "function_body" | "block") {
  1327. stack.push(sib);
  1328. }
  1329. }
  1330. let mut visited = 0usize;
  1331. while let Some(n) = stack.pop() {
  1332. if visited >= MAX_VALUE_REF_NODES {
  1333. break;
  1334. }
  1335. visited += 1;
  1336. if matches!(n.kind(), "identifier" | "constant" | "name" | "simple_identifier") {
  1337. let ref_name = self.text(n);
  1338. if let Some(&target_row) = targets.get(ref_name) {
  1339. let target_id = self.node_ids[target_row as usize].as_str();
  1340. if target_id != self.node_ids[scope.row as usize]
  1341. && ref_name != scope.name
  1342. && !seen.contains(&target_id)
  1343. {
  1344. seen.insert(target_id);
  1345. let meta = self.arena.put(r#"{"valueRef":true}"#);
  1346. self.tables.push_edge(&EdgeRow {
  1347. source_idx: scope.row,
  1348. target_idx: target_row,
  1349. kind: refs_kind,
  1350. provenance: 0,
  1351. line: NONE,
  1352. column: NONE,
  1353. metadata_json: meta,
  1354. source_id_str: NONE_STR,
  1355. target_id_str: NONE_STR,
  1356. });
  1357. }
  1358. }
  1359. }
  1360. for i in 0..n.named_child_count() {
  1361. if let Some(c) = n.named_child(i) {
  1362. stack.push(c);
  1363. }
  1364. }
  1365. }
  1366. }
  1367. }
  1368. }
  1369. fn opt_str(arena: &mut Arena, s: Option<&str>) -> StrRef {
  1370. match s {
  1371. Some(s) => arena.put(s),
  1372. None => NONE_STR,
  1373. }
  1374. }