Skip to main content

normalize_languages/
yaml.rs

1//! YAML 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/// YAML language support.
9pub struct Yaml;
10
11impl Language for Yaml {
12    fn name(&self) -> &'static str {
13        "YAML"
14    }
15    fn extensions(&self) -> &'static [&'static str] {
16        &["yaml", "yml"]
17    }
18    fn grammar_name(&self) -> &'static str {
19        "yaml"
20    }
21
22    fn has_symbols(&self) -> bool {
23        false
24    }
25
26    // YAML is data, not code - no functions/types/control flow
27    fn container_kinds(&self) -> &'static [&'static str] {
28        &["block_mapping", "flow_mapping"]
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    fn public_symbol_kinds(&self) -> &'static [&'static str] {
40        &[]
41    }
42    fn visibility_mechanism(&self) -> VisibilityMechanism {
43        VisibilityMechanism::NotApplicable
44    }
45    fn scope_creating_kinds(&self) -> &'static [&'static str] {
46        &[]
47    }
48    fn control_flow_kinds(&self) -> &'static [&'static str] {
49        &[]
50    }
51    fn complexity_nodes(&self) -> &'static [&'static str] {
52        &[]
53    }
54    fn nesting_nodes(&self) -> &'static [&'static str] {
55        &[]
56    }
57
58    fn signature_suffix(&self) -> &'static str {
59        ""
60    }
61
62    fn extract_function(
63        &self,
64        _node: &Node,
65        _content: &str,
66        _in_container: bool,
67    ) -> Option<Symbol> {
68        None
69    }
70
71    fn extract_container(&self, node: &Node, content: &str) -> Option<Symbol> {
72        if node.kind() == "block_mapping_pair" {
73            let key = node.child_by_field_name("key")?;
74            let key_text = &content[key.byte_range()];
75
76            return Some(Symbol {
77                name: key_text.to_string(),
78                kind: SymbolKind::Variable,
79                signature: key_text.to_string(),
80                docstring: None,
81                attributes: Vec::new(),
82                start_line: node.start_position().row + 1,
83                end_line: node.end_position().row + 1,
84                visibility: Visibility::Public,
85                children: Vec::new(),
86                is_interface_impl: false,
87                implements: Vec::new(),
88            });
89        }
90        None
91    }
92
93    fn extract_type(&self, _node: &Node, _content: &str) -> Option<Symbol> {
94        None
95    }
96    fn extract_docstring(&self, _node: &Node, _content: &str) -> Option<String> {
97        None
98    }
99
100    fn extract_attributes(&self, _node: &Node, _content: &str) -> Vec<String> {
101        Vec::new()
102    }
103    fn extract_imports(&self, _node: &Node, _content: &str) -> Vec<Import> {
104        Vec::new()
105    }
106
107    fn format_import(&self, _import: &Import, _names: Option<&[&str]>) -> String {
108        // YAML has no imports
109        String::new()
110    }
111    fn extract_public_symbols(&self, _node: &Node, _content: &str) -> Vec<Export> {
112        Vec::new()
113    }
114
115    fn is_public(&self, _node: &Node, _content: &str) -> bool {
116        true
117    }
118    fn get_visibility(&self, _node: &Node, _content: &str) -> Visibility {
119        Visibility::Public
120    }
121
122    fn is_test_symbol(&self, _symbol: &crate::Symbol) -> bool {
123        false
124    }
125
126    fn embedded_content(&self, _node: &Node, _content: &str) -> Option<crate::EmbeddedBlock> {
127        None
128    }
129
130    fn container_body<'a>(&self, _node: &'a Node<'a>) -> Option<Node<'a>> {
131        None
132    }
133    fn body_has_docstring(&self, _body: &Node, _content: &str) -> bool {
134        false
135    }
136    fn node_name<'a>(&self, _node: &Node, _content: &'a str) -> Option<&'a str> {
137        None
138    }
139
140    fn file_path_to_module_name(&self, _: &Path) -> Option<String> {
141        None
142    }
143    fn module_name_to_paths(&self, _: &str) -> Vec<String> {
144        Vec::new()
145    }
146
147    fn lang_key(&self) -> &'static str {
148        ""
149    }
150    fn resolve_local_import(&self, _: &str, _: &Path, _: &Path) -> Option<PathBuf> {
151        None
152    }
153    fn resolve_external_import(&self, _: &str, _: &Path) -> Option<ResolvedPackage> {
154        None
155    }
156    fn is_stdlib_import(&self, _: &str, _: &Path) -> bool {
157        false
158    }
159    fn get_version(&self, _: &Path) -> Option<String> {
160        None
161    }
162    fn find_package_cache(&self, _: &Path) -> Option<PathBuf> {
163        None
164    }
165    fn indexable_extensions(&self) -> &'static [&'static str] {
166        &[]
167    }
168    fn find_stdlib(&self, _: &Path) -> Option<PathBuf> {
169        None
170    }
171    fn package_module_name(&self, name: &str) -> String {
172        name.to_string()
173    }
174    fn package_sources(&self, _: &Path) -> Vec<crate::PackageSource> {
175        Vec::new()
176    }
177    fn discover_packages(&self, _: &crate::PackageSource) -> Vec<(String, PathBuf)> {
178        Vec::new()
179    }
180    fn find_package_entry(&self, _: &Path) -> Option<PathBuf> {
181        None
182    }
183
184    fn should_skip_package_entry(&self, name: &str, is_dir: bool) -> bool {
185        use crate::traits::{has_extension, skip_dotfiles};
186        if skip_dotfiles(name) {
187            return true;
188        }
189        !is_dir && !has_extension(name, self.indexable_extensions())
190    }
191}
192
193#[cfg(test)]
194mod tests {
195    use super::*;
196    use crate::validate_unused_kinds_audit;
197
198    #[test]
199    fn unused_node_kinds_audit() {
200        #[rustfmt::skip]
201        let documented_unused: &[&str] = &[
202            "block_mapping_pair", "block_node", "block_scalar",
203            "block_sequence", "block_sequence_item",
204        ];
205        validate_unused_kinds_audit(&Yaml, documented_unused)
206            .expect("YAML unused node kinds audit failed");
207    }
208}