srgn/scoping/langs/
typescript.rs

1use std::fmt::Debug;
2
3use clap::ValueEnum;
4
5use super::{Find, LanguageScoper, QuerySource, TSLanguage, TSQuery, TSQueryError};
6
7/// A compiled query for the TypeScript language.
8#[derive(Debug)]
9pub struct CompiledQuery(super::CompiledQuery);
10
11impl TryFrom<QuerySource> for CompiledQuery {
12    type Error = TSQueryError;
13
14    /// Create a new compiled query for the TypeScript language.
15    ///
16    /// # Errors
17    ///
18    /// See the concrete type of the [`TSQueryError`](tree_sitter::QueryError)variant for when this method errors.
19    fn try_from(query: QuerySource) -> Result<Self, Self::Error> {
20        let q = super::CompiledQuery::from_source(
21            &tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
22            &query,
23        )?;
24        Ok(Self(q))
25    }
26}
27
28impl From<PreparedQuery> for CompiledQuery {
29    fn from(query: PreparedQuery) -> Self {
30        Self(super::CompiledQuery::from_prepared_query(
31            &tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
32            query.as_str(),
33        ))
34    }
35}
36
37/// Prepared tree-sitter queries for TypeScript.
38#[derive(Debug, Clone, Copy, ValueEnum)]
39pub enum PreparedQuery {
40    /// Comments.
41    Comments,
42    /// Strings (literal, template).
43    Strings,
44    /// Imports (module specifiers).
45    Imports,
46    /// Any `function` definitions.
47    Function,
48    /// `async function` definitions.
49    AsyncFunction,
50    /// Non-`async function` definitions.
51    SyncFunction,
52    /// Method definitions.
53    Method,
54    /// `constructor` method definitions.
55    Constructor,
56    /// `class` definitions.
57    Class,
58    /// `enum` definitions.
59    Enum,
60    /// `interface` definitions.
61    Interface,
62    /// `try`/`catch`/`finally` blocks.
63    TryCatch,
64    /// Variable declarations (`let`, `const`, `var`).
65    VarDecl,
66    /// `let` variable declarations.
67    Let,
68    /// `const` variable declarations.
69    Const,
70    /// `var` variable declarations.
71    Var,
72    /// Type (generic) parameters.
73    TypeParams,
74    /// Type alias declarations.
75    TypeAlias,
76    /// `namespace` blocks.
77    Namespace,
78    /// `export` blocks.
79    Export,
80}
81
82impl PreparedQuery {
83    const fn as_str(self) -> &'static str {
84        match self {
85            Self::Comments => "(comment) @comment",
86            Self::Imports => r"(import_statement source: (string (string_fragment) @sf))",
87            Self::Strings => "(string_fragment) @string",
88            Self::Function => "(function_declaration) @func",
89            Self::AsyncFunction => {
90                r#"(
91                    (function_declaration) @func (#match? @func "^async")
92                )"#
93            }
94            Self::SyncFunction => {
95                r#"(
96                    (function_declaration) @func (#not-match? @func "^async")
97                )"#
98            }
99            Self::Method => "(method_definition) @method",
100            Self::Constructor => {
101                r#"(method_definition
102                    name: (_) @name (#eq? @name "constructor")
103                ) @constructor"#
104            }
105            Self::Class => "(class_declaration) @class",
106            Self::Enum => "(enum_declaration) @enum",
107            Self::Interface => "(interface_declaration) @interface",
108            Self::TryCatch => "(try_statement) @try",
109            Self::VarDecl => "(variable_declarator) @var_decl",
110            Self::Let => {
111                r#"(
112                    (lexical_declaration) @let_decl (#match? @let_decl "^let ")
113                )"#
114            }
115            Self::Const => {
116                r#"(
117                    (lexical_declaration) @const_decl (#match? @const_decl "^const ")
118                )"#
119            }
120            Self::Var => {
121                r#"(
122                    (variable_declaration) @var_decl (#match? @var_decl "^var ")
123                )"#
124            }
125            Self::TypeParams => "(type_parameters) @type_parameters",
126            Self::TypeAlias => "(type_alias_declaration) @type_alias_declaration",
127            Self::Namespace => "(internal_module) @internal_module",
128            Self::Export => "(export_statement) @export",
129        }
130    }
131}
132
133impl LanguageScoper for CompiledQuery {
134    fn lang() -> TSLanguage {
135        tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()
136    }
137
138    fn pos_query(&self) -> &TSQuery {
139        &self.0.positive_query
140    }
141
142    fn neg_query(&self) -> Option<&TSQuery> {
143        self.0.negative_query.as_ref()
144    }
145}
146
147impl Find for CompiledQuery {
148    fn extensions(&self) -> &'static [&'static str] {
149        &["ts", "tsx"]
150    }
151}