use std::time::{Instant, SystemTime, UNIX_EPOCH};
use tree_sitter::{Node as TsNode, Parser, Tree};
use crate::extraction::complexity::{count_complexity, DART_COMPLEXITY};
use crate::types::{
generate_node_id, Edge, EdgeKind, ExtractionResult, Node, NodeKind, UnresolvedRef, Visibility,
};
pub struct DartExtractor;
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 DartExtractor {
pub fn extract_dart(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_program_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("dart");
parser
.set_language(&language)
.map_err(|e| format!("failed to load Dart grammar: {e}"))?;
parser
.parse(source, None)
.ok_or_else(|| "tree-sitter parse returned None".to_string())
}
fn visit_program_children(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if !cursor.goto_first_child() {
return;
}
loop {
let child = cursor.node();
match child.kind() {
"library_name" => Self::visit_library(state, child),
"import_or_export" => Self::visit_import(state, child),
"class_definition" => Self::visit_class(state, child),
"mixin_declaration" => Self::visit_mixin(state, child),
"extension_declaration" => Self::visit_extension(state, child),
"enum_declaration" => Self::visit_enum(state, child),
"type_alias" => Self::visit_type_alias(state, child),
"function_signature" => {
let body = child
.next_named_sibling()
.filter(|s| s.kind() == "function_body");
Self::visit_top_level_function(state, child, body);
}
"declaration" => Self::visit_declaration(state, child),
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
fn visit_library(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_child_by_kind(node, "dotted_identifier_list")
.map(|n| state.node_text(n))
.unwrap_or_else(|| state.node_text(node).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(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Library, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Library,
name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(state.node_text(node)),
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 visit_import(state: &mut ExtractionState, node: TsNode<'_>) {
let text = state.node_text(node);
let path = Self::extract_import_path(&text);
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(text.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 extract_import_path(text: &str) -> String {
if let Some(start) = text.find('\'') {
if let Some(end) = text[start + 1..].find('\'') {
return text[start + 1..start + 1 + end].to_string();
}
}
if let Some(start) = text.find('"') {
if let Some(end) = text[start + 1..].find('"') {
return text[start + 1..start + 1 + end].to_string();
}
}
text.trim().to_string()
}
fn visit_top_level_function(
state: &mut ExtractionState,
sig_node: TsNode<'_>,
body: Option<TsNode<'_>>,
) {
let name = sig_node
.child_by_field_name("name")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let visibility = Self::dart_visibility(&name);
let docstring = Self::extract_docstring(state, sig_node);
let sig_text = state.node_text(sig_node);
let signature = Some(sig_text.trim().to_string());
let is_async = match body {
Some(b) => state.node_text(b).starts_with("async"),
None => false,
};
let start_line = sig_node.start_position().row as u32;
let end_line = body.map_or(sig_node.end_position().row as u32, |b| {
b.end_position().row as u32
});
let start_column = sig_node.start_position().column as u32;
let end_column = body.map_or(sig_node.end_position().column as u32, |b| {
b.end_position().column as u32
});
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Function, &name, start_line);
let metrics = body.map_or(Default::default(), |b| {
count_complexity(b, &DART_COMPLEXITY, &state.source)
});
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Function,
name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature,
docstring,
visibility,
is_async,
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),
});
}
if let Some(body_node) = body {
Self::extract_call_sites(state, body_node, &id);
}
Self::extract_annotations_from_modifiers(state, sig_node, &id);
}
fn visit_class(state: &mut ExtractionState, node: TsNode<'_>) {
let is_abstract = Self::find_child_by_kind(node, "abstract").is_some();
let name = node
.child_by_field_name("name")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let visibility = Self::dart_visibility(&name);
let docstring = Self::extract_docstring(state, node);
let signature = Self::extract_signature_to_brace(state, node);
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 is_abstract {
NodeKind::Interface
} else {
NodeKind::Class
};
let id = generate_node_id(&state.file_path, &kind, &name, start_line);
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,
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(superclass) = node.child_by_field_name("superclass") {
if let Some(type_id) = Self::find_child_by_kind(superclass, "type_identifier") {
let type_name = state.node_text(type_id);
state.unresolved_refs.push(UnresolvedRef {
from_node_id: id.clone(),
reference_name: type_name,
reference_kind: EdgeKind::Extends,
line: superclass.start_position().row as u32,
column: superclass.start_position().column as u32,
file_path: state.file_path.clone(),
});
}
}
Self::extract_annotations_from_modifiers(state, node, &id);
if let Some(body) = node.child_by_field_name("body") {
state.node_stack.push((name, id.clone()));
state.class_depth += 1;
Self::visit_class_body(state, body);
state.class_depth -= 1;
state.node_stack.pop();
}
}
fn visit_mixin(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_child_by_kind(node, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let visibility = Self::dart_visibility(&name);
let docstring = Self::extract_docstring(state, node);
let signature = Self::extract_signature_to_brace(state, node);
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::Mixin, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Mixin,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature,
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.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
Self::extract_annotations_from_modifiers(state, node, &id);
if let Some(body) = Self::find_child_by_kind(node, "class_body") {
state.node_stack.push((name, id.clone()));
state.class_depth += 1;
Self::visit_class_body(state, body);
state.class_depth -= 1;
state.node_stack.pop();
}
}
fn visit_extension(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_child_by_kind(node, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let visibility = Self::dart_visibility(&name);
let docstring = Self::extract_docstring(state, node);
let signature = Self::extract_signature_to_brace(state, node);
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::Extension, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Extension,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature,
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.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
if let Some(body) = node.child_by_field_name("body") {
state.node_stack.push((name, id.clone()));
state.class_depth += 1;
Self::visit_body_members(state, body);
state.class_depth -= 1;
state.node_stack.pop();
}
}
fn visit_enum(state: &mut ExtractionState, node: TsNode<'_>) {
let name = node
.child_by_field_name("name")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let visibility = Self::dart_visibility(&name);
let docstring = Self::extract_docstring(state, node);
let signature = Self::extract_signature_to_brace(state, node);
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,
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.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
Self::extract_annotations_from_modifiers(state, node, &id);
if let Some(body) = node.child_by_field_name("body") {
state.node_stack.push((name, id.clone()));
state.class_depth += 1;
Self::visit_enum_body(state, body);
state.class_depth -= 1;
state.node_stack.pop();
}
}
fn visit_enum_body(state: &mut ExtractionState, body: TsNode<'_>) {
let mut cursor = body.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
match child.kind() {
"enum_constant" => Self::visit_enum_constant(state, child),
"declaration" => Self::visit_declaration(state, child),
"method_signature" => Self::visit_method_signature(state, child),
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_enum_constant(state: &mut ExtractionState, node: TsNode<'_>) {
let name = node
.child_by_field_name("name")
.map(|n| state.node_text(n))
.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::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)),
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 visit_type_alias(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_child_by_kind(node, "type_identifier")
.or_else(|| Self::find_child_by_kind(node, "identifier"))
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let visibility = Self::dart_visibility(&name);
let docstring = Self::extract_docstring(state, node);
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::TypeAlias, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::TypeAlias,
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,
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_class_body(state: &mut ExtractionState, body: TsNode<'_>) {
Self::visit_body_members(state, body);
}
fn visit_body_members(state: &mut ExtractionState, body: TsNode<'_>) {
let mut cursor = body.walk();
if !cursor.goto_first_child() {
return;
}
loop {
let child = cursor.node();
match child.kind() {
"declaration" => Self::visit_declaration(state, child),
"method_signature" => Self::visit_method_signature(state, child),
"function_signature" => {
let body_node = child
.next_named_sibling()
.filter(|s| s.kind() == "function_body");
Self::visit_method_from_sig(state, child, body_node);
}
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
fn visit_method_signature(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
match child.kind() {
"function_signature" => {
let body = node
.next_named_sibling()
.filter(|s| s.kind() == "function_body");
Self::visit_method_from_sig(state, child, body);
return;
}
"constructor_signature"
| "constant_constructor_signature"
| "factory_constructor_signature"
| "redirecting_factory_constructor_signature" => {
Self::visit_constructor(state, node, child);
return;
}
"getter_signature" => {
Self::visit_getter_or_setter(state, node, child);
return;
}
"setter_signature" => {
Self::visit_getter_or_setter(state, node, child);
return;
}
"operator_signature" => {
Self::visit_operator(state, node, child);
return;
}
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_declaration(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if !cursor.goto_first_child() {
return;
}
let mut func_sig: Option<TsNode<'_>> = None;
let mut func_body: Option<TsNode<'_>> = None;
let mut has_static = false;
loop {
let child = cursor.node();
match child.kind() {
"function_signature" => func_sig = Some(child),
"function_body" | "block" => func_body = Some(child),
"static" => has_static = true,
"constructor_signature"
| "constant_constructor_signature"
| "factory_constructor_signature"
| "redirecting_factory_constructor_signature" => {
Self::visit_constructor(state, node, child);
return;
}
"getter_signature" => {
Self::visit_getter_or_setter(state, node, child);
return;
}
"setter_signature" => {
Self::visit_getter_or_setter(state, node, child);
return;
}
"operator_signature" => {
Self::visit_operator(state, node, child);
return;
}
"initialized_variable_definition" => {
Self::visit_initialized_var_def(state, node, child, has_static);
return;
}
"initialized_identifier_list" => {
Self::visit_identifier_list_field(state, node, child);
return;
}
"static_final_declaration_list" => {
Self::visit_static_final_declarations(state, node, child);
return;
}
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
if let Some(sig) = func_sig {
if state.class_depth > 0 {
Self::visit_method_from_sig(state, sig, func_body);
} else {
Self::visit_top_level_function(state, sig, func_body);
}
}
}
fn visit_method_from_sig(
state: &mut ExtractionState,
sig_node: TsNode<'_>,
body: Option<TsNode<'_>>,
) {
let name = sig_node
.child_by_field_name("name")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let visibility = Self::dart_visibility(&name);
let docstring = Self::extract_docstring(state, sig_node);
let sig_text = state.node_text(sig_node);
let signature = Some(sig_text.trim().to_string());
let is_async = match body {
Some(b) => state.node_text(b).starts_with("async"),
None => false,
};
let start_line = sig_node.start_position().row as u32;
let end_line = body.map_or(sig_node.end_position().row as u32, |b| {
b.end_position().row as u32
});
let start_column = sig_node.start_position().column as u32;
let end_column = body.map_or(sig_node.end_position().column as u32, |b| {
b.end_position().column as u32
});
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Method, &name, start_line);
let metrics = body.map_or(Default::default(), |b| {
count_complexity(b, &DART_COMPLEXITY, &state.source)
});
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Method,
name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature,
docstring,
visibility,
is_async,
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),
});
}
if let Some(body_node) = body {
Self::extract_call_sites(state, body_node, &id);
}
Self::extract_annotations_from_modifiers(state, sig_node, &id);
if let Some(parent) = sig_node.parent() {
Self::extract_annotations_from_modifiers(state, parent, &id);
}
}
fn visit_constructor(state: &mut ExtractionState, decl_node: TsNode<'_>, sig_node: TsNode<'_>) {
let name = Self::extract_constructor_name(state, sig_node);
let docstring = Self::extract_docstring(state, decl_node);
let text = state.node_text(decl_node);
let signature = text.find('{').map_or_else(
|| Some(text.trim().trim_end_matches(';').trim().to_string()),
|pos| Some(text[..pos].trim().to_string()),
);
let start_line = decl_node.start_position().row as u32;
let end_line = decl_node.end_position().row as u32;
let start_column = decl_node.start_position().column as u32;
let end_column = decl_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(decl_node, &DART_COMPLEXITY, &state.source);
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,
visibility: Visibility::Pub,
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,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn extract_constructor_name(state: &ExtractionState, sig_node: TsNode<'_>) -> String {
let mut parts = Vec::new();
let mut cursor = sig_node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "identifier" {
parts.push(state.node_text(child));
}
if !cursor.goto_next_sibling() {
break;
}
}
}
if parts.is_empty() {
"<constructor>".to_string()
} else {
parts.join(".")
}
}
fn visit_getter_or_setter(
state: &mut ExtractionState,
decl_node: TsNode<'_>,
sig_node: TsNode<'_>,
) {
let name = Self::find_child_by_kind(sig_node, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let visibility = Self::dart_visibility(&name);
let docstring = Self::extract_docstring(state, decl_node);
let text = state.node_text(decl_node);
let signature = text.find('{').map_or_else(
|| {
text.find("=>").map_or_else(
|| Some(text.trim().to_string()),
|pos| Some(text[..pos].trim().to_string()),
)
},
|pos| Some(text[..pos].trim().to_string()),
);
let start_line = decl_node.start_position().row as u32;
let end_line = decl_node.end_position().row as u32;
let start_column = decl_node.start_position().column as u32;
let end_column = decl_node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Method, &name, start_line);
let metrics = count_complexity(decl_node, &DART_COMPLEXITY, &state.source);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Method,
name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature,
docstring,
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,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn visit_operator(state: &mut ExtractionState, decl_node: TsNode<'_>, _sig_node: TsNode<'_>) {
let text = state.node_text(decl_node);
let name = text
.find("operator")
.map(|pos| {
let after = &text[pos + 8..];
after
.trim()
.split('(')
.next()
.unwrap_or("")
.trim()
.to_string()
})
.unwrap_or_else(|| "operator".to_string());
let name = format!("operator {}", name);
let docstring = Self::extract_docstring(state, decl_node);
let signature = text.find('{').map_or_else(
|| Some(text.trim().to_string()),
|pos| Some(text[..pos].trim().to_string()),
);
let start_line = decl_node.start_position().row as u32;
let end_line = decl_node.end_position().row as u32;
let start_column = decl_node.start_position().column as u32;
let end_column = decl_node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Method, &name, start_line);
let metrics = count_complexity(decl_node, &DART_COMPLEXITY, &state.source);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Method,
name,
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: 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,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn visit_initialized_var_def(
state: &mut ExtractionState,
decl_node: TsNode<'_>,
var_def: TsNode<'_>,
_has_static: bool,
) {
let name = var_def
.child_by_field_name("name")
.map(|n| state.node_text(n))
.unwrap_or_else(|| {
Self::find_child_by_kind(var_def, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string())
});
Self::emit_field(state, decl_node, &name);
}
fn visit_identifier_list_field(
state: &mut ExtractionState,
decl_node: TsNode<'_>,
list_node: TsNode<'_>,
) {
let mut cursor = list_node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "initialized_identifier" {
if let Some(ident) = Self::find_child_by_kind(child, "identifier") {
let name = state.node_text(ident);
Self::emit_field(state, decl_node, &name);
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_static_final_declarations(
state: &mut ExtractionState,
decl_node: TsNode<'_>,
list_node: TsNode<'_>,
) {
let mut cursor = list_node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "static_final_declaration" {
if let Some(ident) = Self::find_child_by_kind(child, "identifier") {
let name = state.node_text(ident);
Self::emit_field(state, decl_node, &name);
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn emit_field(state: &mut ExtractionState, decl_node: TsNode<'_>, name: &str) {
let visibility = Self::dart_visibility(name);
let docstring = Self::extract_docstring(state, decl_node);
let text = state.node_text(decl_node);
let start_line = decl_node.start_position().row as u32;
let end_line = decl_node.end_position().row as u32;
let start_column = decl_node.start_position().column as u32;
let end_column = decl_node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Field, name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Field,
name: name.to_string(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(text.trim().trim_end_matches(';').trim().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 extract_call_sites(state: &mut ExtractionState, node: TsNode<'_>, fn_node_id: &str) {
let mut cursor = node.walk();
if !cursor.goto_first_child() {
return;
}
loop {
let child = cursor.node();
match child.kind() {
"expression_statement"
| "block"
| "if_statement"
| "for_statement"
| "while_statement"
| "do_statement"
| "switch_statement"
| "try_statement"
| "return_statement"
| "unary_expression"
| "await_expression"
| "conditional_expression"
| "argument"
| "named_argument"
| "arguments"
| "parenthesized_expression"
| "assignment_expression"
| "assignment_expression_without_cascade"
| "local_variable_declaration"
| "initialized_variable_definition"
| "catch_clause"
| "finally_clause"
| "yield_statement"
| "yield_each_statement"
| "throw_expression"
| "throw_expression_without_cascade"
| "spread_element"
| "for_element"
| "if_element"
| "list_literal"
| "set_or_map_literal"
| "cascade_section"
| "switch_expression"
| "switch_expression_case"
| "switch_statement_case"
| "switch_statement_default"
| "pattern_assignment"
| "pattern_variable_declaration"
| "assert_statement"
| "assert_builtin"
| "assertion" => {
Self::extract_call_sites(state, child, fn_node_id);
}
"identifier" => {
let callee_name = state.node_text(child);
if let Some(next) = child.next_named_sibling() {
if next.kind() == "selector"
&& (Self::find_child_by_kind(next, "argument_part").is_some()
|| Self::find_child_by_kind(next, "arguments").is_some())
{
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(),
});
}
}
}
"selector" => {
if Self::find_child_by_kind(child, "argument_part").is_some()
|| Self::find_child_by_kind(child, "arguments").is_some()
{
if let Some(uas) =
Self::find_child_by_kind(child, "unconditional_assignable_selector")
{
if let Some(ident) = Self::find_child_by_kind(uas, "identifier") {
let callee_name = state.node_text(ident);
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(state, child, fn_node_id);
}
"argument_part" => {
Self::extract_call_sites(state, child, fn_node_id);
}
"function_expression" | "lambda_expression" => {}
_ => {
Self::extract_call_sites(state, child, fn_node_id);
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
fn extract_signature_to_brace(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
let text = state.node_text(node);
if let Some(brace_pos) = text.find('{') {
Some(text[..brace_pos].trim().to_string())
} else {
Some(text.trim().to_string())
}
}
fn extract_docstring(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
let mut comments = Vec::new();
let mut current = node.prev_named_sibling();
while let Some(sibling) = current {
match sibling.kind() {
"documentation_comment" => {
let text = state.node_text(sibling);
comments.push(text);
current = sibling.prev_named_sibling();
}
"comment" => {
let text = state.node_text(sibling);
if text.trim_start().starts_with("///") {
comments.push(text);
current = sibling.prev_named_sibling();
} else {
break;
}
}
_ => break,
}
}
if comments.is_empty() {
return None;
}
comments.reverse();
let cleaned: Vec<String> = comments
.iter()
.map(|c| Self::clean_doc_comment(c))
.collect();
let result = cleaned.join("\n").trim().to_string();
if result.is_empty() {
None
} else {
Some(result)
}
}
fn clean_doc_comment(comment: &str) -> String {
let trimmed = comment.trim();
if trimmed.contains("///") {
return trimmed
.lines()
.map(|line| {
let l = line.trim();
if let Some(stripped) = l.strip_prefix("///") {
stripped.strip_prefix(' ').unwrap_or(stripped).to_string()
} else {
l.to_string()
}
})
.collect::<Vec<_>>()
.join("\n")
.trim()
.to_string();
}
if trimmed.starts_with("/**") && trimmed.ends_with("*/") {
let inner = &trimmed[3..trimmed.len() - 2];
return inner
.lines()
.map(|line| {
let l = line.trim();
l.strip_prefix("* ")
.or_else(|| l.strip_prefix('*'))
.unwrap_or(l)
})
.collect::<Vec<_>>()
.join("\n")
.trim()
.to_string();
}
trimmed.to_string()
}
fn dart_visibility(name: &str) -> Visibility {
if name.starts_with('_') {
Visibility::Private
} else {
Visibility::Pub
}
}
fn find_child_by_kind<'a>(node: TsNode<'a>, kind: &str) -> Option<TsNode<'a>> {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == kind {
return Some(child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
None
}
fn extract_annotations_from_modifiers(
state: &mut ExtractionState,
node: TsNode<'_>,
target_id: &str,
) {
let mut current = node.prev_named_sibling();
while let Some(sibling) = current {
match sibling.kind() {
"annotation" | "marker_annotation" => {
Self::extract_annotations_from_node(state, sibling, target_id);
current = sibling.prev_named_sibling();
}
"comment" | "documentation_comment" => {
current = sibling.prev_named_sibling();
}
_ => break,
}
}
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "annotation" || child.kind() == "marker_annotation" {
Self::extract_annotations_from_node(state, child, target_id);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn extract_annotations_from_node(
state: &mut ExtractionState,
node: TsNode<'_>,
target_id: &str,
) {
let annot_name = Self::extract_annotation_name(state, node);
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(), annot_name);
let id = generate_node_id(
&state.file_path,
&NodeKind::AnnotationUsage,
&annot_name,
start_line,
);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::AnnotationUsage,
name: annot_name.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);
state.unresolved_refs.push(UnresolvedRef {
from_node_id: id.clone(),
reference_name: annot_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),
});
}
fn extract_annotation_name(state: &ExtractionState, node: TsNode<'_>) -> String {
if let Some(ident) = Self::find_child_by_kind(node, "identifier") {
return state.node_text(ident);
}
let text = state.node_text(node);
text.trim()
.strip_prefix('@')
.unwrap_or(&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 DartExtractor {
fn extensions(&self) -> &[&str] {
&["dart"]
}
fn language_name(&self) -> &str {
"Dart"
}
fn extract(&self, file_path: &str, source: &str) -> ExtractionResult {
DartExtractor::extract_dart(file_path, source)
}
}