Skip to main content

normalize_languages/
meson.rs

1//! Meson build system support.
2
3use crate::{Import, Language, LanguageSymbols};
4use tree_sitter::Node;
5
6/// Meson language support.
7pub struct Meson;
8
9impl Language for Meson {
10    fn name(&self) -> &'static str {
11        "Meson"
12    }
13    fn extensions(&self) -> &'static [&'static str] {
14        &["meson.build", "meson_options.txt"]
15    }
16    fn grammar_name(&self) -> &'static str {
17        "meson"
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() != "normal_command" {
26            return Vec::new();
27        }
28
29        let text = &content[node.byte_range()];
30        if text.starts_with("subproject(") || text.starts_with("dependency(") {
31            return vec![Import {
32                module: text.to_string(),
33                names: Vec::new(),
34                alias: None,
35                is_wildcard: false,
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        // Meson: subdir('path')
46        format!("subdir('{}')", 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 node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
54        if let Some(name_node) = node.child_by_field_name("name") {
55            return Some(&content[name_node.byte_range()]);
56        }
57        if let Some(left_node) = node.child_by_field_name("left") {
58            return Some(&content[left_node.byte_range()]);
59        }
60        let mut cursor = node.walk();
61        for child in node.children(&mut cursor) {
62            if child.kind() == "identifier" {
63                return Some(&content[child.byte_range()]);
64            }
65        }
66        None
67    }
68}
69
70impl LanguageSymbols for Meson {}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75    use crate::validate_unused_kinds_audit;
76
77    #[test]
78    fn unused_node_kinds_audit() {
79        #[rustfmt::skip]
80        let documented_unused: &[&str] = &[
81            // Control flow commands
82            "else_command", "elseif_command",
83            // Expression-related
84            "formatunit", "identifier", "operatorunit", "ternaryoperator",
85            // control flow — not extracted as symbols
86            "expression_statement",
87            "foreach_command",
88            "if_condition",
89            "if_command",
90        ];
91        validate_unused_kinds_audit(&Meson, documented_unused)
92            .expect("Meson unused node kinds audit failed");
93    }
94}