Skip to main content

normalize_languages/
vue.rs

1//! Vue language support.
2
3use crate::component::extract_embedded_content;
4use crate::{ContainerBody, Import, Language, LanguageEmbedded, LanguageSymbols};
5use tree_sitter::Node;
6
7/// Vue language support.
8pub struct Vue;
9
10impl Language for Vue {
11    fn name(&self) -> &'static str {
12        "Vue"
13    }
14    fn extensions(&self) -> &'static [&'static str] {
15        &["vue"]
16    }
17    fn grammar_name(&self) -> &'static str {
18        "vue"
19    }
20
21    fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
22        Some(self)
23    }
24
25    fn format_import(&self, import: &Import, names: Option<&[&str]>) -> String {
26        // Vue uses JS import syntax
27        let names_to_use: Vec<&str> = names
28            .map(|n| n.to_vec())
29            .unwrap_or_else(|| import.names.iter().map(|s| s.as_str()).collect());
30        if names_to_use.is_empty() {
31            format!("import '{}';", import.module)
32        } else {
33            format!(
34                "import {{ {} }} from '{}';",
35                names_to_use.join(", "),
36                import.module
37            )
38        }
39    }
40
41    fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
42        {
43            let name = symbol.name.as_str();
44            match symbol.kind {
45                crate::SymbolKind::Function | crate::SymbolKind::Method => {
46                    name.starts_with("test_")
47                        || name.starts_with("Test")
48                        || name == "describe"
49                        || name == "it"
50                        || name == "test"
51                }
52                crate::SymbolKind::Module => {
53                    name == "tests" || name == "test" || name == "__tests__"
54                }
55                _ => false,
56            }
57        }
58    }
59
60    fn as_embedded(&self) -> Option<&dyn LanguageEmbedded> {
61        Some(self)
62    }
63
64    fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
65        // Vue script/style/template elements contain a raw_text child
66        let mut c = node.walk();
67        node.children(&mut c)
68            .find(|&child| child.kind() == "raw_text")
69    }
70    fn analyze_container_body(
71        &self,
72        body_node: &Node,
73        content: &str,
74        inner_indent: &str,
75    ) -> Option<ContainerBody> {
76        // raw_text node from script/style/template element — content after leading newline
77        crate::body::analyze_end_body(body_node, content, inner_indent)
78    }
79}
80
81impl LanguageSymbols for Vue {}
82
83impl LanguageEmbedded for Vue {
84    fn embedded_content(&self, node: &Node, content: &str) -> Option<crate::EmbeddedBlock> {
85        extract_embedded_content(node, content)
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92    use crate::validate_unused_kinds_audit;
93
94    #[test]
95    fn unused_node_kinds_audit() {
96        #[rustfmt::skip]
97        let documented_unused: &[&str] = &[
98            "directive_modifier", "directive_modifiers", "doctype",
99        ];
100
101        validate_unused_kinds_audit(&Vue, documented_unused)
102            .expect("Vue unused node kinds audit failed");
103    }
104}