normalize-languages 0.3.2

Tree-sitter language support and dynamic grammar loading
Documentation
//! Erlang language support.

use crate::traits::{ImportSpec, ModuleId, ModuleResolver, Resolution, ResolverConfig};
use crate::{Import, Language, LanguageSymbols};
use std::path::Path;
use tree_sitter::Node;

/// Erlang language support.
pub struct Erlang;

impl Language for Erlang {
    fn name(&self) -> &'static str {
        "Erlang"
    }
    fn extensions(&self) -> &'static [&'static str] {
        &["erl", "hrl"]
    }
    fn grammar_name(&self) -> &'static str {
        "erlang"
    }

    fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
        Some(self)
    }

    fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
        if node.kind() != "module_attribute" {
            return Vec::new();
        }

        let text = &content[node.byte_range()];
        let line = node.start_position().row + 1;

        // Handle -import(module, [...]).
        if text.starts_with("-import(")
            && let Some(start) = text.find('(')
        {
            let rest = &text[start + 1..];
            if let Some(comma) = rest.find(',') {
                let module = rest[..comma].trim().to_string();
                return vec![Import {
                    module,
                    names: Vec::new(),
                    alias: None,
                    is_wildcard: false,
                    is_relative: false,
                    line,
                }];
            }
        }

        // Handle -include("file.hrl"). or -include_lib("app/include/file.hrl").
        if text.starts_with("-include")
            && let Some(start) = text.find('"')
        {
            let rest = &text[start + 1..];
            if let Some(end) = rest.find('"') {
                let module = rest[..end].to_string();
                return vec![Import {
                    module,
                    names: Vec::new(),
                    alias: None,
                    is_wildcard: false,
                    is_relative: text.starts_with("-include("),
                    line,
                }];
            }
        }

        Vec::new()
    }

    fn format_import(&self, import: &Import, names: Option<&[&str]>) -> String {
        // Erlang: -import(module, [func/arity, ...]).
        let names_to_use: Vec<&str> = names
            .map(|n| n.to_vec())
            .unwrap_or_else(|| import.names.iter().map(|s| s.as_str()).collect());
        if names_to_use.is_empty() {
            format!("-import({}, []).", import.module)
        } else {
            format!("-import({}, [{}]).", import.module, names_to_use.join(", "))
        }
    }

    fn extract_attributes(&self, node: &Node, content: &str) -> Vec<String> {
        let mut attrs = Vec::new();
        let mut prev = node.prev_sibling();
        while let Some(sibling) = prev {
            if sibling.kind() == "module_attribute" {
                let text = content[sibling.byte_range()].trim();
                if text.starts_with("-spec(")
                    || text.starts_with("-spec ")
                    || text.starts_with("-callback(")
                    || text.starts_with("-deprecated(")
                    || text.starts_with("-deprecated.")
                {
                    attrs.insert(0, text.to_string());
                }
                prev = sibling.prev_sibling();
            } else if sibling.kind() == "comment" {
                prev = sibling.prev_sibling();
            } else {
                break;
            }
        }
        attrs
    }

    fn build_signature(&self, node: &Node, content: &str) -> String {
        if node.kind() == "function_clause"
            && let Some(name_node) = node.child_by_field_name("name")
        {
            let name = &content[name_node.byte_range()];
            let arity = node
                .child_by_field_name("arguments")
                .map(|args| {
                    let mut cursor = args.walk();
                    args.children(&mut cursor).count()
                })
                .unwrap_or(0);
            return format!("{}/{}", name, arity);
        }
        let text = &content[node.byte_range()];
        text.lines().next().unwrap_or(text).trim().to_string()
    }

    fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
        let name = symbol.name.as_str();
        match symbol.kind {
            crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
            crate::SymbolKind::Module => name == "tests" || name == "test",
            _ => false,
        }
    }

    fn test_file_globs(&self) -> &'static [&'static str] {
        &["**/*_SUITE.erl", "**/*_test.erl", "**/*_tests.erl"]
    }

    fn module_resolver(&self) -> Option<&dyn ModuleResolver> {
        static RESOLVER: ErlangModuleResolver = ErlangModuleResolver;
        Some(&RESOLVER)
    }
}

impl LanguageSymbols for Erlang {}

// =============================================================================
// Erlang Module Resolver
// =============================================================================

/// Module resolver for Erlang.
///
/// Erlang module = file (1:1). `foo.erl` is module `foo`.
/// Resolves `-import(mymodule, [...])` → `mymodule.erl` in `src/` or `lib/`.
pub struct ErlangModuleResolver;

impl ModuleResolver for ErlangModuleResolver {
    fn workspace_config(&self, root: &Path) -> ResolverConfig {
        ResolverConfig {
            workspace_root: root.to_path_buf(),
            path_mappings: Vec::new(),
            search_roots: vec![root.join("src"), root.join("lib"), root.to_path_buf()],
        }
    }

    fn module_of_file(&self, _root: &Path, file: &Path, _cfg: &ResolverConfig) -> Vec<ModuleId> {
        let ext = file.extension().and_then(|e| e.to_str()).unwrap_or("");
        if ext != "erl" && ext != "hrl" {
            return Vec::new();
        }
        if let Some(stem) = file.file_stem().and_then(|s| s.to_str()) {
            return vec![ModuleId {
                canonical_path: stem.to_string(),
            }];
        }
        Vec::new()
    }

    fn resolve(&self, from_file: &Path, spec: &ImportSpec, cfg: &ResolverConfig) -> Resolution {
        let ext = from_file.extension().and_then(|e| e.to_str()).unwrap_or("");
        if ext != "erl" && ext != "hrl" {
            return Resolution::NotApplicable;
        }
        let raw = &spec.raw;

        // Relative include imports (.hrl files)
        if (spec.is_relative || raw.ends_with(".hrl") || raw.ends_with(".erl"))
            && let Some(parent) = from_file.parent()
        {
            let candidate = parent.join(raw);
            if candidate.exists() {
                let name = candidate
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .unwrap_or("")
                    .to_string();
                return Resolution::Resolved(candidate, name);
            }
        }

        for search_root in &cfg.search_roots {
            for ext_try in &["erl", "hrl"] {
                let candidate = search_root.join(format!("{}.{}", raw, ext_try));
                if candidate.exists() {
                    return Resolution::Resolved(candidate, raw.to_string());
                }
            }
        }
        Resolution::NotFound
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::validate_unused_kinds_audit;

    #[test]
    fn unused_node_kinds_audit() {
        #[rustfmt::skip]
        let documented_unused: &[&str] = &[
            "ann_type", "b_generator", "binary_comprehension", "bit_type_list",
            "bit_type_unit", "block_expr", "catch_expr", "clause_body",
            "cond_match_expr", "deprecated_module", "export_attribute",
            "export_type_attribute", "field_type", "fun_type", "fun_type_sig",
            "generator", "guard_clause", "import_attribute", "list_comprehension",
            "map_comprehension", "map_generator", "match_expr", "module",
            "pp_elif", "pp_else", "pp_endif", "pp_if", "pp_ifdef", "pp_ifndef",
            "range_type", "remote_module", "replacement_cr_clauses",
            "replacement_function_clauses", "ssr_definition", "try_after",
            "try_class", "try_stack", "type_guards", "type_name", "type_sig",
            // control flow — not extracted as symbols
            "cr_clause",
            "try_expr",
            "fun_clause",
            "if_expr",
            "if_clause",
            "case_expr",
            "catch_clause",
        ];
        validate_unused_kinds_audit(&Erlang, documented_unused)
            .expect("Erlang unused node kinds audit failed");
    }
}