use super::language_trait::LanguageImpl;
use tree_sitter::{Language as TSLanguage, Node};
pub struct TypeScriptLanguage {
tsx: bool,
}
impl TypeScriptLanguage {
pub fn new_typescript() -> Self {
TypeScriptLanguage { tsx: false }
}
pub fn new_tsx() -> Self {
TypeScriptLanguage { tsx: true }
}
}
impl LanguageImpl for TypeScriptLanguage {
fn get_tree_sitter_language(&self) -> TSLanguage {
if self.tsx {
tree_sitter_typescript::LANGUAGE_TSX.into()
} else {
tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()
}
}
fn get_extension(&self) -> &'static str {
if self.tsx {
"tsx"
} else {
"ts"
}
}
fn is_acceptable_parent(&self, node: &Node) -> bool {
matches!(
node.kind(),
"function_declaration"
| "method_definition"
| "class_declaration"
| "arrow_function"
| "function"
| "export_statement"
| "variable_declaration"
| "lexical_declaration"
| "interface_declaration" | "type_alias_declaration" | "enum_declaration" )
}
fn is_test_node(&self, node: &Node, source: &[u8]) -> bool {
let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";
let node_type = node.kind();
if node_type == "function_declaration"
|| node_type == "method_definition"
|| node_type == "arrow_function"
{
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "identifier" {
let name = child.utf8_text(source).unwrap_or("");
if name.contains("test") || name.contains("Test") {
if debug_mode {
println!(
"DEBUG: Test node detected (TypeScript): test function/method"
);
}
return true;
}
}
}
}
if node_type == "call_expression" {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "identifier" {
let name = child.utf8_text(source).unwrap_or("");
if name == "describe" || name == "it" || name == "test" || name == "expect" {
if debug_mode {
println!("DEBUG: Test node detected (TypeScript): {name} call");
}
return true;
}
}
}
}
false
}
}