1use std::path::{Path, PathBuf};
4
5use crate::ecmascript;
6use crate::{
7 ContainerBody, Import, ImportSpec, Language, LanguageSymbols, ModuleId, ModuleResolver,
8 Resolution, ResolverConfig, Visibility,
9};
10use tree_sitter::Node;
11
12pub struct JavaScript;
14
15impl Language for JavaScript {
16 fn name(&self) -> &'static str {
17 "JavaScript"
18 }
19 fn extensions(&self) -> &'static [&'static str] {
20 &["js", "mjs", "cjs", "jsx"]
21 }
22 fn grammar_name(&self) -> &'static str {
23 "javascript"
24 }
25
26 fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
27 Some(self)
28 }
29
30 fn signature_suffix(&self) -> &'static str {
31 " {}"
32 }
33
34 fn extract_docstring(&self, node: &Node, content: &str) -> Option<String> {
35 ecmascript::extract_jsdoc(node, content)
36 }
37
38 fn extract_implements(&self, node: &Node, content: &str) -> crate::ImplementsInfo {
39 ecmascript::extract_implements(node, content)
40 }
41
42 fn build_signature(&self, node: &Node, content: &str) -> String {
43 let name = match self.node_name(node, content) {
44 Some(n) => n,
45 None => {
46 return content[node.byte_range()]
47 .lines()
48 .next()
49 .unwrap_or("")
50 .trim()
51 .to_string();
52 }
53 };
54 ecmascript::build_signature(node, content, name)
55 }
56
57 fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
58 ecmascript::extract_imports(node, content)
59 }
60
61 fn format_import(&self, import: &Import, names: Option<&[&str]>) -> String {
62 ecmascript::format_import(import, names)
63 }
64
65 fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
66 {
67 let name = symbol.name.as_str();
68 match symbol.kind {
69 crate::SymbolKind::Function | crate::SymbolKind::Method => {
70 name.starts_with("test_")
71 || name.starts_with("Test")
72 || name == "describe"
73 || name == "it"
74 || name == "test"
75 }
76 crate::SymbolKind::Module => {
77 name == "tests" || name == "test" || name == "__tests__"
78 }
79 _ => false,
80 }
81 }
82 }
83
84 fn test_file_globs(&self) -> &'static [&'static str] {
85 &[
86 "**/__tests__/**/*.js",
87 "**/__mocks__/**/*.js",
88 "**/*.test.js",
89 "**/*.spec.js",
90 "**/*.test.jsx",
91 "**/*.spec.jsx",
92 ]
93 }
94
95 fn extract_attributes(&self, node: &Node, content: &str) -> Vec<String> {
96 ecmascript::extract_decorators(node, content)
97 }
98
99 fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
100 node.child_by_field_name("body")
101 }
102
103 fn analyze_container_body(
104 &self,
105 body_node: &Node,
106 content: &str,
107 inner_indent: &str,
108 ) -> Option<ContainerBody> {
109 crate::body::analyze_brace_body(body_node, content, inner_indent)
110 }
111
112 fn get_visibility(&self, node: &Node, content: &str) -> Visibility {
113 ecmascript::get_visibility(node, content)
114 }
115
116 fn extract_module_doc(&self, src: &str) -> Option<String> {
117 ecmascript::extract_js_module_doc(src)
118 }
119
120 fn module_resolver(&self) -> Option<&dyn ModuleResolver> {
121 static RESOLVER: JsModuleResolver = JsModuleResolver;
122 Some(&RESOLVER)
123 }
124}
125
126impl LanguageSymbols for JavaScript {}
127
128pub struct JsModuleResolver;
139
140impl ModuleResolver for JsModuleResolver {
141 fn workspace_config(&self, root: &Path) -> ResolverConfig {
142 let mut search_roots: Vec<PathBuf> = Vec::new();
143 let mut path_mappings: Vec<(String, PathBuf)> = Vec::new();
144
145 let jsconfig_path = root.join("jsconfig.json");
147 if let Ok(content) = std::fs::read_to_string(&jsconfig_path)
148 && let Ok(jsconfig) = serde_json::from_str::<serde_json::Value>(&content)
149 {
150 let compiler_opts = jsconfig.get("compilerOptions");
151
152 if let Some(base_url) = compiler_opts
154 .and_then(|o| o.get("baseUrl"))
155 .and_then(|v| v.as_str())
156 {
157 search_roots.push(root.join(base_url));
158 }
159
160 if let Some(paths) = compiler_opts
162 .and_then(|o| o.get("paths"))
163 .and_then(|v| v.as_object())
164 {
165 for (alias, targets) in paths {
166 if let Some(first) = targets
167 .as_array()
168 .and_then(|arr| arr.first())
169 .and_then(|v| v.as_str())
170 {
171 let alias_key = alias.trim_end_matches("/*").to_string();
172 let target_path = root.join(first.trim_end_matches("/*"));
173 path_mappings.push((alias_key, target_path));
174 }
175 }
176 }
177 }
178
179 ResolverConfig {
180 workspace_root: root.to_path_buf(),
181 path_mappings,
182 search_roots,
183 }
184 }
185
186 fn module_of_file(&self, _root: &Path, file: &Path, cfg: &ResolverConfig) -> Vec<ModuleId> {
187 let ext = file.extension().and_then(|e| e.to_str()).unwrap_or("");
188 if !matches!(ext, "js" | "mjs" | "cjs" | "jsx") {
189 return Vec::new();
190 }
191
192 let base = cfg.search_roots.first().unwrap_or(&cfg.workspace_root);
193
194 let rel = file
195 .strip_prefix(base)
196 .or_else(|_| file.strip_prefix(&cfg.workspace_root))
197 .unwrap_or(file);
198
199 let stem = rel.with_extension("");
200 let module_path = stem
201 .components()
202 .filter_map(|c| {
203 if let std::path::Component::Normal(s) = c {
204 s.to_str()
205 } else {
206 None
207 }
208 })
209 .collect::<Vec<_>>()
210 .join("/");
211
212 if module_path.is_empty() {
213 return Vec::new();
214 }
215
216 vec![ModuleId {
217 canonical_path: module_path,
218 }]
219 }
220
221 fn resolve(&self, from_file: &Path, spec: &ImportSpec, cfg: &ResolverConfig) -> Resolution {
222 let ext = from_file.extension().and_then(|e| e.to_str()).unwrap_or("");
223 if !matches!(ext, "js" | "mjs" | "cjs" | "jsx") {
224 return Resolution::NotApplicable;
225 }
226
227 let raw = &spec.raw;
228
229 if raw.starts_with("node_modules/") {
231 return Resolution::NotFound;
232 }
233
234 if spec.is_relative || raw.starts_with("./") || raw.starts_with("../") {
236 let base_dir = from_file.parent().unwrap_or(from_file);
237 let joined = base_dir.join(raw);
238 let normalized = normalize_js_path(&joined);
239 return resolve_js_file_candidates(&normalized);
240 }
241
242 for (alias, target_dir) in &cfg.path_mappings {
244 if raw == alias || raw.starts_with(&format!("{}/", alias)) {
245 let rest = raw.strip_prefix(alias).unwrap_or("");
246 let rest = rest.strip_prefix('/').unwrap_or(rest);
247 let candidate = if rest.is_empty() {
248 target_dir.clone()
249 } else {
250 target_dir.join(rest)
251 };
252 let result = resolve_js_file_candidates(&candidate);
253 if !matches!(result, Resolution::NotFound) {
254 return result;
255 }
256 }
257 }
258
259 for search_root in &cfg.search_roots {
261 let candidate = search_root.join(raw);
262 let result = resolve_js_file_candidates(&candidate);
263 if !matches!(result, Resolution::NotFound) {
264 return result;
265 }
266 }
267
268 Resolution::NotFound
270 }
271}
272
273fn resolve_js_file_candidates(base: &Path) -> Resolution {
275 let base_ext = base.extension().and_then(|e| e.to_str()).unwrap_or("");
277 if matches!(base_ext, "js" | "mjs" | "cjs" | "jsx") && base.exists() {
278 return Resolution::Resolved(base.to_path_buf(), String::new());
279 }
280
281 let candidates = [
282 base.with_extension("js"),
283 base.with_extension("mjs"),
284 base.with_extension("cjs"),
285 base.with_extension("jsx"),
286 base.join("index.js"),
287 base.join("index.mjs"),
288 ];
289 for c in &candidates {
290 if c.exists() {
291 return Resolution::Resolved(c.clone(), String::new());
292 }
293 }
294 Resolution::NotFound
295}
296
297fn normalize_js_path(path: &Path) -> PathBuf {
299 let mut out = PathBuf::new();
300 for component in path.components() {
301 match component {
302 std::path::Component::ParentDir => {
303 out.pop();
304 }
305 std::path::Component::CurDir => {}
306 c => out.push(c),
307 }
308 }
309 out
310}
311
312#[cfg(test)]
313mod tests {
314 use super::*;
315 use crate::validate_unused_kinds_audit;
316
317 #[test]
320 fn unused_node_kinds_audit() {
321 #[rustfmt::skip]
322 let documented_unused: &[&str] = &[
323 "class_body", "class_heritage", "class_static_block", "formal_parameters", "field_definition", "private_property_identifier", "property_identifier", "shorthand_property_identifier", "shorthand_property_identifier_pattern", "statement_block", "statement_identifier", "switch_body", "else_clause", "finally_clause", "augmented_assignment_expression", "await_expression", "parenthesized_expression","sequence_expression", "subscript_expression", "unary_expression", "update_expression", "yield_expression", "export_clause", "export_specifier", "import", "import_attribute", "import_clause", "import_specifier", "named_imports", "namespace_export", "namespace_import", "debugger_statement", "empty_statement", "expression_statement", "labeled_statement", "using_declaration", "with_statement", "jsx_expression", "break_statement",
374 "while_statement",
375 "throw_statement",
376 "if_statement",
377 "for_statement",
378 "import_statement",
379 "ternary_expression",
380 "catch_clause",
381 "do_statement",
382 "return_statement",
383 "try_statement",
384 "for_in_statement",
385 "continue_statement",
386 "switch_statement",
387 "switch_case",
388 "arrow_function",
389 ];
390
391 validate_unused_kinds_audit(&JavaScript, documented_unused)
392 .expect("JavaScript unused node kinds audit failed");
393 }
394}