Skip to main content

normalize_languages/
nginx.rs

1//! Nginx configuration file support.
2
3use crate::{ContainerBody, Import, Language, LanguageSymbols};
4use tree_sitter::Node;
5
6/// Nginx language support.
7pub struct Nginx;
8
9impl Language for Nginx {
10    fn name(&self) -> &'static str {
11        "Nginx"
12    }
13    fn extensions(&self) -> &'static [&'static str] {
14        &["nginx", "conf"]
15    }
16    fn grammar_name(&self) -> &'static str {
17        "nginx"
18    }
19
20    fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
21        Some(self)
22    }
23
24    fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
25        if node.kind() != "directive" {
26            return Vec::new();
27        }
28
29        let text = &content[node.byte_range()];
30        if let Some(rest) = text.strip_prefix("include ") {
31            return vec![Import {
32                module: rest.trim_end_matches(';').trim().to_string(),
33                names: Vec::new(),
34                alias: None,
35                is_wildcard: text.contains('*'),
36                is_relative: false,
37                line: node.start_position().row + 1,
38            }];
39        }
40
41        Vec::new()
42    }
43
44    fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
45        // Nginx: include path
46        format!("include {}", import.module)
47    }
48
49    fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
50        node.child_by_field_name("body")
51    }
52
53    fn analyze_container_body(
54        &self,
55        body_node: &Node,
56        content: &str,
57        inner_indent: &str,
58    ) -> Option<ContainerBody> {
59        crate::body::analyze_brace_body(body_node, content, inner_indent)
60    }
61
62    fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
63        // For nginx blocks, the name is the directive name (server, location, etc.)
64        if let Some(dir_node) = node.child_by_field_name("directive") {
65            return Some(&content[dir_node.byte_range()]);
66        }
67        let mut cursor = node.walk();
68        if let Some(first_child) = node.children(&mut cursor).next() {
69            return Some(&content[first_child.byte_range()]);
70        }
71        None
72    }
73}
74
75impl LanguageSymbols for Nginx {}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80    use crate::validate_unused_kinds_audit;
81
82    #[test]
83    fn unused_node_kinds_audit() {
84        #[rustfmt::skip]
85        let documented_unused: &[&str] = &[
86            "lua_block", "lua_block_directive", "modifier",
87            // structural node, not extracted as symbols
88            "block",
89        ];
90        validate_unused_kinds_audit(&Nginx, documented_unused)
91            .expect("Nginx unused node kinds audit failed");
92    }
93}