Skip to main content

normalize_languages/
dot.rs

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