normalize_languages/
starlark.rs1use crate::{Import, Language, LanguageSymbols};
4use tree_sitter::Node;
5
6pub struct Starlark;
8
9impl Language for Starlark {
10 fn name(&self) -> &'static str {
11 "Starlark"
12 }
13 fn extensions(&self) -> &'static [&'static str] {
14 &["star", "bzl"]
15 }
16 fn grammar_name(&self) -> &'static str {
17 "starlark"
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() != "load_statement" {
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 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!("load(\"{}\")", import.module)
47 } else {
48 let quoted: Vec<String> = names_to_use.iter().map(|n| format!("\"{}\"", n)).collect();
49 format!("load(\"{}\", {})", import.module, quoted.join(", "))
50 }
51 }
52
53 fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
54 let name = symbol.name.as_str();
55 match symbol.kind {
56 crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
57 crate::SymbolKind::Module => name == "tests" || name == "test",
58 _ => false,
59 }
60 }
61
62 fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
63 node.child_by_field_name("body")
64 }
65}
66
67impl LanguageSymbols for Starlark {}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72 use crate::validate_unused_kinds_audit;
73
74 #[test]
75 fn unused_node_kinds_audit() {
76 #[rustfmt::skip]
77 let documented_unused: &[&str] = &[
78 "block", "module",
80 "pass_statement", "break_statement", "continue_statement", "return_statement",
82 "expression_statement", "else_clause", "elif_clause", "if_clause", "for_in_clause",
83 "expression", "primary_expression", "parenthesized_expression",
85 "binary_operator", "boolean_operator", "comparison_operator",
86 "unary_operator", "not_operator",
87 "list_comprehension", "dictionary_comprehension",
89 "lambda", "lambda_parameters",
91 "identifier",
93 "for_statement",
95 "load_statement",
96 "if_statement",
97 "conditional_expression",
98 ];
99 validate_unused_kinds_audit(&Starlark, documented_unused)
100 .expect("Starlark unused node kinds audit failed");
101 }
102}