use std::path::{Path, PathBuf};
use crate::ecmascript;
use crate::{
ContainerBody, Import, ImportSpec, Language, LanguageSymbols, ModuleId, ModuleResolver,
Resolution, ResolverConfig, Visibility,
};
use tree_sitter::Node;
pub struct JavaScript;
impl Language for JavaScript {
fn name(&self) -> &'static str {
"JavaScript"
}
fn extensions(&self) -> &'static [&'static str] {
&["js", "mjs", "cjs", "jsx"]
}
fn grammar_name(&self) -> &'static str {
"javascript"
}
fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
Some(self)
}
fn signature_suffix(&self) -> &'static str {
" {}"
}
fn extract_docstring(&self, node: &Node, content: &str) -> Option<String> {
ecmascript::extract_jsdoc(node, content)
}
fn extract_implements(&self, node: &Node, content: &str) -> crate::ImplementsInfo {
ecmascript::extract_implements(node, content)
}
fn build_signature(&self, node: &Node, content: &str) -> String {
let name = match self.node_name(node, content) {
Some(n) => n,
None => {
return content[node.byte_range()]
.lines()
.next()
.unwrap_or("")
.trim()
.to_string();
}
};
ecmascript::build_signature(node, content, name)
}
fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
ecmascript::extract_imports(node, content)
}
fn format_import(&self, import: &Import, names: Option<&[&str]>) -> String {
ecmascript::format_import(import, names)
}
fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
{
let name = symbol.name.as_str();
match symbol.kind {
crate::SymbolKind::Function | crate::SymbolKind::Method => {
name.starts_with("test_")
|| name.starts_with("Test")
|| name == "describe"
|| name == "it"
|| name == "test"
}
crate::SymbolKind::Module => {
name == "tests" || name == "test" || name == "__tests__"
}
_ => false,
}
}
}
fn test_file_globs(&self) -> &'static [&'static str] {
&[
"**/__tests__/**/*.js",
"**/__mocks__/**/*.js",
"**/*.test.js",
"**/*.spec.js",
"**/*.test.jsx",
"**/*.spec.jsx",
]
}
fn extract_attributes(&self, node: &Node, content: &str) -> Vec<String> {
ecmascript::extract_decorators(node, content)
}
fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
node.child_by_field_name("body")
}
fn analyze_container_body(
&self,
body_node: &Node,
content: &str,
inner_indent: &str,
) -> Option<ContainerBody> {
crate::body::analyze_brace_body(body_node, content, inner_indent)
}
fn get_visibility(&self, node: &Node, content: &str) -> Visibility {
ecmascript::get_visibility(node, content)
}
fn extract_module_doc(&self, src: &str) -> Option<String> {
ecmascript::extract_js_module_doc(src)
}
fn module_resolver(&self) -> Option<&dyn ModuleResolver> {
static RESOLVER: JsModuleResolver = JsModuleResolver;
Some(&RESOLVER)
}
}
impl LanguageSymbols for JavaScript {}
pub struct JsModuleResolver;
impl ModuleResolver for JsModuleResolver {
fn workspace_config(&self, root: &Path) -> ResolverConfig {
let mut search_roots: Vec<PathBuf> = Vec::new();
let mut path_mappings: Vec<(String, PathBuf)> = Vec::new();
let jsconfig_path = root.join("jsconfig.json");
if let Ok(content) = std::fs::read_to_string(&jsconfig_path)
&& let Ok(jsconfig) = serde_json::from_str::<serde_json::Value>(&content)
{
let compiler_opts = jsconfig.get("compilerOptions");
if let Some(base_url) = compiler_opts
.and_then(|o| o.get("baseUrl"))
.and_then(|v| v.as_str())
{
search_roots.push(root.join(base_url));
}
if let Some(paths) = compiler_opts
.and_then(|o| o.get("paths"))
.and_then(|v| v.as_object())
{
for (alias, targets) in paths {
if let Some(first) = targets
.as_array()
.and_then(|arr| arr.first())
.and_then(|v| v.as_str())
{
let alias_key = alias.trim_end_matches("/*").to_string();
let target_path = root.join(first.trim_end_matches("/*"));
path_mappings.push((alias_key, target_path));
}
}
}
}
ResolverConfig {
workspace_root: root.to_path_buf(),
path_mappings,
search_roots,
}
}
fn module_of_file(&self, _root: &Path, file: &Path, cfg: &ResolverConfig) -> Vec<ModuleId> {
let ext = file.extension().and_then(|e| e.to_str()).unwrap_or("");
if !matches!(ext, "js" | "mjs" | "cjs" | "jsx") {
return Vec::new();
}
let base = cfg.search_roots.first().unwrap_or(&cfg.workspace_root);
let rel = file
.strip_prefix(base)
.or_else(|_| file.strip_prefix(&cfg.workspace_root))
.unwrap_or(file);
let stem = rel.with_extension("");
let module_path = stem
.components()
.filter_map(|c| {
if let std::path::Component::Normal(s) = c {
s.to_str()
} else {
None
}
})
.collect::<Vec<_>>()
.join("/");
if module_path.is_empty() {
return Vec::new();
}
vec![ModuleId {
canonical_path: module_path,
}]
}
fn resolve(&self, from_file: &Path, spec: &ImportSpec, cfg: &ResolverConfig) -> Resolution {
let ext = from_file.extension().and_then(|e| e.to_str()).unwrap_or("");
if !matches!(ext, "js" | "mjs" | "cjs" | "jsx") {
return Resolution::NotApplicable;
}
let raw = &spec.raw;
if raw.starts_with("node_modules/") {
return Resolution::NotFound;
}
if spec.is_relative || raw.starts_with("./") || raw.starts_with("../") {
let base_dir = from_file.parent().unwrap_or(from_file);
let joined = base_dir.join(raw);
let normalized = normalize_js_path(&joined);
return resolve_js_file_candidates(&normalized);
}
for (alias, target_dir) in &cfg.path_mappings {
if raw == alias || raw.starts_with(&format!("{}/", alias)) {
let rest = raw.strip_prefix(alias).unwrap_or("");
let rest = rest.strip_prefix('/').unwrap_or(rest);
let candidate = if rest.is_empty() {
target_dir.clone()
} else {
target_dir.join(rest)
};
let result = resolve_js_file_candidates(&candidate);
if !matches!(result, Resolution::NotFound) {
return result;
}
}
}
for search_root in &cfg.search_roots {
let candidate = search_root.join(raw);
let result = resolve_js_file_candidates(&candidate);
if !matches!(result, Resolution::NotFound) {
return result;
}
}
Resolution::NotFound
}
}
fn resolve_js_file_candidates(base: &Path) -> Resolution {
let base_ext = base.extension().and_then(|e| e.to_str()).unwrap_or("");
if matches!(base_ext, "js" | "mjs" | "cjs" | "jsx") && base.exists() {
return Resolution::Resolved(base.to_path_buf(), String::new());
}
let candidates = [
base.with_extension("js"),
base.with_extension("mjs"),
base.with_extension("cjs"),
base.with_extension("jsx"),
base.join("index.js"),
base.join("index.mjs"),
];
for c in &candidates {
if c.exists() {
return Resolution::Resolved(c.clone(), String::new());
}
}
Resolution::NotFound
}
fn normalize_js_path(path: &Path) -> PathBuf {
let mut out = PathBuf::new();
for component in path.components() {
match component {
std::path::Component::ParentDir => {
out.pop();
}
std::path::Component::CurDir => {}
c => out.push(c),
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::validate_unused_kinds_audit;
#[test]
fn unused_node_kinds_audit() {
#[rustfmt::skip]
let documented_unused: &[&str] = &[
"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",
"while_statement",
"throw_statement",
"if_statement",
"for_statement",
"import_statement",
"ternary_expression",
"catch_clause",
"do_statement",
"return_statement",
"try_statement",
"for_in_statement",
"continue_statement",
"switch_statement",
"switch_case",
"arrow_function",
];
validate_unused_kinds_audit(&JavaScript, documented_unused)
.expect("JavaScript unused node kinds audit failed");
}
}