normalize_languages/
nix.rs1use std::path::Path;
4
5use crate::{
6 Import, ImportSpec, Language, LanguageSymbols, ModuleId, ModuleResolver, Resolution,
7 ResolverConfig,
8};
9use tree_sitter::Node;
10
11pub struct Nix;
13
14impl Language for Nix {
15 fn name(&self) -> &'static str {
16 "Nix"
17 }
18 fn extensions(&self) -> &'static [&'static str] {
19 &["nix"]
20 }
21 fn grammar_name(&self) -> &'static str {
22 "nix"
23 }
24
25 fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
26 Some(self)
27 }
28
29 fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
30 if node.kind() != "apply_expression" {
31 return Vec::new();
32 }
33
34 let text = &content[node.byte_range()];
35 if !text.starts_with("import ") {
36 return Vec::new();
37 }
38
39 let rest = text.strip_prefix("import ").unwrap_or("").trim();
41 let module = rest.split_whitespace().next().unwrap_or(rest).to_string();
42
43 vec![Import {
44 module,
45 names: Vec::new(),
46 alias: None,
47 is_wildcard: false,
48 is_relative: rest.starts_with('.'),
49 line: node.start_position().row + 1,
50 }]
51 }
52
53 fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
54 format!("import {}", import.module)
56 }
57
58 fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
59 let name = symbol.name.as_str();
60 match symbol.kind {
61 crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
62 crate::SymbolKind::Module => name == "tests" || name == "test",
63 _ => false,
64 }
65 }
66
67 fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
68 node.child_by_field_name("attrpath")
69 .map(|n| &content[n.byte_range()])
70 }
71
72 fn module_resolver(&self) -> Option<&dyn ModuleResolver> {
73 static RESOLVER: NixModuleResolver = NixModuleResolver;
74 Some(&RESOLVER)
75 }
76}
77
78impl LanguageSymbols for Nix {}
79
80pub struct NixModuleResolver;
91
92impl ModuleResolver for NixModuleResolver {
93 fn workspace_config(&self, root: &Path) -> ResolverConfig {
94 ResolverConfig {
95 workspace_root: root.to_path_buf(),
96 path_mappings: Vec::new(),
97 search_roots: Vec::new(),
98 }
99 }
100
101 fn module_of_file(&self, _root: &Path, file: &Path, cfg: &ResolverConfig) -> Vec<ModuleId> {
102 let ext = file.extension().and_then(|e| e.to_str()).unwrap_or("");
103 if ext != "nix" {
104 return Vec::new();
105 }
106
107 let rel = file.strip_prefix(&cfg.workspace_root).unwrap_or(file);
108 let path_str = rel.to_string_lossy().into_owned();
109 if path_str.is_empty() {
110 return Vec::new();
111 }
112 vec![ModuleId {
113 canonical_path: path_str,
114 }]
115 }
116
117 fn resolve(&self, from_file: &Path, spec: &ImportSpec, cfg: &ResolverConfig) -> Resolution {
118 let ext = from_file.extension().and_then(|e| e.to_str()).unwrap_or("");
119 if ext != "nix" {
120 return Resolution::NotApplicable;
121 }
122
123 let raw = &spec.raw;
124
125 if raw.starts_with('<') {
127 return Resolution::NotFound;
128 }
129
130 if raw.starts_with("./") || raw.starts_with("../") {
132 let base_dir = from_file.parent().unwrap_or(&cfg.workspace_root);
133 let candidate = base_dir.join(raw);
134 if candidate.exists() {
135 return Resolution::Resolved(candidate, String::new());
136 }
137 return Resolution::NotFound;
138 }
139
140 Resolution::NotFound
142 }
143}
144
145#[cfg(test)]
146mod tests {
147 use super::*;
148 use crate::validate_unused_kinds_audit;
149
150 #[test]
151 fn unused_node_kinds_audit() {
152 #[rustfmt::skip]
153 let documented_unused: &[&str] = &[
154 "assert_expression", "binary_expression", "float_expression",
155 "formal", "formals", "has_attr_expression", "hpath_expression",
156 "identifier", "indented_string_expression", "integer_expression",
157 "list_expression", "let_attrset_expression", "parenthesized_expression",
158 "path_expression", "select_expression", "spath_expression",
159 "string_expression", "unary_expression", "uri_expression",
160 "variable_expression",
161 "apply_expression", "if_expression", "with_expression",
163 "let_expression",
165 "attrset_expression",
166 "rec_attrset_expression",
167 "function_expression",
168 ];
169 validate_unused_kinds_audit(&Nix, documented_unused)
170 .expect("Nix unused node kinds audit failed");
171 }
172}