Skip to main content

normalize_languages/
wit.rs

1//! WebAssembly Interface Types (WIT) support.
2
3use crate::{ContainerBody, Import, Language, LanguageSymbols};
4use tree_sitter::Node;
5
6/// WIT language support.
7pub struct Wit;
8
9impl Language for Wit {
10    fn name(&self) -> &'static str {
11        "WIT"
12    }
13    fn extensions(&self) -> &'static [&'static str] {
14        &["wit"]
15    }
16    fn grammar_name(&self) -> &'static str {
17        "wit"
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() != "use_item" {
26            return Vec::new();
27        }
28
29        let text = &content[node.byte_range()];
30        vec![Import {
31            module: text.trim().to_string(),
32            names: Vec::new(),
33            alias: None,
34            is_wildcard: false,
35            is_relative: false,
36            line: node.start_position().row + 1,
37        }]
38    }
39
40    fn format_import(&self, import: &Import, names: Option<&[&str]>) -> String {
41        // WIT: use interface.{func1, func2}
42        let names_to_use: Vec<&str> = names
43            .map(|n| n.to_vec())
44            .unwrap_or_else(|| import.names.iter().map(|s| s.as_str()).collect());
45        if names_to_use.is_empty() {
46            format!("use {}", import.module)
47        } else {
48            format!("use {}.{{{}}}", import.module, names_to_use.join(", "))
49        }
50    }
51
52    fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
53        node.child_by_field_name("body")
54    }
55
56    fn analyze_container_body(
57        &self,
58        body_node: &Node,
59        content: &str,
60        inner_indent: &str,
61    ) -> Option<ContainerBody> {
62        crate::body::analyze_brace_body(body_node, content, inner_indent)
63    }
64}
65
66impl LanguageSymbols for Wit {}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71    use crate::validate_unused_kinds_audit;
72
73    #[test]
74    fn unused_node_kinds_audit() {
75        #[rustfmt::skip]
76        let documented_unused: &[&str] = &[
77            // Items
78            "export_item", "import_item",
79            // Types
80            "named_type", "extern_type", "func_type",
81            // Enums/variants
82            "enum_items", "enum_case", "variant_items", "variant_case",
83            // Resources
84            "resource_method",
85            // Other
86            "definitions", "body", "block_comment",
87            // covered by tags.scm
88            "interface_item",
89            "type_item",
90        ];
91        validate_unused_kinds_audit(&Wit, documented_unused)
92            .expect("WIT unused node kinds audit failed");
93    }
94}