Skip to main content

normalize_languages/
json.rs

1//! JSON 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/// JSON language support.
9pub struct Json;
10
11impl Language for Json {
12    fn name(&self) -> &'static str {
13        "JSON"
14    }
15    fn extensions(&self) -> &'static [&'static str] {
16        &["json", "jsonc"]
17    }
18    fn grammar_name(&self) -> &'static str {
19        "json"
20    }
21
22    fn has_symbols(&self) -> bool {
23        false
24    }
25
26    // JSON is data, not code - no functions/types/control flow
27    // "pair" nodes are key-value pairs that we extract as symbols
28    fn container_kinds(&self) -> &'static [&'static str] {
29        &["pair"]
30    }
31    fn function_kinds(&self) -> &'static [&'static str] {
32        &[]
33    }
34    fn type_kinds(&self) -> &'static [&'static str] {
35        &[]
36    }
37    fn import_kinds(&self) -> &'static [&'static str] {
38        &[]
39    }
40    fn public_symbol_kinds(&self) -> &'static [&'static str] {
41        &[]
42    }
43    fn visibility_mechanism(&self) -> VisibilityMechanism {
44        VisibilityMechanism::NotApplicable
45    }
46    fn scope_creating_kinds(&self) -> &'static [&'static str] {
47        &[]
48    }
49    fn control_flow_kinds(&self) -> &'static [&'static str] {
50        &[]
51    }
52    fn complexity_nodes(&self) -> &'static [&'static str] {
53        &[]
54    }
55    fn nesting_nodes(&self) -> &'static [&'static str] {
56        &[]
57    }
58
59    fn signature_suffix(&self) -> &'static str {
60        ""
61    }
62
63    fn extract_function(
64        &self,
65        _node: &Node,
66        _content: &str,
67        _in_container: bool,
68    ) -> Option<Symbol> {
69        None
70    }
71
72    fn extract_container(&self, node: &Node, content: &str) -> Option<Symbol> {
73        // Extract JSON key-value pairs as symbols
74        // node.kind() is already "pair" from container_kinds()
75        let key = node.child_by_field_name("key")?;
76        let key_text = content[key.byte_range()].trim_matches('"');
77
78        Some(Symbol {
79            name: key_text.to_string(),
80            kind: SymbolKind::Variable,
81            signature: key_text.to_string(),
82            docstring: None,
83            attributes: Vec::new(),
84            start_line: node.start_position().row + 1,
85            end_line: node.end_position().row + 1,
86            visibility: Visibility::Public,
87            children: Vec::new(),
88            is_interface_impl: false,
89            implements: Vec::new(),
90        })
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        // JSON 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        // JSON has no "interesting" unused kinds matching our patterns
201        let documented_unused: &[&str] = &[];
202        validate_unused_kinds_audit(&Json, documented_unused)
203            .expect("JSON unused node kinds audit failed");
204    }
205}