use std::time::{Instant, SystemTime, UNIX_EPOCH};
use tree_sitter::{Node as TsNode, Parser, Tree};
use crate::extraction::complexity::{count_complexity, ComplexityConfig};
use crate::types::{
generate_node_id, Edge, EdgeKind, ExtractionResult, Node, NodeKind, UnresolvedRef, Visibility,
};
pub static VBNET_COMPLEXITY: ComplexityConfig = ComplexityConfig {
branch_types: &[
"if_statement",
"select_case_statement",
"case_clause",
"catch_clause",
],
loop_types: &[
"for_statement",
"for_each_statement",
"while_statement",
"do_statement",
],
return_types: &[
"return_statement",
"exit_statement",
"continue_statement",
"throw_statement",
],
nesting_types: &["statement"],
unsafe_types: &[],
unchecked_types: &[],
unchecked_methods: &[],
call_expression_types: &["invocation"],
call_method_field: "target",
assertion_names: &["Assert", "AreEqual", "IsTrue", "IsFalse"],
macro_invocation_types: &[],
};
pub struct VbNetExtractor;
struct ExtractionState {
nodes: Vec<Node>,
edges: Vec<Edge>,
unresolved_refs: Vec<UnresolvedRef>,
errors: Vec<String>,
node_stack: Vec<(String, String)>,
file_path: String,
source: Vec<u8>,
timestamp: u64,
class_depth: usize,
}
impl ExtractionState {
fn new(file_path: &str, source: &str) -> Self {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
Self {
nodes: Vec::new(),
edges: Vec::new(),
unresolved_refs: Vec::new(),
errors: Vec::new(),
node_stack: Vec::new(),
file_path: file_path.to_string(),
source: source.as_bytes().to_vec(),
timestamp,
class_depth: 0,
}
}
fn qualified_prefix(&self) -> String {
let mut parts = vec![self.file_path.clone()];
for (name, _) in &self.node_stack {
parts.push(name.clone());
}
parts.join("::")
}
fn parent_node_id(&self) -> Option<&str> {
self.node_stack.last().map(|(_, id)| id.as_str())
}
fn node_text(&self, node: TsNode<'_>) -> String {
node.utf8_text(&self.source)
.unwrap_or("<invalid utf8>")
.to_string()
}
}
impl VbNetExtractor {
pub fn extract_vbnet(file_path: &str, source: &str) -> ExtractionResult {
let start = Instant::now();
let mut state = ExtractionState::new(file_path, source);
let tree = match Self::parse_source(source) {
Ok(tree) => tree,
Err(msg) => {
state.errors.push(msg);
return Self::build_result(state, start);
}
};
let file_node = Node {
id: generate_node_id(file_path, &NodeKind::File, file_path, 0),
kind: NodeKind::File,
name: file_path.to_string(),
qualified_name: file_path.to_string(),
file_path: file_path.to_string(),
start_line: 0,
end_line: source.lines().count().saturating_sub(1) as u32,
start_column: 0,
end_column: 0,
signature: None,
docstring: None,
visibility: Visibility::Pub,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
let file_node_id = file_node.id.clone();
state.nodes.push(file_node);
state.node_stack.push((file_path.to_string(), file_node_id));
let root = tree.root_node();
Self::visit_children(&mut state, root);
state.node_stack.pop();
Self::build_result(state, start)
}
fn parse_source(source: &str) -> Result<Tree, String> {
let mut parser = Parser::new();
let language = crate::extraction::ts_provider::language("vbnet");
parser
.set_language(&language)
.map_err(|e| format!("failed to load VB.NET grammar: {e}"))?;
parser
.parse(source, None)
.ok_or_else(|| "tree-sitter parse returned None".to_string())
}
fn visit_children(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
Self::visit_node(state, child);
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_node(state: &mut ExtractionState, node: TsNode<'_>) {
match node.kind() {
"imports_statement" => Self::visit_imports(state, node),
"type_declaration" => Self::visit_type_declaration(state, node),
"ERROR" => Self::visit_error_node(state, node),
_ => {
Self::visit_children(state, node);
}
}
}
fn visit_type_declaration(state: &mut ExtractionState, node: TsNode<'_>) {
let docstring = Self::extract_xml_docstring(state, node);
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
match child.kind() {
"class_block" => Self::visit_class(state, child, docstring.clone()),
"structure_block" => Self::visit_struct(state, child, docstring.clone()),
"interface_block" => Self::visit_interface(state, child, docstring.clone()),
"enum_block" => Self::visit_enum(state, child, docstring.clone()),
"module_block" => Self::visit_module(state, child, docstring.clone()),
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_error_node(state: &mut ExtractionState, node: TsNode<'_>) {
let text = state.node_text(node);
let trimmed = text.trim();
if trimmed.starts_with("Const ") || trimmed.starts_with("Public Const ")
|| trimmed.starts_with("Private Const ")
{
let after_const = if let Some(rest) = trimmed.strip_prefix("Public Const ") {
rest
} else if let Some(rest) = trimmed.strip_prefix("Private Const ") {
rest
} else {
trimmed.strip_prefix("Const ").unwrap_or(trimmed)
};
let name = after_const
.split_whitespace()
.next()
.unwrap_or("<anonymous>")
.to_string();
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Const, &name, start_line);
let docstring = Self::extract_xml_docstring(state, node);
let visibility = if trimmed.starts_with("Private ") {
Visibility::Private
} else if trimmed.starts_with("Public ") {
Visibility::Pub
} else {
Visibility::Private
};
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Const,
name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(trimmed.to_string()),
docstring,
visibility,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
}
fn visit_imports(state: &mut ExtractionState, node: TsNode<'_>) {
let path = node
.child_by_field_name("namespace")
.map(|n| state.node_text(n))
.unwrap_or_else(|| {
let text = state.node_text(node);
text.trim()
.strip_prefix("Imports ")
.unwrap_or(&text)
.trim()
.to_string()
});
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), path);
let id = generate_node_id(&state.file_path, &NodeKind::Use, &path, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Use,
name: path.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(state.node_text(node).trim().to_string()),
docstring: None,
visibility: Visibility::Private,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
state.unresolved_refs.push(UnresolvedRef {
from_node_id: id,
reference_name: path,
reference_kind: EdgeKind::Uses,
line: start_line,
column: start_column,
file_path: state.file_path.clone(),
});
}
fn visit_class(state: &mut ExtractionState, node: TsNode<'_>, docstring: Option<String>) {
let name = Self::extract_block_name(state, node).unwrap_or_else(|| "<anonymous>".to_string());
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let kind = if state.class_depth > 0 {
NodeKind::InnerClass
} else {
NodeKind::Class
};
let id = generate_node_id(&state.file_path, &kind, &name, start_line);
let signature = Self::extract_block_signature(state, node, "Class");
let graph_node = Node {
id: id.clone(),
kind,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature,
docstring,
visibility: Visibility::Pub,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
if let Some(type_decl) = node.parent() {
Self::extract_annotations_from_prev_siblings(state, type_decl, &id);
}
Self::extract_inherits_implements(state, node, &id);
state.node_stack.push((name, id));
state.class_depth += 1;
Self::visit_block_children(state, node);
state.class_depth -= 1;
state.node_stack.pop();
}
fn visit_struct(state: &mut ExtractionState, node: TsNode<'_>, docstring: Option<String>) {
let name = Self::extract_block_name(state, node).unwrap_or_else(|| "<anonymous>".to_string());
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Struct, &name, start_line);
let signature = Self::extract_block_signature(state, node, "Structure");
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Struct,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature,
docstring,
visibility: Visibility::Pub,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
state.node_stack.push((name, id));
state.class_depth += 1;
Self::visit_block_children(state, node);
state.class_depth -= 1;
state.node_stack.pop();
}
fn visit_interface(state: &mut ExtractionState, node: TsNode<'_>, docstring: Option<String>) {
let name = Self::extract_block_name(state, node).unwrap_or_else(|| "<anonymous>".to_string());
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Interface, &name, start_line);
let signature = Self::extract_block_signature(state, node, "Interface");
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Interface,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature,
docstring,
visibility: Visibility::Pub,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
state.node_stack.push((name, id));
state.class_depth += 1;
Self::visit_block_children(state, node);
state.class_depth -= 1;
state.node_stack.pop();
}
fn visit_enum(state: &mut ExtractionState, node: TsNode<'_>, docstring: Option<String>) {
let name = Self::extract_block_name(state, node).unwrap_or_else(|| "<anonymous>".to_string());
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Enum, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Enum,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(format!("Enum {}", name)),
docstring,
visibility: Visibility::Pub,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
state.node_stack.push((name, id));
Self::extract_enum_members(state, node);
state.node_stack.pop();
}
fn visit_module(state: &mut ExtractionState, node: TsNode<'_>, docstring: Option<String>) {
let name = Self::extract_block_name(state, node).unwrap_or_else(|| "<anonymous>".to_string());
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Module, &name, start_line);
let signature = Self::extract_block_signature(state, node, "Module");
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Module,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature,
docstring,
visibility: Visibility::Pub,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
state.node_stack.push((name, id));
state.class_depth += 1;
Self::visit_block_children(state, node);
state.class_depth -= 1;
state.node_stack.pop();
}
fn visit_block_children(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
match child.kind() {
"method_declaration" => Self::visit_method(state, child),
"constructor_declaration" => Self::visit_constructor(state, child),
"property_declaration" => Self::visit_property(state, child),
"field_declaration" => Self::visit_field(state, child),
"type_declaration" => Self::visit_type_declaration(state, child),
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_method(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::extract_name(state, node).unwrap_or_else(|| "<anonymous>".to_string());
let visibility = Self::extract_vbnet_visibility(node, state);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let kind = if state.class_depth > 0 {
NodeKind::Method
} else {
NodeKind::Function
};
let id = generate_node_id(&state.file_path, &kind, &name, start_line);
let metrics = count_complexity(node, &VBNET_COMPLEXITY, &state.source);
let signature = Self::extract_method_signature(state, node);
let graph_node = Node {
id: id.clone(),
kind,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature,
docstring: None,
visibility,
is_async: false,
branches: metrics.branches,
loops: metrics.loops,
returns: metrics.returns,
max_nesting: metrics.max_nesting,
unsafe_blocks: metrics.unsafe_blocks,
unchecked_calls: metrics.unchecked_calls,
assertions: metrics.assertions,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
Self::extract_annotations_from_children(state, node, &id);
Self::extract_call_sites_from_children(state, node, &id);
}
fn visit_constructor(state: &mut ExtractionState, node: TsNode<'_>) {
let name = "New".to_string();
let visibility = Self::extract_vbnet_visibility(node, state);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Constructor, &name, start_line);
let metrics = count_complexity(node, &VBNET_COMPLEXITY, &state.source);
let sig_text = state.node_text(node);
let signature = sig_text
.lines()
.next()
.map(|l| l.trim().to_string());
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Constructor,
name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature,
docstring: None,
visibility,
is_async: false,
branches: metrics.branches,
loops: metrics.loops,
returns: metrics.returns,
max_nesting: metrics.max_nesting,
unsafe_blocks: metrics.unsafe_blocks,
unchecked_calls: metrics.unchecked_calls,
assertions: metrics.assertions,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
Self::extract_call_sites_from_children(state, node, &id);
}
fn visit_property(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::extract_name(state, node).unwrap_or_else(|| "<anonymous>".to_string());
let visibility = Self::extract_vbnet_visibility(node, state);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Property, &name, start_line);
let type_str = Self::extract_as_clause_type(state, node);
let sig = if let Some(t) = &type_str {
format!("Property {} As {}", name, t)
} else {
format!("Property {}", name)
};
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Property,
name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(sig),
docstring: None,
visibility,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn visit_field(state: &mut ExtractionState, node: TsNode<'_>) {
let text = state.node_text(node);
let trimmed = text.trim();
if trimmed.starts_with("Inherits") || trimmed.starts_with("erializable")
|| trimmed.starts_with("Implements")
{
return;
}
let visibility = Self::extract_vbnet_visibility(node, state);
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "variable_declarator" {
let field_name = child
.child_by_field_name("name")
.map(|n| state.node_text(n))
.unwrap_or_else(|| {
let mut inner = child.walk();
if inner.goto_first_child() {
loop {
let ic = inner.node();
if ic.kind() == "identifier" {
return state.node_text(ic);
}
if !inner.goto_next_sibling() {
break;
}
}
}
state.node_text(child)
});
if field_name == "Inherits" || field_name.starts_with("erializable") {
continue;
}
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), field_name);
let id = generate_node_id(
&state.file_path,
&NodeKind::Field,
&field_name,
start_line,
);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Field,
name: field_name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(trimmed.to_string()),
docstring: None,
visibility: visibility.clone(),
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn extract_enum_members(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "enum_member" {
Self::extract_single_enum_member(state, child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn extract_single_enum_member(state: &mut ExtractionState, node: TsNode<'_>) {
let name = node
.child_by_field_name("name")
.map(|n| state.node_text(n))
.unwrap_or_else(|| {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "identifier" {
return state.node_text(child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
"<anonymous>".to_string()
});
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::EnumVariant, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::EnumVariant,
name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(state.node_text(node).trim().to_string()),
docstring: None,
visibility: Visibility::Pub,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn extract_block_name(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
if let Some(name_node) = node.child_by_field_name("name") {
return Some(state.node_text(name_node));
}
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "identifier" {
return Some(state.node_text(child));
}
if !cursor.goto_next_sibling() {
break;
}
}
}
None
}
fn extract_name(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
if let Some(name_node) = node.child_by_field_name("name") {
return Some(state.node_text(name_node));
}
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "identifier" {
return Some(state.node_text(child));
}
if !cursor.goto_next_sibling() {
break;
}
}
}
None
}
fn extract_block_signature(state: &ExtractionState, node: TsNode<'_>, keyword: &str) -> Option<String> {
let text = state.node_text(node);
let first_line = text.lines().next().unwrap_or("").trim();
if first_line.is_empty() {
Some(format!("{} {}", keyword, Self::extract_block_name(state, node).unwrap_or_default()))
} else {
Some(first_line.to_string())
}
}
fn extract_method_signature(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
let text = state.node_text(node);
text.lines().next().map(|l| l.trim().to_string())
}
fn extract_as_clause_type(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "as_clause" {
if let Some(type_node) = child.child_by_field_name("type") {
return Some(state.node_text(type_node));
}
let mut inner = child.walk();
if inner.goto_first_child() {
loop {
let ic = inner.node();
if ic.kind() == "type" {
return Some(state.node_text(ic));
}
if !inner.goto_next_sibling() {
break;
}
}
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
None
}
fn extract_vbnet_visibility(node: TsNode<'_>, state: &ExtractionState) -> Visibility {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "modifiers" {
let mut inner = child.walk();
if inner.goto_first_child() {
loop {
let mc = inner.node();
if mc.kind() == "modifier" {
let text = state.node_text(mc);
match text.as_str() {
"Public" => return Visibility::Pub,
"Private" => return Visibility::Private,
"Friend" => return Visibility::PubCrate,
"Protected" => return Visibility::PubSuper,
_ => {}
}
}
if !inner.goto_next_sibling() {
break;
}
}
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
Visibility::Private
}
fn extract_inherits_implements(
state: &mut ExtractionState,
node: TsNode<'_>,
class_id: &str,
) {
let text = state.node_text(node);
let base_line = node.start_position().row as u32;
for (i, line) in text.lines().enumerate() {
let trimmed = line.trim();
if let Some(base_type) = trimmed.strip_prefix("Inherits ") {
let base_type = base_type.trim();
if !base_type.is_empty() {
state.unresolved_refs.push(UnresolvedRef {
from_node_id: class_id.to_string(),
reference_name: base_type.to_string(),
reference_kind: EdgeKind::Extends,
line: base_line + i as u32,
column: 0,
file_path: state.file_path.clone(),
});
}
} else if let Some(iface_list) = trimmed.strip_prefix("Implements ") {
for iface in iface_list.split(',') {
let iface = iface.trim();
if !iface.is_empty() {
state.unresolved_refs.push(UnresolvedRef {
from_node_id: class_id.to_string(),
reference_name: iface.to_string(),
reference_kind: EdgeKind::Implements,
line: base_line + i as u32,
column: 0,
file_path: state.file_path.clone(),
});
}
}
}
}
}
fn extract_xml_docstring(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
let mut comments = Vec::new();
let mut current = node.prev_sibling();
while let Some(sibling) = current {
let kind = sibling.kind();
if kind == "comment" {
let text = state.node_text(sibling);
let trimmed = text.trim();
if trimmed.starts_with("'''") {
comments.push(trimmed.to_string());
current = sibling.prev_sibling();
} else {
break;
}
} else if kind == "blank_line" {
current = sibling.prev_sibling();
} else {
break;
}
}
if comments.is_empty() {
return None;
}
comments.reverse();
let cleaned: Vec<String> = comments
.iter()
.map(|line| {
let stripped = line.strip_prefix("'''").unwrap_or(line).trim();
Self::strip_xml_tags(stripped)
})
.filter(|s| !s.is_empty())
.collect();
if cleaned.is_empty() {
None
} else {
Some(cleaned.join("\n").trim().to_string())
}
}
fn strip_xml_tags(s: &str) -> String {
let mut result = String::new();
let mut in_tag = false;
for c in s.chars() {
if c == '<' {
in_tag = true;
} else if c == '>' {
in_tag = false;
} else if !in_tag {
result.push(c);
}
}
result.trim().to_string()
}
fn extract_call_sites_from_children(
state: &mut ExtractionState,
node: TsNode<'_>,
fn_node_id: &str,
) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
match child.kind() {
"invocation" => {
let callee_name = Self::extract_invocation_name(state, child);
state.unresolved_refs.push(UnresolvedRef {
from_node_id: fn_node_id.to_string(),
reference_name: callee_name,
reference_kind: EdgeKind::Calls,
line: child.start_position().row as u32,
column: child.start_position().column as u32,
file_path: state.file_path.clone(),
});
Self::extract_call_sites_from_children(state, child, fn_node_id);
}
"method_declaration" | "constructor_declaration" | "class_block" => {}
_ => {
Self::extract_call_sites_from_children(state, child, fn_node_id);
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn extract_invocation_name(state: &ExtractionState, node: TsNode<'_>) -> String {
if let Some(target) = node.child_by_field_name("target") {
return state.node_text(target);
}
if let Some(first) = node.child(0) {
if first.kind() != "argument_list" {
return state.node_text(first);
}
}
state.node_text(node)
}
fn extract_annotations_from_children(
state: &mut ExtractionState,
node: TsNode<'_>,
target_id: &str,
) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "attribute_block" {
Self::extract_annotations_from_block(state, child, target_id);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn extract_annotations_from_prev_siblings(
state: &mut ExtractionState,
node: TsNode<'_>,
target_id: &str,
) {
let mut sibling = node.prev_sibling();
while let Some(sib) = sibling {
if sib.kind() == "attribute_block" {
Self::extract_annotations_from_block(state, sib, target_id);
} else if sib.kind() != "blank_line" && sib.kind() != "comment" {
break;
}
sibling = sib.prev_sibling();
}
}
fn extract_annotations_from_block(
state: &mut ExtractionState,
attr_block: TsNode<'_>,
target_id: &str,
) {
let mut cursor = attr_block.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "attribute" {
let attr_name = Self::extract_vb_attribute_name(state, child);
let start_line = child.start_position().row as u32;
let end_line = child.end_position().row as u32;
let start_column = child.start_position().column as u32;
let end_column = child.end_position().column as u32;
let qualified_name =
format!("{}::@{}", state.qualified_prefix(), attr_name);
let id = generate_node_id(
&state.file_path,
&NodeKind::AnnotationUsage,
&attr_name,
start_line,
);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::AnnotationUsage,
name: attr_name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(
format!("<{}>", state.node_text(child).trim()),
),
docstring: None,
visibility: Visibility::Private,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
state.unresolved_refs.push(UnresolvedRef {
from_node_id: id.clone(),
reference_name: attr_name,
reference_kind: EdgeKind::Annotates,
line: start_line,
column: start_column,
file_path: state.file_path.clone(),
});
state.edges.push(Edge {
source: id,
target: target_id.to_string(),
kind: EdgeKind::Annotates,
line: Some(start_line),
});
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn extract_vb_attribute_name(state: &ExtractionState, node: TsNode<'_>) -> String {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "identifier" || child.kind() == "qualified_name" {
return state.node_text(child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
let text = state.node_text(node);
text.split('(').next().unwrap_or(&text).trim().to_string()
}
fn build_result(state: ExtractionState, start: Instant) -> ExtractionResult {
ExtractionResult {
nodes: state.nodes,
edges: state.edges,
unresolved_refs: state.unresolved_refs,
errors: state.errors,
duration_ms: start.elapsed().as_millis() as u64,
}
}
}
impl crate::extraction::LanguageExtractor for VbNetExtractor {
fn extensions(&self) -> &[&str] {
&["vb"]
}
fn language_name(&self) -> &str {
"VB.NET"
}
fn extract(&self, file_path: &str, source: &str) -> ExtractionResult {
VbNetExtractor::extract_vbnet(file_path, source)
}
}