normalize_languages/
jinja2.rs1use crate::external_packages::ResolvedPackage;
4use crate::{Export, Import, Language, Symbol, Visibility, VisibilityMechanism};
5use std::path::{Path, PathBuf};
6use tree_sitter::Node;
7
8pub struct Jinja2;
10
11impl Language for Jinja2 {
12 fn name(&self) -> &'static str {
13 "Jinja2"
14 }
15 fn extensions(&self) -> &'static [&'static str] {
16 &["j2", "jinja", "jinja2"]
17 }
18 fn grammar_name(&self) -> &'static str {
19 "jinja2"
20 }
21
22 fn has_symbols(&self) -> bool {
23 true
24 }
25
26 fn container_kinds(&self) -> &'static [&'static str] {
27 &[]
28 }
29
30 fn function_kinds(&self) -> &'static [&'static str] {
31 &[]
32 }
33
34 fn type_kinds(&self) -> &'static [&'static str] {
35 &[]
36 }
37
38 fn import_kinds(&self) -> &'static [&'static str] {
39 &[]
40 }
41
42 fn public_symbol_kinds(&self) -> &'static [&'static str] {
43 &[]
44 }
45
46 fn visibility_mechanism(&self) -> VisibilityMechanism {
47 VisibilityMechanism::AllPublic
48 }
49
50 fn extract_public_symbols(&self, _node: &Node, _content: &str) -> Vec<Export> {
51 Vec::new()
53 }
54
55 fn scope_creating_kinds(&self) -> &'static [&'static str] {
56 &[]
57 }
58
59 fn control_flow_kinds(&self) -> &'static [&'static str] {
60 &[]
61 }
62
63 fn complexity_nodes(&self) -> &'static [&'static str] {
64 &[]
65 }
66
67 fn nesting_nodes(&self) -> &'static [&'static str] {
68 &[]
69 }
70
71 fn signature_suffix(&self) -> &'static str {
72 ""
73 }
74
75 fn extract_function(
76 &self,
77 _node: &Node,
78 _content: &str,
79 _in_container: bool,
80 ) -> Option<Symbol> {
81 None
83 }
84
85 fn extract_container(&self, _node: &Node, _content: &str) -> Option<Symbol> {
86 None
88 }
89
90 fn extract_type(&self, _node: &Node, _content: &str) -> Option<Symbol> {
91 None
92 }
93 fn extract_docstring(&self, _node: &Node, _content: &str) -> Option<String> {
94 None
95 }
96
97 fn extract_attributes(&self, _node: &Node, _content: &str) -> Vec<String> {
98 Vec::new()
99 }
100
101 fn extract_imports(&self, _node: &Node, _content: &str) -> Vec<Import> {
102 Vec::new()
104 }
105
106 fn format_import(&self, _import: &Import, _names: Option<&[&str]>) -> String {
107 String::new()
109 }
110
111 fn is_public(&self, _node: &Node, _content: &str) -> bool {
112 true
113 }
114 fn get_visibility(&self, _node: &Node, _content: &str) -> Visibility {
115 Visibility::Public
116 }
117
118 fn is_test_symbol(&self, _symbol: &crate::Symbol) -> bool {
119 false
120 }
121
122 fn embedded_content(&self, _node: &Node, _content: &str) -> Option<crate::EmbeddedBlock> {
123 None
124 }
125
126 fn container_body<'a>(&self, _node: &'a Node<'a>) -> Option<Node<'a>> {
127 None
128 }
129
130 fn body_has_docstring(&self, _body: &Node, _content: &str) -> bool {
131 false
132 }
133
134 fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
135 node.child_by_field_name("name")
136 .map(|n| &content[n.byte_range()])
137 }
138
139 fn file_path_to_module_name(&self, path: &Path) -> Option<String> {
140 let ext = path.extension()?.to_str()?;
141 if !["j2", "jinja", "jinja2"].contains(&ext) {
142 return None;
143 }
144 let stem = path.file_stem()?.to_str()?;
145 Some(stem.to_string())
146 }
147
148 fn module_name_to_paths(&self, module: &str) -> Vec<String> {
149 vec![
150 format!("{}.j2", module),
151 format!("{}.jinja2", module),
152 format!("{}.jinja", module),
153 ]
154 }
155
156 fn lang_key(&self) -> &'static str {
157 "jinja2"
158 }
159
160 fn is_stdlib_import(&self, _: &str, _: &Path) -> bool {
161 false
162 }
163 fn find_stdlib(&self, _: &Path) -> Option<PathBuf> {
164 None
165 }
166 fn resolve_local_import(&self, _: &str, _: &Path, _: &Path) -> Option<PathBuf> {
167 None
168 }
169 fn resolve_external_import(&self, _: &str, _: &Path) -> Option<ResolvedPackage> {
170 None
171 }
172 fn get_version(&self, _: &Path) -> Option<String> {
173 None
174 }
175 fn find_package_cache(&self, _: &Path) -> Option<PathBuf> {
176 None
177 }
178 fn indexable_extensions(&self) -> &'static [&'static str] {
179 &["j2", "jinja", "jinja2"]
180 }
181 fn package_sources(&self, _: &Path) -> Vec<crate::PackageSource> {
182 Vec::new()
183 }
184
185 fn should_skip_package_entry(&self, name: &str, is_dir: bool) -> bool {
186 use crate::traits::{has_extension, skip_dotfiles};
187 if skip_dotfiles(name) {
188 return true;
189 }
190 !is_dir && !has_extension(name, self.indexable_extensions())
191 }
192
193 fn discover_packages(&self, _: &crate::PackageSource) -> Vec<(String, PathBuf)> {
194 Vec::new()
195 }
196
197 fn package_module_name(&self, entry_name: &str) -> String {
198 entry_name
199 .strip_suffix(".j2")
200 .or_else(|| entry_name.strip_suffix(".jinja2"))
201 .or_else(|| entry_name.strip_suffix(".jinja"))
202 .unwrap_or(entry_name)
203 .to_string()
204 }
205
206 fn find_package_entry(&self, path: &Path) -> Option<PathBuf> {
207 if path.is_file() {
208 Some(path.to_path_buf())
209 } else {
210 None
211 }
212 }
213}
214
215#[cfg(test)]
216mod tests {
217 use super::*;
218 use crate::validate_unused_kinds_audit;
219
220 #[test]
221 fn unused_node_kinds_audit() {
222 #[rustfmt::skip]
223 let documented_unused: &[&str] = &[
224 "identifier", "expression", "statement", "operator",
226 "expression_begin", "expression_end", "statement_begin", "statement_end",
227 ];
228 validate_unused_kinds_audit(&Jinja2, documented_unused)
229 .expect("Jinja2 unused node kinds audit failed");
230 }
231}