use std::time::{Instant, SystemTime, UNIX_EPOCH};
use tree_sitter::{Node as TsNode, Parser, Tree};
use crate::extraction::complexity::{count_complexity, SWIFT_COMPLEXITY};
use crate::types::{
generate_node_id, Edge, EdgeKind, ExtractionResult, Node, NodeKind, UnresolvedRef, Visibility,
};
pub struct SwiftExtractor;
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 SwiftExtractor {
pub fn extract_swift(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("swift");
parser
.set_language(&language)
.map_err(|e| format!("failed to load Swift 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() {
"import_declaration" => Self::visit_import(state, node),
"class_declaration" => Self::visit_class_declaration(state, node),
"protocol_declaration" => Self::visit_protocol(state, node),
"function_declaration" => Self::visit_function(state, node),
"init_declaration" => Self::visit_init(state, node),
"property_declaration" => Self::visit_property(state, node),
"typealias_declaration" => Self::visit_typealias(state, node),
_ => {}
}
}
fn visit_import(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_child_by_kind(node, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| {
let text = state.node_text(node);
text.strip_prefix("import ")
.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(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Use, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Use,
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 visit_class_declaration(state: &mut ExtractionState, node: TsNode<'_>) {
let decl_kind = Self::declaration_kind_keyword(state, node);
match decl_kind.as_str() {
"class" => Self::visit_class(state, node),
"struct" => Self::visit_struct(state, node),
"enum" => Self::visit_enum(state, node),
"extension" => Self::visit_extension(state, node),
_ => Self::visit_class(state, node), }
}
fn declaration_kind_keyword(state: &ExtractionState, node: TsNode<'_>) -> String {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
if cursor.field_name() == Some("declaration_kind") {
return state.node_text(cursor.node());
}
if !cursor.goto_next_sibling() {
break;
}
}
}
if let Some(first) = node.child(0) {
return state.node_text(first);
}
"class".to_string()
}
fn visit_class(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::extract_type_name(state, node);
let docstring = Self::extract_docstring(state, node);
let signature = Self::extract_first_line_signature(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::Class, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Class,
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),
});
}
Self::extract_inheritance(state, node, &id);
state.node_stack.push((name.clone(), id));
state.class_depth += 1;
if let Some(body) = node.child_by_field_name("body") {
Self::visit_children(state, body);
}
state.class_depth -= 1;
state.node_stack.pop();
}
fn visit_struct(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::extract_type_name(state, node);
let docstring = Self::extract_docstring(state, node);
let signature = Self::extract_first_line_signature(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::Struct, &name, start_line);
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.clone(), id));
state.class_depth += 1;
if let Some(body) = node.child_by_field_name("body") {
Self::visit_children(state, body);
}
state.class_depth -= 1;
state.node_stack.pop();
}
fn visit_enum(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::extract_type_name(state, node);
let docstring = Self::extract_docstring(state, node);
let signature = Self::extract_first_line_signature(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: 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.clone(), id));
state.class_depth += 1;
if let Some(body) = node.child_by_field_name("body") {
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_entry" => Self::visit_enum_entry(state, child),
"function_declaration" => Self::visit_function(state, child),
"init_declaration" => Self::visit_init(state, child),
"property_declaration" => Self::visit_property(state, child),
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_enum_entry(state: &mut ExtractionState, node: TsNode<'_>) {
let name = node
.child_by_field_name("name")
.map(|n| state.node_text(n))
.or_else(|| {
Self::find_child_by_kind(node, "simple_identifier").map(|n| state.node_text(n))
})
.unwrap_or_else(|| {
let text = state.node_text(node);
text.strip_prefix("case ")
.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(), 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 visit_extension(state: &mut ExtractionState, node: TsNode<'_>) {
let name = node
.child_by_field_name("name")
.map(|n| {
Self::find_child_by_kind(n, "type_identifier")
.map(|ti| state.node_text(ti))
.unwrap_or_else(|| state.node_text(n))
})
.unwrap_or_else(|| "<anonymous>".to_string());
let docstring = Self::extract_docstring(state, node);
let signature = Self::extract_first_line_signature(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: 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.clone(), id));
state.class_depth += 1;
if let Some(body) = node.child_by_field_name("body") {
Self::visit_children(state, body);
}
state.class_depth -= 1;
state.node_stack.pop();
}
fn visit_protocol(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 docstring = Self::extract_docstring(state, node);
let signature = Self::extract_first_line_signature(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::Interface, &name, start_line);
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.clone(), id));
state.class_depth += 1;
if let Some(body) = node.child_by_field_name("body") {
Self::visit_protocol_body(state, body);
}
state.class_depth -= 1;
state.node_stack.pop();
}
fn visit_protocol_body(state: &mut ExtractionState, body: TsNode<'_>) {
let mut cursor = body.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
match child.kind() {
"protocol_function_declaration" => {
Self::visit_protocol_function(state, child);
}
"property_declaration" => Self::visit_property(state, child),
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_protocol_function(state: &mut ExtractionState, node: TsNode<'_>) {
let name = node
.child_by_field_name("name")
.map(|n| state.node_text(n))
.or_else(|| {
Self::find_child_by_kind(node, "simple_identifier").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::Method, &name, start_line);
let signature = Self::extract_first_line_signature(state, node);
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: 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_function(state: &mut ExtractionState, node: TsNode<'_>) {
let name = node
.child_by_field_name("name")
.map(|n| state.node_text(n))
.or_else(|| {
Self::find_child_by_kind(node, "simple_identifier").map(|n| state.node_text(n))
})
.unwrap_or_else(|| "<anonymous>".to_string());
let in_class = state.class_depth > 0;
let kind = if in_class {
NodeKind::Method
} else {
NodeKind::Function
};
let visibility = Self::extract_visibility(state, node);
let is_async = Self::has_async_keyword(node, &state.source);
let signature = Self::extract_first_line_signature(state, node);
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, &kind, &name, start_line);
let metrics = count_complexity(node, &SWIFT_COMPLEXITY, &state.source);
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,
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(state, node, &id);
}
fn visit_init(state: &mut ExtractionState, node: TsNode<'_>) {
let name = "init".to_string();
let visibility = Self::extract_visibility(state, node);
let signature = Self::extract_first_line_signature(state, node);
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::Constructor, &name, start_line);
let metrics = count_complexity(node, &SWIFT_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,
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(state, node, &id);
}
fn visit_property(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::extract_property_name(state, node);
let in_class = state.class_depth > 0;
let kind = if in_class {
NodeKind::Property
} else {
NodeKind::Const
};
let visibility = Self::extract_visibility(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, &kind, &name, start_line);
let text = state.node_text(node);
let graph_node = Node {
id: id.clone(),
kind,
name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(text.lines().next().unwrap_or("").trim().to_string()),
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_typealias(state: &mut ExtractionState, node: TsNode<'_>) {
let name = node
.child_by_field_name("name")
.map(|n| state.node_text(n))
.or_else(|| {
Self::find_child_by_kind(node, "type_identifier").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::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: 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_type_name(state: &ExtractionState, node: TsNode<'_>) -> String {
node.child_by_field_name("name")
.map(|n| {
if n.kind() == "user_type" {
Self::find_child_by_kind(n, "type_identifier")
.map(|ti| state.node_text(ti))
.unwrap_or_else(|| state.node_text(n))
} else {
state.node_text(n)
}
})
.unwrap_or_else(|| "<anonymous>".to_string())
}
fn extract_inheritance(state: &mut ExtractionState, node: TsNode<'_>, class_id: &str) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "inheritance_specifier" {
if let Some(inherits_from) = child.child_by_field_name("inherits_from") {
let base_name = Self::find_child_by_kind(inherits_from, "type_identifier")
.map(|ti| state.node_text(ti))
.unwrap_or_else(|| state.node_text(inherits_from));
let line = inherits_from.start_position().row as u32;
let column = inherits_from.start_position().column as u32;
state.unresolved_refs.push(UnresolvedRef {
from_node_id: class_id.to_string(),
reference_name: base_name,
reference_kind: EdgeKind::Extends,
line,
column,
file_path: state.file_path.clone(),
});
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn extract_property_name(state: &ExtractionState, node: TsNode<'_>) -> String {
if let Some(pattern) = node.child_by_field_name("name") {
if let Some(ident) = pattern.child_by_field_name("bound_identifier") {
return state.node_text(ident);
}
if let Some(ident) = Self::find_child_by_kind(pattern, "simple_identifier") {
return state.node_text(ident);
}
return state.node_text(pattern);
}
if let Some(pattern) = Self::find_child_by_kind(node, "pattern") {
if let Some(ident) = Self::find_child_by_kind(pattern, "simple_identifier") {
return state.node_text(ident);
}
}
"<anonymous>".to_string()
}
fn extract_visibility(state: &ExtractionState, node: TsNode<'_>) -> Visibility {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "modifiers" {
if let Some(vis_mod) = Self::find_child_by_kind(child, "visibility_modifier") {
let text = state.node_text(vis_mod);
return match text.as_str() {
"private" => Visibility::Private,
"fileprivate" => Visibility::Private,
"internal" => Visibility::PubCrate,
"public" => Visibility::Pub,
"open" => Visibility::Pub,
_ => Visibility::Pub,
};
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
Visibility::Pub
}
fn has_async_keyword(node: TsNode<'_>, _source: &[u8]) -> bool {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "async" {
return true;
}
if !cursor.goto_next_sibling() {
break;
}
}
}
false
}
fn extract_first_line_signature(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
let text = state.node_text(node);
let first_line = text.lines().next()?.trim().to_string();
if first_line.is_empty() {
None
} else {
Some(first_line)
}
}
fn extract_docstring(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
let mut comments: Vec<String> = Vec::new();
let mut prev = node.prev_named_sibling();
while let Some(prev_node) = prev {
if prev_node.kind() == "comment" {
let text = state.node_text(prev_node);
let stripped = text
.trim_start_matches("///")
.trim_start_matches("//")
.trim()
.to_string();
comments.push(stripped);
prev = prev_node.prev_named_sibling();
} else {
break;
}
}
if comments.is_empty() {
return None;
}
comments.reverse();
Some(comments.join("\n"))
}
fn extract_call_sites(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() {
"call_expression" => {
let callee_name = Self::extract_callee_name(state, child);
if let Some(name) = callee_name {
state.unresolved_refs.push(UnresolvedRef {
from_node_id: fn_node_id.to_string(),
reference_name: 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);
}
"function_declaration" | "init_declaration" | "class_declaration"
| "protocol_declaration" => {}
_ => {
Self::extract_call_sites(state, child, fn_node_id);
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn extract_callee_name(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
let first_child = node.named_child(0)?;
match first_child.kind() {
"simple_identifier" => Some(state.node_text(first_child)),
"navigation_expression" => {
let mut last_name = None;
let mut cursor = first_child.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "navigation_suffix" {
if let Some(ident) =
Self::find_child_by_kind(child, "simple_identifier")
{
last_name = Some(state.node_text(ident));
}
} else if child.kind() == "simple_identifier" && last_name.is_none() {
last_name = Some(state.node_text(child));
}
if !cursor.goto_next_sibling() {
break;
}
}
}
last_name
}
_ => Some(state.node_text(first_child)),
}
}
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 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 SwiftExtractor {
fn extensions(&self) -> &[&str] {
&["swift"]
}
fn language_name(&self) -> &str {
"Swift"
}
fn extract(&self, file_path: &str, source: &str) -> ExtractionResult {
Self::extract_swift(file_path, source)
}
}