use std::time::Instant;
use tree_sitter::{Node as TsNode, Parser, Tree};
use crate::extraction::complexity::{count_complexity, BASH_COMPLEXITY};
use crate::extraction::ts_state::{find_child_by_kind, ExtractionState};
use crate::types::{
generate_node_id, Edge, EdgeKind, ExtractionResult, Node, NodeKind, UnresolvedRef, Visibility,
};
pub struct BashExtractor;
impl BashExtractor {
pub fn extract_bash(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 state.build_result(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,
attrs_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,
cognitive_complexity: 0,
distinct_operators: 0,
distinct_operands: 0,
total_operators: 0,
total_operands: 0,
updated_at: state.timestamp,
parent_id: None,
};
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();
state.build_result(start)
}
fn parse_source(source: &str) -> Result<Tree, String> {
let mut parser = Parser::new();
let language = crate::extraction::ts_provider::language("bash");
parser
.set_language(&language)
.map_err(|e| format!("failed to load Bash 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() {
"function_definition" => Self::visit_function(state, node),
"declaration_command" => Self::visit_declaration(state, node),
"command" => Self::visit_command(state, node),
_ => {}
}
}
fn visit_function(state: &mut ExtractionState, node: TsNode<'_>) {
let name = node
.child_by_field_name("name")
.map_or_else(|| "<anonymous>".to_string(), |n| state.node_text(n));
let kind = NodeKind::Function;
let visibility = Visibility::Pub;
let signature = Self::extract_function_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, &BASH_COMPLEXITY, &state.source);
let graph_node = Node {
id: id.clone(),
kind,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
attrs_start_line: 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,
cognitive_complexity: metrics.cognitive_complexity,
distinct_operators: metrics.distinct_operators,
distinct_operands: metrics.distinct_operands,
total_operators: metrics.total_operators,
total_operands: metrics.total_operands,
updated_at: state.timestamp,
parent_id: None,
};
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_declaration(state: &mut ExtractionState, node: TsNode<'_>) {
let text = state.node_text(node);
if !text.starts_with("readonly") {
return;
}
if let Some(assignment) = find_child_by_kind(node, "variable_assignment") {
if let Some(name_node) = assignment.child_by_field_name("name") {
let name = state.node_text(name_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::Const, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Const,
name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
attrs_start_line: start_line,
end_line,
start_column,
end_column,
signature: Some(text.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,
cognitive_complexity: 0,
distinct_operators: 0,
distinct_operands: 0,
total_operators: 0,
total_operands: 0,
updated_at: state.timestamp,
parent_id: None,
};
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_command(state: &mut ExtractionState, node: TsNode<'_>) {
let cmd_name = node
.child_by_field_name("name")
.map(|n| state.node_text(n))
.unwrap_or_default();
if cmd_name == "source" || cmd_name == "." {
if let Some(arg) = Self::find_first_argument(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(), arg);
let id = generate_node_id(&state.file_path, &NodeKind::Use, &arg, start_line);
let text = state.node_text(node);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Use,
name: arg,
qualified_name,
file_path: state.file_path.clone(),
start_line,
attrs_start_line: start_line,
end_line,
start_column,
end_column,
signature: Some(text.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,
cognitive_complexity: 0,
distinct_operators: 0,
distinct_operands: 0,
total_operators: 0,
total_operands: 0,
updated_at: state.timestamp,
parent_id: None,
};
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_function_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().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() {
"command" => {
if let Some(name_node) = child.child_by_field_name("name") {
let callee_name = state.node_text(name_node);
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);
}
"function_definition" => {}
_ => {
Self::extract_call_sites(state, child, fn_node_id);
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn find_first_argument(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if cursor.field_name() == Some("argument") {
return Some(state.node_text(child));
}
if !cursor.goto_next_sibling() {
break;
}
}
}
None
}
}
impl crate::extraction::LanguageExtractor for BashExtractor {
fn extensions(&self) -> &[&str] {
&["sh", "bash"]
}
fn language_name(&self) -> &'static str {
"Bash"
}
fn extract(&self, file_path: &str, source: &str) -> ExtractionResult {
Self::extract_bash(file_path, source)
}
}