use crate::ecmascript;
use crate::{ContainerBody, Import, Language, LanguageSymbols, Visibility};
use tree_sitter::Node;
pub struct TypeScript;
pub struct Tsx;
impl Language for TypeScript {
fn name(&self) -> &'static str {
"TypeScript"
}
fn extensions(&self) -> &'static [&'static str] {
&["ts", "mts", "cts"]
}
fn grammar_name(&self) -> &'static str {
"typescript"
}
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__/**/*.ts",
"**/__mocks__/**/*.ts",
"**/*.test.ts",
"**/*.spec.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
]
}
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>> {
if let Some(body) = node.child_by_field_name("body") {
return Some(body);
}
for i in 0..node.child_count() as u32 {
if let Some(child) = node.child(i)
&& (child.kind() == "interface_body" || child.kind() == "class_body")
{
return Some(child);
}
}
None
}
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)
}
}
impl LanguageSymbols for TypeScript {}
impl Language for Tsx {
fn name(&self) -> &'static str {
"TSX"
}
fn extensions(&self) -> &'static [&'static str] {
&["tsx"]
}
fn grammar_name(&self) -> &'static str {
"tsx"
}
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__/**/*.ts",
"**/__mocks__/**/*.ts",
"**/*.test.ts",
"**/*.spec.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
]
}
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>> {
if let Some(body) = node.child_by_field_name("body") {
return Some(body);
}
for i in 0..node.child_count() as u32 {
if let Some(child) = node.child(i)
&& (child.kind() == "interface_body" || child.kind() == "class_body")
{
return Some(child);
}
}
None
}
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)
}
}
#[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", "enum_assignment", "enum_body", "formal_parameters", "identifier", "interface_body", "nested_identifier", "nested_type_identifier", "private_property_identifier", "property_identifier", "public_field_definition", "shorthand_property_identifier", "shorthand_property_identifier_pattern", "statement_block", "statement_identifier", "switch_body",
"default_type", "else_clause", "extends_clause", "extends_type_clause", "finally_clause", "implements_clause",
"as_expression", "assignment_expression", "augmented_assignment_expression", "await_expression", "call_expression", "function_expression", "instantiation_expression", "member_expression", "non_null_expression", "parenthesized_expression", "satisfies_expression", "sequence_expression", "subscript_expression", "unary_expression", "update_expression", "yield_expression",
"adding_type_annotation", "array_type", "conditional_type", "construct_signature", "constructor_type", "existential_type", "flow_maybe_type", "function_type", "generic_type", "index_type_query", "infer_type", "intersection_type", "literal_type", "lookup_type", "mapped_type_clause", "object_type", "omitting_type_annotation", "opting_type_annotation", "optional_type", "override_modifier", "parenthesized_type", "predefined_type", "readonly_type", "rest_type", "template_literal_type", "template_type", "this_type", "tuple_type", "type_arguments", "type_assertion", "type_parameter", "type_parameters", "type_predicate", "type_predicate_annotation", "type_query", "union_type",
"accessibility_modifier", "export_clause", "export_specifier", "import", "import_alias", "import_attribute", "import_clause", "import_require_clause", "import_specifier", "named_imports", "namespace_export", "namespace_import",
"ambient_declaration", "debugger_statement", "empty_statement", "expression_statement", "generator_function", "generator_function_declaration", "internal_module", "labeled_statement", "lexical_declaration", "using_declaration", "variable_declaration", "with_statement", "for_in_statement",
"switch_case",
"continue_statement",
"do_statement",
"return_statement",
"class",
"switch_statement",
"binary_expression",
"while_statement",
"for_statement",
"if_statement",
"throw_statement",
"try_statement",
"break_statement",
"arrow_function",
"catch_clause",
"ternary_expression",
"import_statement",
"export_statement",
];
validate_unused_kinds_audit(&TypeScript, documented_unused)
.expect("TypeScript unused node kinds audit failed");
}
}