Skip to main content

normalize_languages/
bash.rs

1//! Bash language support.
2
3use crate::{Import, Language, LanguageSymbols};
4use tree_sitter::Node;
5
6/// Bash language support.
7pub struct Bash;
8
9impl Language for Bash {
10    fn name(&self) -> &'static str {
11        "Bash"
12    }
13    fn extensions(&self) -> &'static [&'static str] {
14        &["sh", "bash"]
15    }
16    fn grammar_name(&self) -> &'static str {
17        "bash"
18    }
19
20    fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
21        Some(self)
22    }
23
24    fn build_signature(&self, node: &Node, content: &str) -> String {
25        let name = match self.node_name(node, content) {
26            Some(n) => n,
27            None => {
28                return content[node.byte_range()]
29                    .lines()
30                    .next()
31                    .unwrap_or("")
32                    .trim()
33                    .to_string();
34            }
35        };
36        format!("function {}", name)
37    }
38
39    fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
40        // Bash: source file or . file
41        format!("source {}", import.module)
42    }
43
44    fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
45        let name = symbol.name.as_str();
46        match symbol.kind {
47            crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
48            crate::SymbolKind::Module => name == "tests" || name == "test",
49            _ => false,
50        }
51    }
52
53    fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
54        node.child_by_field_name("body")
55    }
56}
57
58impl LanguageSymbols for Bash {}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use crate::validate_unused_kinds_audit;
64
65    #[test]
66    fn unused_node_kinds_audit() {
67        #[rustfmt::skip]
68        let documented_unused: &[&str] = &[
69            "binary_expression", "brace_expression", "c_style_for_statement",
70            "compound_statement", "declaration_command", "else_clause",
71            "heredoc_body", "parenthesized_expression", "postfix_expression",
72            "redirected_statement", "ternary_expression", "test_operator",
73            "unary_expression",
74            // control flow — not extracted as symbols
75            "if_statement",
76            "for_statement",
77            "case_statement",
78            "case_item",
79            "while_statement",
80            "elif_clause",
81        ];
82        validate_unused_kinds_audit(&Bash, documented_unused)
83            .expect("Bash unused node kinds audit failed");
84    }
85}