code-analyze-core 0.4.1

Multi-language AST analysis library using tree-sitter
Documentation
// SPDX-FileCopyrightText: 2026 code-analyze-mcp contributors
// SPDX-License-Identifier: Apache-2.0
use tree_sitter::Node;

/// Tree-sitter query for extracting Go elements (functions, methods, and types).
pub const ELEMENT_QUERY: &str = r"
(function_declaration
  name: (identifier) @func_name) @function
(method_declaration
  name: (field_identifier) @method_name) @function
(type_spec
  name: (type_identifier) @type_name
  type: (struct_type)) @class
(type_spec
  name: (type_identifier) @type_name
  type: (interface_type)) @class
";

/// Tree-sitter query for extracting function calls.
pub const CALL_QUERY: &str = r"
(call_expression
  function: (identifier) @call)
(call_expression
  function: (selector_expression field: (field_identifier) @call))
";

/// Tree-sitter query for extracting type references.
pub const REFERENCE_QUERY: &str = r"
(type_identifier) @type_ref
";

/// Tree-sitter query for extracting Go imports.
pub const IMPORT_QUERY: &str = r"
(import_declaration) @import_path
";

/// Find method name for a receiver type.
#[must_use]
pub fn find_method_for_receiver(
    node: &Node,
    source: &str,
    _depth: Option<usize>,
) -> Option<String> {
    if node.kind() != "method_declaration" && node.kind() != "function_declaration" {
        return None;
    }
    node.child_by_field_name("name").and_then(|n| {
        let start = n.start_byte();
        let end = n.end_byte();
        if end <= source.len() {
            Some(source[start..end].to_string())
        } else {
            None
        }
    })
}

/// Extract inheritance information from a Go type node.
#[must_use]
pub fn extract_inheritance(node: &Node, source: &str) -> Vec<String> {
    let mut inherits = Vec::new();

    // Get the type field from type_spec
    if let Some(type_field) = node.child_by_field_name("type") {
        match type_field.kind() {
            "struct_type" => {
                // For struct embedding, walk children for field_declaration_list
                for i in 0..type_field.named_child_count() {
                    if let Some(field_list) =
                        type_field.named_child(u32::try_from(i).unwrap_or(u32::MAX))
                        && field_list.kind() == "field_declaration_list"
                    {
                        // Walk field_declaration_list for field_declaration without name
                        for j in 0..field_list.named_child_count() {
                            if let Some(field) =
                                field_list.named_child(u32::try_from(j).unwrap_or(u32::MAX))
                                && field.kind() == "field_declaration"
                                && field.child_by_field_name("name").is_none()
                            {
                                // Embedded type has no name field
                                if let Some(type_node) = field.child_by_field_name("type") {
                                    let text =
                                        &source[type_node.start_byte()..type_node.end_byte()];
                                    inherits.push(text.to_string());
                                }
                            }
                        }
                    }
                }
            }
            "interface_type" => {
                // For interface embedding, walk children for type_elem
                for i in 0..type_field.named_child_count() {
                    if let Some(elem) = type_field.named_child(u32::try_from(i).unwrap_or(u32::MAX))
                        && elem.kind() == "type_elem"
                    {
                        let text = &source[elem.start_byte()..elem.end_byte()];
                        inherits.push(text.to_string());
                    }
                }
            }
            _ => {}
        }
    }

    inherits
}

#[cfg(all(test, feature = "lang-go"))]
mod tests {
    use super::*;
    use tree_sitter::Parser;

    fn parse_go(source: &str) -> tree_sitter::Tree {
        let mut parser = Parser::new();
        parser
            .set_language(&tree_sitter_go::LANGUAGE.into())
            .expect("failed to set Go language");
        parser.parse(source, None).expect("failed to parse source")
    }

    #[test]
    fn test_extract_inheritance_struct_no_embeds() {
        // Arrange: struct with no embedded types
        let source = "package p\ntype Foo struct { x int }";
        let tree = parse_go(source);
        let root = tree.root_node();
        // find the type_spec node
        let type_spec = (0..root.named_child_count())
            .filter_map(|i| root.named_child(i as u32))
            .find_map(|n| {
                if n.kind() == "type_declaration" {
                    (0..n.named_child_count())
                        .filter_map(|j| n.named_child(j as u32))
                        .find(|c| c.kind() == "type_spec")
                } else {
                    None
                }
            })
            .expect("expected type_spec node");
        // Act
        let result = extract_inheritance(&type_spec, source);
        // Assert
        assert!(
            result.is_empty(),
            "expected no inherited types, got {:?}",
            result
        );
    }

    #[test]
    fn test_find_method_for_receiver_wrong_kind() {
        // Arrange: use a struct node (not a method or function declaration)
        let source = "package p\ntype Bar struct {}";
        let tree = parse_go(source);
        let root = tree.root_node();
        let node = root.named_child(0).expect("expected child");
        // Act
        let result = find_method_for_receiver(&node, source, None);
        // Assert
        assert_eq!(result, None);
    }
}