1use crate::external_packages::ResolvedPackage;
4use crate::{Export, Import, Language, Symbol, SymbolKind, Visibility, VisibilityMechanism};
5use std::path::{Path, PathBuf};
6use tree_sitter::Node;
7
8pub struct Bash;
10
11impl Language for Bash {
12 fn name(&self) -> &'static str {
13 "Bash"
14 }
15 fn extensions(&self) -> &'static [&'static str] {
16 &["sh", "bash"]
17 }
18 fn grammar_name(&self) -> &'static str {
19 "bash"
20 }
21
22 fn has_symbols(&self) -> bool {
23 true
24 }
25
26 fn container_kinds(&self) -> &'static [&'static str] {
27 &[]
28 }
29 fn function_kinds(&self) -> &'static [&'static str] {
30 &["function_definition"]
31 }
32 fn type_kinds(&self) -> &'static [&'static str] {
33 &[]
34 }
35 fn import_kinds(&self) -> &'static [&'static str] {
36 &[]
37 }
38 fn public_symbol_kinds(&self) -> &'static [&'static str] {
39 &["function_definition"]
40 }
41 fn visibility_mechanism(&self) -> VisibilityMechanism {
42 VisibilityMechanism::AllPublic
43 }
44 fn scope_creating_kinds(&self) -> &'static [&'static str] {
45 &["subshell", "command_substitution"]
46 }
47
48 fn control_flow_kinds(&self) -> &'static [&'static str] {
49 &[
50 "if_statement",
51 "for_statement",
52 "while_statement",
53 "case_statement",
54 ]
55 }
56
57 fn complexity_nodes(&self) -> &'static [&'static str] {
58 &[
59 "if_statement",
60 "elif_clause",
61 "for_statement",
62 "while_statement",
63 "case_statement",
64 "case_item",
65 "pipeline", "list", ]
68 }
69
70 fn nesting_nodes(&self) -> &'static [&'static str] {
71 &[
72 "if_statement",
73 "for_statement",
74 "while_statement",
75 "case_statement",
76 "function_definition",
77 "subshell",
78 ]
79 }
80
81 fn signature_suffix(&self) -> &'static str {
82 ""
83 }
84
85 fn extract_function(&self, node: &Node, content: &str, _in_container: bool) -> Option<Symbol> {
86 let name = self.node_name(node, content)?;
87 Some(Symbol {
88 name: name.to_string(),
89 kind: SymbolKind::Function,
90 signature: format!("function {}", name),
91 docstring: None,
92 attributes: Vec::new(),
93 start_line: node.start_position().row + 1,
94 end_line: node.end_position().row + 1,
95 visibility: Visibility::Public,
96 children: Vec::new(),
97 is_interface_impl: false,
98 implements: Vec::new(),
99 })
100 }
101
102 fn extract_container(&self, _node: &Node, _content: &str) -> Option<Symbol> {
103 None
104 }
105
106 fn extract_type(&self, _node: &Node, _content: &str) -> Option<Symbol> {
107 None
108 }
109 fn extract_docstring(&self, _node: &Node, _content: &str) -> Option<String> {
110 None
111 }
112
113 fn extract_attributes(&self, _node: &Node, _content: &str) -> Vec<String> {
114 Vec::new()
115 }
116 fn extract_imports(&self, _node: &Node, _content: &str) -> Vec<Import> {
117 Vec::new()
118 }
119
120 fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
121 format!("source {}", import.module)
123 }
124 fn extract_public_symbols(&self, node: &Node, content: &str) -> Vec<Export> {
125 if node.kind() != "function_definition" {
126 return Vec::new();
127 }
128 let name = match self.node_name(node, content) {
129 Some(n) => n.to_string(),
130 None => return Vec::new(),
131 };
132 vec![Export {
133 name,
134 kind: SymbolKind::Function,
135 line: node.start_position().row + 1,
136 }]
137 }
138
139 fn is_public(&self, _node: &Node, _content: &str) -> bool {
140 true
141 }
142 fn get_visibility(&self, _node: &Node, _content: &str) -> Visibility {
143 Visibility::Public
144 }
145
146 fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
147 let name = symbol.name.as_str();
148 match symbol.kind {
149 crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
150 crate::SymbolKind::Module => name == "tests" || name == "test",
151 _ => false,
152 }
153 }
154
155 fn embedded_content(&self, _node: &Node, _content: &str) -> Option<crate::EmbeddedBlock> {
156 None
157 }
158
159 fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
160 node.child_by_field_name("body")
161 }
162 fn body_has_docstring(&self, _body: &Node, _content: &str) -> bool {
163 false
164 }
165
166 fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
167 let name_node = node.child_by_field_name("name")?;
168 Some(&content[name_node.byte_range()])
169 }
170
171 fn file_path_to_module_name(&self, _: &Path) -> Option<String> {
172 None
173 }
174 fn module_name_to_paths(&self, _: &str) -> Vec<String> {
175 Vec::new()
176 }
177
178 fn lang_key(&self) -> &'static str {
179 "bash"
180 }
181 fn resolve_local_import(&self, _: &str, _: &Path, _: &Path) -> Option<PathBuf> {
182 None
183 }
184 fn resolve_external_import(&self, _: &str, _: &Path) -> Option<ResolvedPackage> {
185 None
186 }
187 fn is_stdlib_import(&self, _: &str, _: &Path) -> bool {
188 false
189 }
190 fn get_version(&self, _: &Path) -> Option<String> {
191 None
192 }
193 fn find_package_cache(&self, _: &Path) -> Option<PathBuf> {
194 None
195 }
196 fn indexable_extensions(&self) -> &'static [&'static str] {
197 &["sh", "bash"]
198 }
199 fn find_stdlib(&self, _: &Path) -> Option<PathBuf> {
200 None
201 }
202 fn package_module_name(&self, name: &str) -> String {
203 name.to_string()
204 }
205 fn package_sources(&self, _: &Path) -> Vec<crate::PackageSource> {
206 Vec::new()
207 }
208 fn discover_packages(&self, _: &crate::PackageSource) -> Vec<(String, PathBuf)> {
209 Vec::new()
210 }
211 fn find_package_entry(&self, path: &Path) -> Option<PathBuf> {
212 if path.is_file() {
213 Some(path.to_path_buf())
214 } else {
215 None
216 }
217 }
218
219 fn should_skip_package_entry(&self, name: &str, is_dir: bool) -> bool {
220 use crate::traits::{has_extension, skip_dotfiles};
221 if skip_dotfiles(name) {
222 return true;
223 }
224 !is_dir && !has_extension(name, self.indexable_extensions())
225 }
226}
227
228#[cfg(test)]
229mod tests {
230 use super::*;
231 use crate::validate_unused_kinds_audit;
232
233 #[test]
234 fn unused_node_kinds_audit() {
235 #[rustfmt::skip]
236 let documented_unused: &[&str] = &[
237 "binary_expression", "brace_expression", "c_style_for_statement",
238 "compound_statement", "declaration_command", "else_clause",
239 "heredoc_body", "parenthesized_expression", "postfix_expression",
240 "redirected_statement", "ternary_expression", "test_operator",
241 "unary_expression",
242 ];
243 validate_unused_kinds_audit(&Bash, documented_unused)
244 .expect("Bash unused node kinds audit failed");
245 }
246}