Skip to main content

normalize_languages/
batch.rs

1//! Windows Batch file support.
2
3use crate::{Import, Language, LanguageSymbols};
4use tree_sitter::Node;
5
6/// Batch language support.
7pub struct Batch;
8
9impl Language for Batch {
10    fn name(&self) -> &'static str {
11        "Batch"
12    }
13    fn extensions(&self) -> &'static [&'static str] {
14        &["bat", "cmd"]
15    }
16    fn grammar_name(&self) -> &'static str {
17        "batch"
18    }
19
20    fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
21        Some(self)
22    }
23
24    fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
25        // Batch: call script.bat
26        format!("call {}", import.module)
27    }
28
29    fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
30        let name = symbol.name.as_str();
31        match symbol.kind {
32            crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
33            crate::SymbolKind::Module => name == "tests" || name == "test",
34            _ => false,
35        }
36    }
37
38    fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
39        if let Some(name_node) = node.child_by_field_name("name") {
40            return Some(&content[name_node.byte_range()]);
41        }
42        let mut cursor = node.walk();
43        for child in node.children(&mut cursor) {
44            if child.kind() == "identifier" {
45                return Some(&content[child.byte_range()]);
46            }
47        }
48        None
49    }
50}
51
52impl LanguageSymbols for Batch {}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57    use crate::validate_unused_kinds_audit;
58
59    #[test]
60    fn unused_node_kinds_audit() {
61        #[rustfmt::skip]
62        let documented_unused: &[&str] = &[
63            "identifier",
64            // covered by tags.scm
65            "function_definition",
66            "variable_declaration",
67        ];
68        validate_unused_kinds_audit(&Batch, documented_unused)
69            .expect("Batch unused node kinds audit failed");
70    }
71}