use crate::calls::extract_type_references;
use crate::imports::{parse_imports, specifier_imported_name, specifier_local_name};
use crate::parser::{grammar_for, LangId};
use serde::{Deserialize, Serialize};
use std::collections::{hash_map::Entry, BTreeMap, BTreeSet, HashMap};
use std::path::Path;
use std::sync::OnceLock;
#[derive(Default)]
pub struct FactParsers {
parsers: HashMap<LangId, tree_sitter::Parser>,
}
impl FactParsers {
pub fn parse_source(&mut self, lang: LangId, source: &str) -> Option<tree_sitter::Tree> {
if crate::parser::markdown_too_deep_to_parse(lang, source) {
return None;
}
let parser = match self.parsers.entry(lang) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
let grammar = grammar_for(lang).ok()?;
let mut parser = tree_sitter::Parser::new();
if parser.set_language(&grammar).is_err() {
return None;
}
entry.insert(parser)
}
};
parser.parse(source, None)
}
}
pub const RUST_MACRO_REF_SHAPE_CALL: &str = "call";
pub const RUST_MACRO_REF_SHAPE_METHOD: &str = "method";
pub const RUST_MACRO_REF_SHAPE_STRUCT: &str = "struct";
pub const TOP_LEVEL_SYMBOL: &str = "<top-level>";
#[derive(Debug, Default)]
pub struct FileAnalysis {
pub oversized: bool,
pub freshness: Option<crate::cache_freshness::FileFreshness>,
pub rust_api: Option<RustApiFacts>,
pub go_dispatch: Option<GoDispatchFacts>,
pub raw_imports: Vec<RawImportContribution>,
pub rust_imports: Vec<RawImportContribution>,
pub raw_reexports: Vec<RawReexportContribution>,
pub attribute_entry_points: Vec<String>,
pub macro_token_refs: Vec<MacroTokenRefContribution>,
pub cfg_test_ranges: Vec<RustCfgTestRange>,
pub cfg_test_module_files: Vec<String>,
pub globs_parent_module: bool,
pub trait_impl_methods: Vec<String>,
pub type_ref_names: BTreeSet<String>,
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct StoredFileFacts {
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub not_text: bool,
#[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
pub type_ref_names: BTreeSet<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub js: Option<StoredJsFacts>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rust: Option<StoredRustFacts>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub go: Option<GoDispatchFacts>,
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct StoredJsFacts {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub raw_imports: Vec<RawImportContribution>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub raw_reexports: Vec<RawReexportContribution>,
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct StoredRustFacts {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub imports: Vec<RawImportContribution>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub raw_reexports: Vec<RawReexportContribution>,
pub api: RustApiFacts,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub attribute_entry_points: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub macro_token_refs: Vec<MacroTokenRefContribution>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub cfg_test_ranges: Vec<RustCfgTestRange>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub cfg_test_modules: Vec<RustCfgTestModule>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub globs_parent_module: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub trait_impl_methods: Vec<String>,
}
impl StoredFileFacts {
pub fn read_for(lang: LangId) -> bool {
supports_type_refs(lang)
}
pub fn encode_for_record(source: &[u8], lang: LangId, tree: &tree_sitter::Tree) -> Vec<u8> {
let facts = match std::str::from_utf8(source) {
Ok(source) => Self::extract(source, lang, Some(tree)),
Err(_) => Self {
not_text: true,
..Self::default()
},
};
serde_json::to_vec(&facts).unwrap_or_default()
}
pub fn decode(bytes: &[u8]) -> Result<Self, String> {
serde_json::from_slice(bytes).map_err(|error| error.to_string())
}
pub fn extract(source: &str, lang: LangId, tree: Option<&tree_sitter::Tree>) -> Self {
if !Self::read_for(lang) {
return Self::default();
}
let type_ref_names = tree
.map(|tree| extract_type_references(source, tree.root_node(), lang))
.unwrap_or_default();
let js = matches!(lang, LangId::TypeScript | LangId::Tsx | LangId::JavaScript).then(|| {
StoredJsFacts {
raw_imports: tree
.map(|tree| raw_imports_from_tree(source, tree, lang))
.unwrap_or_default(),
raw_reexports: tree
.map(|tree| ts_raw_reexport_contributions(source, tree.root_node()))
.unwrap_or_default(),
}
});
let rust = (lang == LangId::Rust).then(|| {
let api = tree
.map(|tree| rust_api_facts(source, tree.root_node(), ""))
.unwrap_or_default();
let attribute_entry_points = tree
.map(|tree| {
let mut roots = BTreeSet::new();
for entry in
crate::parser::rust_attribute_entry_points(source, tree.root_node())
{
roots.insert(entry.name);
roots.insert(entry.scoped_name);
}
roots.extend(rust_serde_function_references(
source,
tree.root_node(),
api.items.as_slice(),
));
roots.into_iter().collect()
})
.unwrap_or_default();
StoredRustFacts {
imports: tree
.map(|tree| rust_raw_import_contributions(source, tree))
.unwrap_or_default(),
raw_reexports: rust_raw_reexport_contributions(source),
attribute_entry_points,
macro_token_refs: tree
.map(|tree| rust_macro_token_refs(source, tree.root_node()))
.unwrap_or_default(),
cfg_test_ranges: tree
.map(|tree| rust_cfg_test_ranges(source, tree.root_node()))
.unwrap_or_default(),
cfg_test_modules: tree
.map(|tree| rust_cfg_test_modules(source, tree.root_node()))
.unwrap_or_default(),
globs_parent_module: tree
.is_some_and(|tree| rust_globs_parent_module(source, tree.root_node())),
trait_impl_methods: tree
.map(|tree| rust_trait_impl_methods(source, tree.root_node()))
.unwrap_or_default(),
api,
}
});
let go = (lang == LangId::Go).then(|| {
tree.map(|tree| go_dispatch_facts(source, tree.root_node()))
.unwrap_or_default()
});
Self {
not_text: false,
type_ref_names,
js,
rust,
go,
}
}
pub fn into_analysis(
self,
lang: LangId,
file: &Path,
relative_file: &str,
has_oxc_file: bool,
) -> FileAnalysis {
if self.not_text || !Self::read_for(lang) {
return FileAnalysis::default();
}
let needs_ts_raw_facts =
matches!(lang, LangId::TypeScript | LangId::Tsx | LangId::JavaScript) && !has_oxc_file;
let js = self.js.unwrap_or_default();
let rust = (lang == LangId::Rust).then(|| {
let mut rust = self.rust.unwrap_or_default();
for module in &mut rust.api.modules {
if module.file.is_some() {
let mut nested = module.scope.clone();
nested.push(module.name.clone());
module.file = Some(rust_module_file(
relative_file,
&nested,
module.path_override.as_deref(),
));
}
}
rust
});
let go_dispatch = (lang == LangId::Go).then(|| {
let mut facts = self.go.unwrap_or_default();
facts.module = go_module_dir(file, relative_file);
facts
});
let raw_reexports = if needs_ts_raw_facts {
js.raw_reexports
} else {
rust.as_ref()
.map(|rust| rust.raw_reexports.clone())
.unwrap_or_default()
};
let Some(rust) = rust else {
return FileAnalysis {
go_dispatch,
raw_imports: if needs_ts_raw_facts {
js.raw_imports
} else {
Vec::new()
},
raw_reexports,
type_ref_names: self.type_ref_names,
..FileAnalysis::default()
};
};
FileAnalysis {
oversized: false,
freshness: None,
go_dispatch,
raw_imports: Vec::new(),
raw_reexports,
cfg_test_module_files: rust_cfg_test_module_files_for(
relative_file,
&rust.cfg_test_modules,
),
rust_api: Some(rust.api),
rust_imports: rust.imports,
attribute_entry_points: rust.attribute_entry_points,
macro_token_refs: rust.macro_token_refs,
cfg_test_ranges: rust.cfg_test_ranges,
globs_parent_module: rust.globs_parent_module,
trait_impl_methods: rust.trait_impl_methods,
type_ref_names: self.type_ref_names,
}
}
}
pub fn go_module_dir(file: &Path, relative_file: &str) -> String {
let mut module = String::new();
let mut relative = Path::new(relative_file).parent().unwrap_or(Path::new(""));
let mut absolute = file.parent().unwrap_or(Path::new(""));
loop {
if absolute.join("go.mod").is_file() {
module = relative.to_string_lossy().replace('\\', "/");
break;
}
let (Some(next_relative), Some(next_absolute)) = (relative.parent(), absolute.parent())
else {
break;
};
relative = next_relative;
absolute = next_absolute;
}
module
}
#[derive(Debug, Clone)]
pub struct RustMacroToken<'a> {
pub text: &'a str,
pub kind: &'a str,
pub line: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct RustCfgTestRange {
pub start_line: u32,
pub end_line: u32,
}
impl RustCfgTestRange {
pub fn contains(self, line: u32) -> bool {
self.start_line <= line && line <= self.end_line
}
}
pub fn rust_globs_parent_module(source: &str, root: tree_sitter::Node) -> bool {
let mut cursor = root.walk();
let globs = root.children(&mut cursor).any(|node| {
node.kind() == "use_declaration"
&& node_text(source, node)
.chars()
.filter(|ch| !ch.is_whitespace())
.collect::<String>()
.ends_with("usesuper::*;")
});
globs
}
pub fn rust_serde_function_references(
source: &str,
root: tree_sitter::Node,
items: &[RustApiItem],
) -> Vec<String> {
static SERDE_FUNCTION_ATTRIBUTE: OnceLock<regex::Regex> = OnceLock::new();
let pattern = SERDE_FUNCTION_ATTRIBUTE.get_or_init(|| {
regex::Regex::new(
r#"\b(?:deserialize_with|serialize_with|default|skip_serializing_if)\s*=\s*"([A-Za-z_][A-Za-z0-9_:]*)""#,
)
.expect("serde function attribute pattern compiles")
});
let identities = items
.iter()
.filter(|item| item.owner.is_none())
.map(rust_qualified_item_name)
.collect::<BTreeSet<_>>();
pattern
.captures_iter(source)
.filter_map(|captures| captures.get(1))
.map(|path| {
let scope = root
.descendant_for_byte_range(path.start(), path.end())
.map(|node| rust_mod_scope_chain(&node, source))
.unwrap_or_default();
rust_serde_root_identity(path.as_str(), &scope, &identities)
})
.collect()
}
pub fn rust_serde_root_identity(
path: &str,
scope: &[String],
identities: &BTreeSet<String>,
) -> String {
let mut segments = rust_path_segments(path);
let short = segments.last().cloned().unwrap_or_default();
let mut scope = scope.to_vec();
let mut anchored = false;
while let Some(first) = segments.first().map(String::as_str) {
match first {
"self" => {}
"super" => {
scope.pop();
}
"crate" => scope.clear(),
_ => break,
}
segments.remove(0);
anchored = true;
}
loop {
let candidate = scope
.iter()
.chain(&segments)
.cloned()
.collect::<Vec<_>>()
.join("::");
if identities.contains(&candidate) {
return candidate;
}
if anchored || scope.pop().is_none() {
break;
}
}
if !anchored && segments.len() == 1 {
let suffix = format!("::{short}");
let mut matches = identities
.iter()
.filter(|identity| identity.ends_with(&suffix));
if let (Some(only), None) = (matches.next(), matches.next()) {
return only.clone();
}
}
short
}
pub fn rust_trait_impl_methods(source: &str, root: tree_sitter::Node) -> Vec<String> {
let mut names = BTreeSet::new();
let mut stack = vec![root];
while let Some(node) = stack.pop() {
if node.kind() == "impl_item" && node.child_by_field_name("trait").is_some() {
if let Some(body) = node.child_by_field_name("body") {
let mut cursor = body.walk();
for item in body.children(&mut cursor) {
if item.kind() == "function_item" {
if let Some(name) = item.child_by_field_name("name") {
names.insert(node_text(source, name).to_string());
}
}
}
}
continue;
}
let mut cursor = node.walk();
stack.extend(node.children(&mut cursor));
}
names.into_iter().collect()
}
pub fn rust_path_segments(path: &str) -> Vec<String> {
path.split("::")
.map(str::trim)
.filter(|segment| !segment.is_empty())
.map(str::to_string)
.collect()
}
pub fn macro_scoped_symbol(path: &[String], name: &str) -> String {
if path.is_empty() {
name.to_string()
} else {
format!("{}::{name}", path.join("::"))
}
}
pub fn raw_imports_from_tree(
source: &str,
tree: &tree_sitter::Tree,
lang: LangId,
) -> Vec<RawImportContribution> {
parse_imports(source, tree, lang)
.imports
.into_iter()
.map(|import| RawImportContribution {
source: import.module_path,
names: import.names,
default_import: import.default_import,
namespace_import: import.namespace_import,
})
.collect()
}
pub fn rust_raw_import_contributions(
source: &str,
tree: &tree_sitter::Tree,
) -> Vec<RawImportContribution> {
parse_imports(source, tree, LangId::Rust)
.imports
.into_iter()
.map(|import| RawImportContribution {
source: crate::imports::rust_use_tree(&import),
names: import.names,
default_import: None,
namespace_import: None,
})
.collect()
}
pub fn rust_cfg_test_ranges(source: &str, root: tree_sitter::Node) -> Vec<RustCfgTestRange> {
let mut ranges = Vec::new();
let mut stack = vec![(root, false)];
while let Some((node, parent_is_block)) = stack.pop() {
let in_function_body = parent_is_block
&& !matches!(
node.kind(),
"attribute_item" | "inner_attribute_item" | "line_comment" | "block_comment"
);
let item = matches!(
node.kind(),
"mod_item"
| "function_item"
| "impl_item"
| "struct_item"
| "enum_item"
| "union_item"
| "trait_item"
| "type_item"
| "const_item"
| "static_item"
);
if (in_function_body || item) && rust_node_has_cfg_test_attribute(source, node) {
ranges.push(RustCfgTestRange {
start_line: rust_item_start_line(node),
end_line: node.end_position().row as u32 + 1,
});
}
let mut cursor = node.walk();
if cursor.goto_first_child() {
let is_block = node.kind() == "block";
loop {
stack.push((cursor.node(), is_block));
if !cursor.goto_next_sibling() {
break;
}
}
}
}
ranges.sort_by_key(|range| (range.start_line, range.end_line));
ranges.dedup();
ranges
}
pub fn rust_item_start_line(node: tree_sitter::Node<'_>) -> u32 {
let mut first = node;
while let Some(previous) = first.prev_sibling() {
if !matches!(
previous.kind(),
"attribute_item" | "line_comment" | "block_comment"
) {
break;
}
first = previous;
}
first.start_position().row as u32 + 1
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RustCfgTestModule {
Path(String),
Name(String),
}
pub fn rust_cfg_test_modules(source: &str, root: tree_sitter::Node) -> Vec<RustCfgTestModule> {
let mut modules = Vec::new();
let mut cursor = root.walk();
for node in root.children(&mut cursor) {
if node.kind() != "mod_item"
|| node.child_by_field_name("body").is_some()
|| !rust_node_has_cfg_test_attribute(source, node)
{
continue;
}
if let Some(path) = rust_path_attribute_value(source, node) {
modules.push(RustCfgTestModule::Path(path));
continue;
}
let Some(name) = node
.child_by_field_name("name")
.map(|name| node_text(source, name).to_string())
else {
continue;
};
modules.push(RustCfgTestModule::Name(name));
}
modules
}
pub fn rust_cfg_test_module_files_for(
relative_file: &str,
modules: &[RustCfgTestModule],
) -> Vec<String> {
let normalized = relative_file.replace('\\', "/");
let (dir, file_name) = normalized
.rsplit_once('/')
.map_or(("", normalized.as_str()), |(dir, name)| (dir, name));
let module_dir = match file_name.strip_suffix(".rs") {
Some("mod" | "lib" | "main") | None => dir.to_string(),
Some(stem) if dir.is_empty() => stem.to_string(),
Some(stem) => format!("{dir}/{stem}"),
};
let join = |base: &str, rest: &str| {
if base.is_empty() {
rest.to_string()
} else {
format!("{base}/{rest}")
}
};
let mut files = Vec::new();
for module in modules {
match module {
RustCfgTestModule::Path(path) => {
files.push(normalize_relative_segments(&join(dir, path)));
}
RustCfgTestModule::Name(name) => {
files.push(join(&module_dir, &format!("{name}.rs")));
files.push(join(&module_dir, &format!("{name}/mod.rs")));
}
}
}
files
}
pub fn rust_path_attribute_value(source: &str, node: tree_sitter::Node<'_>) -> Option<String> {
let mut previous = node.prev_sibling();
while let Some(attribute) = previous {
match attribute.kind() {
"attribute_item" => {
let text = &source[attribute.byte_range()];
let inner = text
.trim()
.strip_prefix("#[")
.and_then(|inner| inner.strip_suffix(']'))
.map(str::trim);
if let Some(value) = inner
.and_then(|inner| inner.strip_prefix("path"))
.map(str::trim_start)
.and_then(|rest| rest.strip_prefix('='))
.map(str::trim)
.and_then(|rest| rest.strip_prefix('"'))
.and_then(|rest| rest.strip_suffix('"'))
{
return Some(value.replace('\\', "/"));
}
previous = attribute.prev_sibling();
}
"line_comment" | "block_comment" => previous = attribute.prev_sibling(),
_ => break,
}
}
None
}
pub fn normalize_relative_segments(path: &str) -> String {
let mut segments: Vec<&str> = Vec::new();
for segment in path.split(['/', '\\']) {
match segment {
"" | "." => {}
".." => {
segments.pop();
}
other => segments.push(other),
}
}
segments.join("/")
}
pub fn rust_node_has_cfg_test_attribute(source: &str, node: tree_sitter::Node<'_>) -> bool {
let mut previous = node.prev_sibling();
while let Some(attribute) = previous {
match attribute.kind() {
"attribute_item" => {
if rust_attribute_requires_test(source, attribute) {
return true;
}
previous = attribute.prev_sibling();
}
"line_comment" | "block_comment" => previous = attribute.prev_sibling(),
_ => break,
}
}
false
}
pub fn rust_children_cfg_test_gated<'t>(
source: &str,
parent: tree_sitter::Node<'t>,
) -> Vec<(tree_sitter::Node<'t>, bool)> {
let mut children = Vec::new();
let mut run_requires_test = false;
let mut cursor = parent.walk();
for node in parent.children(&mut cursor) {
if node.is_named() {
children.push((node, run_requires_test));
}
match node.kind() {
"attribute_item" => {
run_requires_test = run_requires_test || rust_attribute_requires_test(source, node);
}
"line_comment" | "block_comment" => {}
_ => run_requires_test = false,
}
}
children
}
pub fn rust_attribute_requires_test(source: &str, attribute: tree_sitter::Node<'_>) -> bool {
let compact = source[attribute.byte_range()]
.chars()
.filter(|ch| !ch.is_whitespace())
.collect::<String>();
compact
.strip_prefix("#[cfg(")
.and_then(|inner| inner.strip_suffix(")]"))
.is_some_and(cfg_predicate_requires_test)
}
pub fn cfg_predicate_requires_test(predicate: &str) -> bool {
if predicate == "test" {
return true;
}
if let Some(inner) = predicate
.strip_prefix("all(")
.and_then(|inner| inner.strip_suffix(')'))
{
return split_cfg_predicates(inner)
.into_iter()
.any(cfg_predicate_requires_test);
}
if let Some(inner) = predicate
.strip_prefix("any(")
.and_then(|inner| inner.strip_suffix(')'))
{
let predicates = split_cfg_predicates(inner);
return !predicates.is_empty() && predicates.into_iter().all(cfg_predicate_requires_test);
}
false
}
pub fn split_cfg_predicates(input: &str) -> Vec<&str> {
let mut parts = Vec::new();
let mut depth = 0usize;
let mut start = 0usize;
for (index, ch) in input.char_indices() {
match ch {
'(' => depth += 1,
')' => depth = depth.saturating_sub(1),
',' if depth == 0 => {
parts.push(input[start..index].trim());
start = index + ch.len_utf8();
}
_ => {}
}
}
let tail = input[start..].trim();
if !tail.is_empty() {
parts.push(tail);
}
parts
}
pub fn rust_macro_token_refs(
source: &str,
root: tree_sitter::Node,
) -> Vec<MacroTokenRefContribution> {
let mut refs = BTreeSet::new();
let mut scope_stack = Vec::new();
collect_rust_macro_token_refs(source, root, &mut scope_stack, &mut refs);
let mut functions: BTreeMap<String, Vec<tree_sitter::Node>> = BTreeMap::new();
let mut stack = vec![root];
while let Some(node) = stack.pop() {
if node.kind() == "function_item" {
if let Some(name) = rust_function_symbol_name(source, &node) {
functions.entry(name).or_default().push(node);
}
}
let mut cursor = node.walk();
stack.extend(node.named_children(&mut cursor));
}
for (caller_symbol, variants) in functions {
for node in variants {
for (full, _, line, _, _) in rust_function_body_calls(source, node) {
if full.contains('.') || full.ends_with('!') {
continue;
}
let mut path = rust_path_segments(&full);
let Some(name) = path.pop() else {
continue;
};
refs.insert(MacroTokenRefContribution {
caller_symbol: caller_symbol.clone(),
name,
line,
path: macro_ref_path(path),
shape: RUST_MACRO_REF_SHAPE_CALL.into(),
});
}
}
}
refs.into_iter().collect()
}
pub fn rust_function_body_calls(
source: &str,
function: tree_sitter::Node,
) -> Vec<crate::calls::CallTuple> {
let (calls, facts) = crate::calls::extract_rust_calls_with_macro_facts(
source,
function,
function.start_byte(),
function.end_byte(),
);
if facts.bodies.is_empty() {
return calls;
}
let owner = |start: usize, end: usize| {
facts
.bodies
.iter()
.filter(|body| body.byte_start <= start && end <= body.byte_end)
.min_by_key(|body| body.byte_end - body.byte_start)
.map(|body| body.name.as_str())
};
let mut direct_invocations = Vec::new();
let mut template_invocations: BTreeMap<&str, Vec<String>> = BTreeMap::new();
for (full, _, _, start, end) in &calls {
let Some(path) = full.strip_suffix('!') else {
continue;
};
let name = path.rsplit("::").next().unwrap_or(path).trim().to_string();
match owner(*start, *end) {
Some(macro_name) => template_invocations
.entry(macro_name)
.or_default()
.push(name),
None => direct_invocations.push(name),
}
}
let mut invoked = BTreeSet::new();
let mut pending = direct_invocations;
while let Some(name) = pending.pop() {
if !invoked.insert(name.clone()) {
continue;
}
if let Some(nested) = template_invocations.get(name.as_str()) {
pending.extend(nested.iter().cloned());
}
}
let unresolved_invocation = facts.bodies.iter().any(|body| {
invoked.contains(&body.name)
&& rust_template_token_tree(function, body.byte_start - 1, body.byte_end + 1)
.is_none_or(|tree| {
tree.has_error() || rust_token_tree_invokes_metavariable_macro(tree)
})
});
if unresolved_invocation {
return calls;
}
calls
.into_iter()
.filter(|(_, _, _, start, end)| {
owner(*start, *end).is_none_or(|macro_name| invoked.contains(macro_name))
})
.collect()
}
pub fn rust_template_token_tree(
root: tree_sitter::Node<'_>,
start: usize,
end: usize,
) -> Option<tree_sitter::Node<'_>> {
let mut stack = vec![root];
while let Some(node) = stack.pop() {
let range = node.byte_range();
if range.start > start || range.end < end {
continue;
}
if node.kind() == "token_tree" && range.start == start && range.end == end {
return Some(node);
}
let mut cursor = node.walk();
stack.extend(node.children(&mut cursor));
}
None
}
pub fn rust_token_tree_invokes_metavariable_macro(token_tree: tree_sitter::Node<'_>) -> bool {
let mut stack = vec![token_tree];
while let Some(node) = stack.pop() {
let mut cursor = node.walk();
let tokens = node
.children(&mut cursor)
.filter(|child| !child.is_extra())
.collect::<Vec<_>>();
for (index, token) in tokens.iter().enumerate() {
if token.child_count() > 0 {
stack.push(*token);
}
if token.kind() != "metavariable" {
continue;
}
let mut next = index + 1;
let mut name_end = token.end_byte();
while let Some(part) = tokens
.get(next)
.filter(|part| part.kind() == "identifier" && part.start_byte() == name_end)
{
name_end = part.end_byte();
next += 1;
}
if tokens.get(next).is_some_and(|bang| bang.kind() == "!") {
return true;
}
}
}
false
}
pub fn collect_rust_macro_token_refs(
source: &str,
root: tree_sitter::Node,
scope_stack: &mut Vec<String>,
refs: &mut BTreeSet<MacroTokenRefContribution>,
) {
enum Step<'tree> {
Enter(tree_sitter::Node<'tree>),
Leave(usize),
}
let mut pending = vec![Step::Enter(root)];
while let Some(step) = pending.pop() {
let node = match step {
Step::Enter(node) => node,
Step::Leave(scope_len) => {
scope_stack.truncate(scope_len);
continue;
}
};
pending.push(Step::Leave(scope_stack.len()));
if node.kind() == "function_item" {
if let Some(symbol) = rust_function_symbol_name(source, &node) {
scope_stack.push(symbol);
}
}
if node.kind() == "macro_invocation" {
if let Some(token_tree) = find_child_by_kind(node, "token_tree") {
let caller_symbol = scope_stack
.last()
.cloned()
.unwrap_or_else(|| TOP_LEVEL_SYMBOL.to_string());
let mut tokens = Vec::new();
collect_rust_macro_tokens(source, token_tree, &mut tokens);
let mut shadowed = BTreeSet::new();
for token in &tokens {
if rust_macro_token_is_identifier(token)
&& crate::calls::rust_name_is_shadowed_at(node, source, token.text)
{
shadowed.insert(token.text.to_string());
}
}
let mut binding = false;
for token in &tokens {
if token.text == "let" {
binding = true;
} else if token.text == "=" {
binding = false;
} else if binding && rust_macro_token_is_identifier(token) {
shadowed.insert(token.text.to_string());
}
}
extract_rust_macro_token_refs(&tokens, &caller_symbol, &shadowed, refs);
}
}
let start = pending.len();
let mut cursor = node.walk();
pending.extend(node.children(&mut cursor).map(Step::Enter));
pending[start..].reverse();
}
}
pub fn collect_rust_macro_tokens<'a>(
source: &'a str,
root: tree_sitter::Node,
tokens: &mut Vec<RustMacroToken<'a>>,
) {
let mut cursor = root.walk();
loop {
let node = cursor.node();
if !rust_macro_token_node_is_opaque(node.kind()) {
if node.child_count() == 0 {
let text = node_text(source, node).trim();
if !text.is_empty() {
tokens.push(RustMacroToken {
text,
kind: node.kind(),
line: node.start_position().row as u32 + 1,
});
}
} else if cursor.goto_first_child() {
continue;
}
}
while !cursor.goto_next_sibling() {
if !cursor.goto_parent() {
return;
}
}
}
}
pub fn rust_macro_token_node_is_opaque(kind: &str) -> bool {
matches!(
kind,
"string_literal" | "raw_string_literal" | "char_literal" | "line_comment" | "block_comment"
)
}
pub fn extract_rust_macro_token_refs(
tokens: &[RustMacroToken<'_>],
caller_symbol: &str,
shadowed: &BTreeSet<String>,
refs: &mut BTreeSet<MacroTokenRefContribution>,
) {
for index in 0..tokens.len() {
let token = &tokens[index];
if !rust_macro_token_is_identifier(token) || rust_macro_token_is_keyword(token.text) {
continue;
}
if index > 0 && tokens[index - 1].text == "." {
continue;
}
if tokens.get(index + 1).is_some_and(|next| next.text == "!") {
continue;
}
let path = rust_macro_path_before(tokens, index);
let next = rust_macro_next_after_optional_turbofish(tokens, index + 1);
if index > 0
&& tokens[index - 1].text == "("
&& tokens
.get(next)
.is_some_and(|next| matches!(next.text, ")" | ","))
&& !shadowed.contains(token.text)
{
refs.insert(MacroTokenRefContribution {
caller_symbol: caller_symbol.to_string(),
line: token.line,
name: token.text.to_string(),
path: macro_ref_path(path.clone()),
shape: RUST_MACRO_REF_SHAPE_CALL.into(),
});
}
if tokens.get(next).is_some_and(|next| next.text == "(") {
let shape = if path
.last()
.is_some_and(|segment| rust_macro_name_is_upper_camel(segment))
{
RUST_MACRO_REF_SHAPE_METHOD
} else {
RUST_MACRO_REF_SHAPE_CALL
};
refs.insert(MacroTokenRefContribution {
caller_symbol: caller_symbol.to_string(),
line: token.line,
name: token.text.to_string(),
path: macro_ref_path(path),
shape: shape.to_string(),
});
continue;
}
if rust_macro_name_is_upper_camel(token.text)
&& tokens.get(index + 1).is_some_and(|next| next.text == "{")
{
refs.insert(MacroTokenRefContribution {
caller_symbol: caller_symbol.to_string(),
line: token.line,
name: token.text.to_string(),
path: macro_ref_path(path),
shape: RUST_MACRO_REF_SHAPE_STRUCT.to_string(),
});
}
}
}
pub fn rust_macro_path_before(tokens: &[RustMacroToken<'_>], index: usize) -> Vec<String> {
let mut segments = Vec::new();
let mut cursor = index;
while cursor >= 2
&& tokens[cursor - 1].text == "::"
&& rust_macro_token_is_path_segment(&tokens[cursor - 2])
{
segments.push(tokens[cursor - 2].text.to_string());
cursor -= 2;
}
segments.reverse();
segments
}
pub fn rust_macro_next_after_optional_turbofish(
tokens: &[RustMacroToken<'_>],
index: usize,
) -> usize {
if tokens.get(index).is_none_or(|token| token.text != "::")
|| tokens.get(index + 1).is_none_or(|token| token.text != "<")
{
return index;
}
let mut depth = 0usize;
let mut cursor = index + 1;
while let Some(token) = tokens.get(cursor) {
match token.text {
"<" => depth += 1,
">" => {
depth = depth.saturating_sub(1);
if depth == 0 {
return cursor + 1;
}
}
_ => {}
}
cursor += 1;
}
index
}
pub fn macro_ref_path(path: Vec<String>) -> Option<Vec<String>> {
(!path.is_empty()).then_some(path)
}
pub fn rust_macro_token_is_identifier(token: &RustMacroToken<'_>) -> bool {
matches!(token.kind, "identifier" | "type_identifier")
|| rust_macro_text_is_identifier(token.text)
}
pub fn rust_macro_token_is_path_segment(token: &RustMacroToken<'_>) -> bool {
rust_macro_token_is_identifier(token)
&& (!rust_macro_token_is_keyword(token.text)
|| matches!(token.text, "crate" | "self" | "super"))
}
pub fn rust_macro_text_is_identifier(text: &str) -> bool {
let mut chars = text.chars();
let Some(first) = chars.next() else {
return false;
};
(first == '_' || first.is_ascii_alphabetic())
&& chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric())
}
pub fn rust_macro_name_is_upper_camel(name: &str) -> bool {
name.chars().next().is_some_and(char::is_uppercase)
}
pub fn rust_macro_token_is_keyword(text: &str) -> bool {
matches!(
text,
"as" | "async"
| "await"
| "break"
| "const"
| "continue"
| "crate"
| "dyn"
| "else"
| "enum"
| "extern"
| "false"
| "fn"
| "for"
| "if"
| "impl"
| "in"
| "let"
| "loop"
| "match"
| "mod"
| "move"
| "mut"
| "pub"
| "ref"
| "return"
| "self"
| "Self"
| "static"
| "struct"
| "super"
| "trait"
| "true"
| "type"
| "unsafe"
| "use"
| "where"
| "while"
)
}
pub fn rust_function_symbol_name(
source: &str,
function_node: &tree_sitter::Node<'_>,
) -> Option<String> {
let name_node = function_node.child_by_field_name("name")?;
let name = node_text(source, name_node).to_string();
let declaration_list_owner = rust_function_declaration_list_owner(function_node);
match declaration_list_owner.as_ref().map(tree_sitter::Node::kind) {
Some("impl_item") => {
let scope_name = rust_impl_scope_name(declaration_list_owner.as_ref().unwrap(), source);
if scope_name.is_empty() {
Some(name)
} else {
Some(format!("{scope_name}::{name}"))
}
}
Some(owner_kind) if owner_kind != "mod_item" => None,
_ => {
let scope_chain = rust_mod_scope_chain(function_node, source);
if scope_chain.is_empty() {
Some(name)
} else {
Some(format!("{}::{name}", scope_chain.join("::")))
}
}
}
}
pub fn rust_function_declaration_list_owner<'a>(
function_node: &tree_sitter::Node<'a>,
) -> Option<tree_sitter::Node<'a>> {
function_node
.parent()
.filter(|parent| parent.kind() == "declaration_list")
.and_then(|parent| parent.parent())
}
pub fn rust_mod_scope_chain(node: &tree_sitter::Node<'_>, source: &str) -> Vec<String> {
let mut scopes = Vec::new();
let mut current = node.parent();
while let Some(parent) = current {
if parent.kind() == "mod_item" {
if let Some(name_node) = parent.child_by_field_name("name") {
scopes.push(node_text(source, name_node).to_string());
}
}
current = parent.parent();
}
scopes.reverse();
scopes
}
pub fn rust_impl_scope_name(impl_node: &tree_sitter::Node<'_>, source: &str) -> String {
let Some(self_type) = impl_node.child_by_field_name("type") else {
return String::new();
};
let self_name = node_text(source, self_type);
match impl_node.child_by_field_name("trait") {
Some(trait_type) => format!("{} for {self_name}", node_text(source, trait_type)),
None => self_name.to_string(),
}
}
pub fn ts_raw_reexport_contributions(
source: &str,
root: tree_sitter::Node,
) -> Vec<RawReexportContribution> {
let mut reexports = Vec::new();
let mut cursor = root.walk();
if !cursor.goto_first_child() {
return reexports;
}
loop {
let node = cursor.node();
if node.kind() == "export_statement" {
if let Some(module_path) = export_source_module(source, node) {
let line = (node.start_position().row + 1) as u32;
let raw_export = node_text(source, node).trim();
for specifier in ts_reexport_specifiers(raw_export) {
reexports.push(RawReexportContribution {
language: "ts".to_string(),
source: module_path.clone(),
kind: "named".to_string(),
imported: Some(specifier.imported),
exported: Some(specifier.exported),
line,
});
}
if raw_export.contains('*') {
if let Some(namespace_export) = ts_namespace_reexport_name(raw_export) {
reexports.push(RawReexportContribution {
language: "ts".to_string(),
source: module_path.clone(),
kind: "namespace".to_string(),
imported: Some("*".to_string()),
exported: Some(namespace_export),
line,
});
} else {
reexports.push(RawReexportContribution {
language: "ts".to_string(),
source: module_path.clone(),
kind: "star".to_string(),
imported: Some("*".to_string()),
exported: None,
line,
});
}
}
}
}
if !cursor.goto_next_sibling() {
break;
}
}
reexports
}
pub fn rust_raw_reexport_contributions(source: &str) -> Vec<RawReexportContribution> {
rust_pub_use_statements(source)
.into_iter()
.flat_map(|(statement, line)| {
rust_reexport_specifiers(&statement)
.into_iter()
.map(move |specifier| RawReexportContribution {
language: "rust".to_string(),
source: specifier.module_path.join("::"),
kind: if specifier.imported == "*" {
"star".to_string()
} else {
"named".to_string()
},
imported: Some(specifier.imported),
exported: Some(specifier.exported),
line,
})
})
.collect()
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RustApiFacts {
#[serde(default)]
pub receiver_references: BTreeSet<String>,
#[serde(default)]
pub initializers: Vec<RustInitializer>,
#[serde(default)]
pub initializer_references: Vec<MacroTokenRefContribution>,
pub items: Vec<RustApiItem>,
pub modules: Vec<RustApiModule>,
pub uses: Vec<RustApiUse>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RustInitializer {
pub name: String,
pub range: RustCfgTestRange,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RustApiItem {
pub name: String,
pub scope: Vec<String>,
pub public: bool,
pub line: u32,
pub owner: Option<String>,
pub trait_name: Option<String>,
}
pub fn rust_qualified_item_name(item: &RustApiItem) -> String {
let mut parts = item.scope.clone();
if let Some(owner) = &item.owner {
parts.push(match &item.trait_name {
Some(trait_name) => format!("{trait_name} for {owner}"),
None => owner.clone(),
});
}
parts.push(item.name.clone());
parts.join("::")
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RustApiModule {
pub scope: Vec<String>,
pub name: String,
pub public: bool,
pub file: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path_override: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RustApiUse {
pub scope: Vec<String>,
pub path: Vec<String>,
pub alias: String,
pub public: bool,
pub line: u32,
}
pub fn rust_unrestricted_pub(source: &str, node: tree_sitter::Node) -> bool {
find_child_by_kind(node, "visibility_modifier")
.is_some_and(|visibility| node_text(source, visibility).trim() == "pub")
}
pub fn rust_api_facts(source: &str, root: tree_sitter::Node, file: &str) -> RustApiFacts {
let mut facts = RustApiFacts::default();
rust_collect_api_facts(source, root, file, &[], &mut facts, 0);
let mut owners = Vec::new();
let mut stack = vec![root];
while let Some(node) = stack.pop() {
let owner = match node.kind() {
"function_item" => rust_function_symbol_name(source, &node),
"static_item" | "const_item" => node.child_by_field_name("name").map(|name| {
macro_scoped_symbol(
&rust_mod_scope_chain(&node, source),
node_text(source, name),
)
}),
_ => None,
};
if let Some(owner) = owner {
owners.push((node.start_byte(), node.end_byte(), owner.clone()));
if matches!(node.kind(), "static_item" | "const_item") {
facts.initializers.push(RustInitializer {
name: owner,
range: RustCfgTestRange {
start_line: rust_item_start_line(node),
end_line: node.end_position().row as u32 + 1,
},
});
}
}
let mut cursor = node.walk();
stack.extend(node.named_children(&mut cursor));
}
facts.initializer_references = crate::calls::extract_rust_value_references(source, root)
.into_iter()
.map(|(full, name, line, start, end)| {
let mut path = rust_path_segments(&full);
path.pop();
let caller_symbol = owners
.iter()
.filter(|(a, b, _)| *a <= start && *b >= end)
.min_by_key(|(a, b, _)| b - a)
.map(|(_, _, name)| name.clone())
.unwrap_or_else(|| "<top-level>".into());
MacroTokenRefContribution {
caller_symbol,
name,
line,
path: macro_ref_path(path),
shape: "call".into(),
}
})
.collect();
facts
}
pub const MAX_RUST_API_NESTING: usize = 64;
pub fn rust_collect_api_facts(
source: &str,
root: tree_sitter::Node,
file: &str,
scope: &[String],
facts: &mut RustApiFacts,
depth: usize,
) {
if depth > MAX_RUST_API_NESTING {
return;
}
let gated = rust_children_cfg_test_gated(source, root);
for (node, gated) in gated {
if gated {
continue;
}
if !matches!(node.kind(), "impl_item" | "mod_item") {
facts
.receiver_references
.extend(extract_type_references(source, node, LangId::Rust));
}
let name = node
.child_by_field_name("name")
.map(|name| node_text(source, name).to_string());
let public = rust_unrestricted_pub(source, node);
match node.kind() {
"mod_item" => {
let Some(name) = name else {
continue;
};
let body = node.child_by_field_name("body");
let path_override = rust_path_attribute_value(source, node);
let mut nested = scope.to_vec();
nested.push(name.clone());
let module_file = body
.is_none()
.then(|| rust_module_file(file, &nested, path_override.as_deref()));
facts.modules.push(RustApiModule {
scope: scope.to_vec(),
name,
public,
file: module_file,
path_override,
});
if let Some(body) = body {
rust_collect_api_facts(source, body, file, &nested, facts, depth + 1);
}
}
"use_declaration" => {
let text = node_text(source, node);
let Some((_, path)) = text.split_once("use ") else {
continue;
};
rust_api_use_paths(
path.trim().trim_end_matches(';'),
&[],
&mut |path, alias| {
facts.uses.push(RustApiUse {
scope: scope.to_vec(),
path,
alias,
public,
line: node.start_position().row as u32 + 1,
});
},
);
}
"impl_item" => {
let owner = node
.child_by_field_name("type")
.map(|ty| rust_api_type_path(node_text(source, ty)));
let trait_name = node
.child_by_field_name("trait")
.map(|ty| rust_api_type_path(node_text(source, ty)));
if let Some(body) = node.child_by_field_name("body") {
let mut cursor = body.walk();
for method in body
.named_children(&mut cursor)
.filter(|item| item.kind() == "function_item")
{
if rust_node_has_cfg_test_attribute(source, method) {
continue;
}
if let Some(name) = method.child_by_field_name("name") {
facts.items.push(RustApiItem {
name: node_text(source, name).to_string(),
scope: scope.to_vec(),
public: rust_unrestricted_pub(source, method)
|| trait_name.is_some(),
line: method.start_position().row as u32 + 1,
owner: owner.clone(),
trait_name: trait_name.clone(),
});
}
}
}
}
"function_item" | "struct_item" | "enum_item" | "trait_item" | "type_item"
| "const_item" | "static_item" => {
if let Some(name) = name {
facts.items.push(RustApiItem {
name,
scope: scope.to_vec(),
public,
line: node.start_position().row as u32 + 1,
owner: None,
trait_name: None,
});
}
}
"macro_invocation" => {
if let Some(tokens) = find_child_by_kind(node, "token_tree") {
rust_collect_api_macro_items(source, tokens, file, scope, facts, depth + 1);
}
if let Some(name) = node.child_by_field_name("macro") {
let mut cursor = root.walk();
for definition in root
.named_children(&mut cursor)
.filter(|n| n.kind() == "macro_definition")
{
if definition
.child_by_field_name("name")
.is_some_and(|n| node_text(source, n) == node_text(source, name))
{
let mut cursor = definition.walk();
for rule in definition
.named_children(&mut cursor)
.filter(|n| n.kind() == "macro_rule")
{
if let (Some(left), Some(right), Some(input)) = (
rule.child_by_field_name("left"),
rule.child_by_field_name("right"),
find_child_by_kind(node, "token_tree"),
) {
if let Some(expanded) = rust_api_macro_expansion(
node_text(source, left),
node_text(source, right),
node_text(source, input),
) {
rust_collect_api_macro_text(
&expanded,
right.start_position().row,
file,
scope,
facts,
depth + 1,
);
}
}
}
}
}
}
}
_ => {}
}
}
}
pub fn rust_module_file(file: &str, nested: &[String], path_override: Option<&str>) -> String {
let base = Path::new(file);
let dir = base.parent().unwrap_or(Path::new(""));
let stem = base.file_stem().and_then(|s| s.to_str()).unwrap_or("");
let dir = if matches!(stem, "lib" | "main" | "mod") {
dir.to_path_buf()
} else {
dir.join(stem)
};
if let Some(path) = path_override {
normalize_relative_segments(
&base
.parent()
.unwrap_or(Path::new(""))
.join(path)
.to_string_lossy(),
)
} else {
dir.join(nested.join("/"))
.with_extension("rs")
.to_string_lossy()
.replace('\\', "/")
}
}
pub fn rust_collect_api_macro_items(
source: &str,
tokens: tree_sitter::Node,
file: &str,
scope: &[String],
facts: &mut RustApiFacts,
depth: usize,
) {
let text = node_text(source, tokens);
if text.len() < 2 {
return;
}
rust_collect_api_macro_text(
&text[1..text.len() - 1],
tokens.start_position().row,
file,
scope,
facts,
depth,
);
}
pub fn rust_api_macro_expansion(pattern: &str, template: &str, input: &str) -> Option<String> {
let interior = |text: &str| {
text.get(1..text.len().checked_sub(1)?)
.map(str::trim)
.map(str::to_string)
};
let pattern = interior(pattern)?;
let input = interior(input)?;
let mut template = interior(template)?;
if !pattern.contains('$') {
return (pattern == input).then_some(template);
}
let patterns = split_cfg_predicates(&pattern);
let inputs = split_cfg_predicates(&input);
if patterns.len() != inputs.len() {
return None;
}
for (pattern, input) in patterns.into_iter().zip(inputs) {
let (name, kind) = pattern.trim().strip_prefix('$')?.split_once(':')?;
if !matches!(kind.trim(), "ident" | "path")
|| !name.chars().all(|ch| ch.is_alphanumeric() || ch == '_')
{
return None;
}
if !input
.trim()
.chars()
.all(|ch| ch.is_alphanumeric() || ch == '_' || ch == ':')
{
return None;
}
template = template.replace(&format!("${}", name.trim()), input.trim());
}
(!template.contains('$')).then_some(template)
}
pub fn rust_collect_api_macro_text(
interior: &str,
row: usize,
file: &str,
scope: &[String],
facts: &mut RustApiFacts,
depth: usize,
) {
if interior.contains('$') {
return;
}
let padded = format!("{}{}", "\n".repeat(row), interior);
let mut analyzer = FactParsers::default();
if let Some(tree) = analyzer.parse_source(LangId::Rust, &padded) {
if !tree.root_node().has_error() {
rust_collect_api_facts(&padded, tree.root_node(), file, scope, facts, depth);
}
}
}
pub fn rust_api_type_path(text: &str) -> String {
text.split('<').next().unwrap_or(text).trim().to_string()
}
pub const MAX_RUST_USE_GROUP_NESTING: usize = 64;
pub fn rust_api_use_paths(
text: &str,
prefix: &[String],
emit: &mut impl FnMut(Vec<String>, String),
) {
rust_api_use_paths_at(text, prefix, emit, 0);
}
pub fn rust_api_use_paths_at(
text: &str,
prefix: &[String],
emit: &mut impl FnMut(Vec<String>, String),
depth: usize,
) {
let text = text.trim();
if let Some(open) = text.find('{') {
if depth >= MAX_RUST_USE_GROUP_NESTING {
return;
}
let mut prefix = prefix.to_vec();
prefix.extend(rust_path_segments(text[..open].trim_end_matches(':')));
let inner = text[open + 1..].trim_end_matches('}');
let mut depth_in_group = 0;
let mut start = 0;
for (index, ch) in inner.char_indices() {
match ch {
'{' => depth_in_group += 1,
'}' => depth_in_group -= 1,
',' if depth_in_group == 0 => {
rust_api_use_paths_at(&inner[start..index], &prefix, emit, depth + 1);
start = index + 1;
}
_ => {}
}
}
rust_api_use_paths_at(&inner[start..], &prefix, emit, depth + 1);
} else if !text.is_empty() {
let (path, alias) = text
.split_once(" as ")
.map(|(path, alias)| (path.trim(), Some(alias.trim())))
.unwrap_or((text, None));
let mut segments = prefix.to_vec();
segments.extend(rust_path_segments(path));
if segments.last().is_some_and(|s| s == "self") {
segments.pop();
}
if let Some(name) = alias.or_else(|| segments.last().map(String::as_str)) {
emit(segments.clone(), name.to_string());
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GoDispatchFacts {
#[serde(default)]
pub module: String,
pub methods: Vec<GoMethodFact>,
pub interfaces: Vec<Vec<String>>,
pub used_types: BTreeSet<String>,
#[serde(default)]
pub live_methods: BTreeSet<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GoMethodFact {
pub receiver: String,
pub name: String,
pub signature: String,
}
pub fn go_dispatch_facts(source: &str, root: tree_sitter::Node) -> GoDispatchFacts {
let mut facts = GoDispatchFacts::default();
let mut stack = vec![root];
while let Some(node) = stack.pop() {
if node.kind() == "method_declaration" {
if let (Some(receiver), Some(name)) = (
node.child_by_field_name("receiver"),
node.child_by_field_name("name"),
) {
let mut cursor = receiver.walk();
if let Some(parameter) = receiver.named_children(&mut cursor).next() {
if let Some(ty) = parameter.child_by_field_name("type") {
facts.methods.push(GoMethodFact {
receiver: node_text(source, ty).trim_start_matches('*').to_string(),
name: node_text(source, name).to_string(),
signature: go_method_signature(source, node),
});
}
}
let mut cursor = node.walk();
stack.extend(
node.named_children(&mut cursor)
.filter(|child| child.id() != receiver.id()),
);
continue;
}
}
if node.kind() == "interface_type" {
let mut cursor = node.walk();
let methods = node
.named_children(&mut cursor)
.filter(|child| child.kind() == "method_elem")
.map(|method| go_method_signature(source, method))
.collect::<Vec<_>>();
let mut cursor = node.walk();
if !methods.is_empty()
&& !node
.named_children(&mut cursor)
.any(|child| child.kind() == "type_elem")
{
facts.interfaces.push(methods);
}
continue;
}
if node.kind() == "type_identifier"
&& !node.parent().is_some_and(|parent| {
parent.kind() == "type_spec"
&& parent
.child_by_field_name("name")
.is_some_and(|name| name.id() == node.id())
})
{
facts.used_types.insert(node_text(source, node).to_string());
}
let mut cursor = node.walk();
stack.extend(node.named_children(&mut cursor));
}
facts
}
pub fn go_method_signature(source: &str, method: tree_sitter::Node) -> String {
let name = method
.child_by_field_name("name")
.map(|name| node_text(source, name))
.unwrap_or("");
let parameters = method
.child_by_field_name("parameters")
.map(|node| go_signature_types(source, node))
.unwrap_or_default();
let result = method
.child_by_field_name("result")
.map(|node| go_signature_types(source, node))
.unwrap_or_default();
format!("{name}({parameters})({result})")
}
pub fn go_signature_types(source: &str, node: tree_sitter::Node) -> String {
if node.kind() != "parameter_list" {
return node_text(source, node)
.split_whitespace()
.collect::<String>();
}
let mut types = Vec::new();
let mut cursor = node.walk();
for parameter in node.named_children(&mut cursor) {
let Some(ty) = parameter.child_by_field_name("type") else {
continue;
};
let mut cursor = parameter.walk();
let count = parameter
.named_children(&mut cursor)
.filter(|child| child.kind() == "identifier")
.count()
.max(1);
let ty = format!(
"{}{}",
if parameter.kind() == "variadic_parameter_declaration" {
"..."
} else {
""
},
node_text(source, ty).split_whitespace().collect::<String>()
);
types.extend(std::iter::repeat_n(ty, count));
}
types.join(",")
}
pub fn ts_reexport_specifiers(raw_export: &str) -> Vec<ReexportSpecifier> {
let Some(start) = raw_export.find('{').map(|index| index + 1) else {
return Vec::new();
};
let Some(end) = raw_export[start..].find('}').map(|index| start + index) else {
return Vec::new();
};
raw_export[start..end]
.split(',')
.filter_map(|specifier| {
let specifier = specifier.trim();
if specifier.is_empty() {
return None;
}
let imported = specifier_imported_name(specifier).trim();
let exported = specifier_local_name(specifier).trim();
if imported.is_empty() || exported.is_empty() {
return None;
}
Some(ReexportSpecifier {
imported: imported.to_string(),
exported: exported.to_string(),
})
})
.collect()
}
pub fn ts_namespace_reexport_name(raw_export: &str) -> Option<String> {
let after_star = raw_export.split_once('*')?.1.trim_start();
let after_as = after_star.strip_prefix("as")?.trim_start();
let name = after_as
.split_whitespace()
.next()?
.trim_matches(|ch: char| ch == '{' || ch == '}' || ch == ';' || ch == ',');
(!name.is_empty()).then(|| name.to_string())
}
pub fn rust_pub_use_statements(source: &str) -> Vec<(String, u32)> {
let mut statements = Vec::new();
let mut current = String::new();
let mut start_line = 0u32;
for (index, line) in source.lines().enumerate() {
let trimmed = line.trim();
if current.is_empty() {
if !(trimmed.starts_with("pub use ") || trimmed.starts_with("pub(crate) use ")) {
continue;
}
start_line = (index + 1) as u32;
}
current.push(' ');
current.push_str(trimmed);
if trimmed.ends_with(';') {
statements.push((current.trim().to_string(), start_line));
current.clear();
}
}
statements
}
pub fn rust_reexport_specifiers(statement: &str) -> Vec<RustReexportSpecifier> {
let statement = statement
.trim()
.trim_end_matches(';')
.strip_prefix("pub(crate) use ")
.or_else(|| {
statement
.trim()
.trim_end_matches(';')
.strip_prefix("pub use ")
})
.unwrap_or("")
.trim();
if statement.is_empty() {
return Vec::new();
}
if let Some((module_path, grouped)) = statement.split_once("::{") {
let grouped = grouped.trim_end_matches('}');
return grouped
.split(',')
.filter_map(|specifier| rust_reexport_specifier(module_path.trim(), specifier.trim()))
.collect();
}
let Some((module_path, imported)) = statement.rsplit_once("::") else {
return Vec::new();
};
rust_reexport_specifier(module_path.trim(), imported.trim())
.into_iter()
.collect()
}
pub fn rust_reexport_specifier(
module_path: &str,
specifier: &str,
) -> Option<RustReexportSpecifier> {
if specifier.is_empty() {
return None;
}
let (imported, exported) = specifier
.split_once(" as ")
.map(|(imported, exported)| (imported.trim(), exported.trim()))
.unwrap_or((specifier.trim(), specifier.trim()));
if imported.is_empty() || exported.is_empty() {
return None;
}
Some(RustReexportSpecifier {
module_path: rust_normalize_module_path(module_path),
imported: imported.to_string(),
exported: exported.to_string(),
})
}
pub fn rust_normalize_module_path(module_path: &str) -> Vec<String> {
module_path
.split("::")
.filter_map(|segment| {
let segment = segment.trim();
if segment.is_empty() || matches!(segment, "self" | "crate") {
None
} else {
Some(segment.to_string())
}
})
.collect()
}
pub fn export_source_module(source: &str, node: tree_sitter::Node) -> Option<String> {
node.child_by_field_name("source")
.or_else(|| find_child_by_kind(node, "string"))
.and_then(|source_node| string_literal_content(source, source_node))
}
pub fn find_child_by_kind<'tree>(
node: tree_sitter::Node<'tree>,
kind: &str,
) -> Option<tree_sitter::Node<'tree>> {
let mut cursor = node.walk();
if !cursor.goto_first_child() {
return None;
}
loop {
if cursor.node().kind() == kind {
return Some(cursor.node());
}
if cursor.goto_first_child() {
continue;
}
while !cursor.goto_next_sibling() {
if !cursor.goto_parent() || cursor.node().id() == node.id() {
return None;
}
}
}
}
pub fn string_literal_content(source: &str, node: tree_sitter::Node) -> Option<String> {
let raw = node_text(source, node).trim();
let quote = raw.chars().next()?;
if quote != '\'' && quote != '"' {
return None;
}
raw.strip_prefix(quote)
.and_then(|value| value.strip_suffix(quote))
.map(ToOwned::to_owned)
}
pub fn node_text<'a>(source: &'a str, node: tree_sitter::Node) -> &'a str {
&source[node.byte_range()]
}
#[derive(Debug)]
pub struct ReexportSpecifier {
pub imported: String,
pub exported: String,
}
#[derive(Debug)]
pub struct RustReexportSpecifier {
pub module_path: Vec<String>,
pub imported: String,
pub exported: String,
}
pub fn supports_type_refs(lang: LangId) -> bool {
matches!(
lang,
LangId::TypeScript
| LangId::Tsx
| LangId::JavaScript
| LangId::Python
| LangId::Rust
| LangId::Go
)
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RawImportContribution {
pub source: String,
#[serde(default)]
pub names: Vec<String>,
#[serde(default)]
pub default_import: Option<String>,
#[serde(default)]
pub namespace_import: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RawReexportContribution {
pub language: String,
pub source: String,
pub kind: String,
#[serde(default)]
pub imported: Option<String>,
#[serde(default)]
pub exported: Option<String>,
pub line: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct MacroTokenRefContribution {
pub caller_symbol: String,
pub line: u32,
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path: Option<Vec<String>>,
pub shape: String,
}