1; Variables
2
3(identifier) @variable
4
5; Special identifiers
6
7(type_annotation) @type
8
9(type_identifier) @type
10(predefined_type) @type.builtin
11
12(type_alias_declaration
13 (type_identifier) @type)
14
15(type_alias_declaration
16 value: (_
17 (type_identifier) @type))
18
19(interface_declaration
20 (type_identifier) @type)
21
22(class_declaration
23 (type_identifier) @type.class)
24
25(extends_clause
26 value: (identifier) @type.class)
27
28(extends_type_clause
29 type: (type_identifier) @type)
30
31(implements_clause
32 (type_identifier) @type)
33
34;; Enables ts-pretty-errors
35;; The Lsp returns "snippets" of typescript, which are not valid typescript in totality,
36;; but should still be highlighted
37;; Highlights object literals by hijacking the statement_block pattern, but only if
38;; the statement block follows an object literal pattern
39((statement_block
40 (labeled_statement
41 ;; highlight the label like a property name
42 label: (statement_identifier) @property.name
43 body: [
44 ;; match a terminating expression statement
45 (expression_statement
46 ;; single identifier - treat as a type name
47 [(identifier) @type.name
48 ;; object - treat as a property - type pair
49 (object
50 (pair
51 key: (_) @property.name
52 value: (_) @type.name))
53 ;; subscript_expression - treat as an array declaration
54 (subscript_expression
55 object: (_) @type.name
56 index: (_)
57 )
58 ;; templated string - treat each identifier contained as a type name
59 (template_string
60 (template_substitution
61 (identifier) @type.name))
62 ])
63 ;; match a nested statement block
64 (statement_block) @nested
65 ])))
66
67(import_specifier
68 "type"
69 name: (identifier) @type
70 alias: (identifier) @type
71)
72
73(import_statement
74 "type"
75 (import_clause
76 (named_imports
77 (import_specifier
78 name: (identifier) @type
79 alias: (identifier) @type
80 )
81 )
82 )
83)
84
85([
86 (identifier)
87 (shorthand_property_identifier)
88 (shorthand_property_identifier_pattern)
89 ] @constant
90 (#match? @constant "^_*[A-Z_][A-Z\\d_]*$"))
91
92; Properties
93
94(property_identifier) @property
95(shorthand_property_identifier) @property
96(shorthand_property_identifier_pattern) @property
97(private_property_identifier) @property
98
99; Function and method calls
100
101(call_expression
102 function: (identifier) @function)
103
104(call_expression
105 function: (member_expression
106 property: [(property_identifier) (private_property_identifier)] @function.method))
107
108; Function and method definitions
109
110(function_expression
111 name: (identifier) @function)
112(function_declaration
113 name: (identifier) @function)
114(method_definition
115 name: [(property_identifier) (private_property_identifier)] @function.method)
116(method_definition
117 name: (property_identifier) @constructor
118 (#eq? @constructor "constructor"))
119
120(pair
121 key: [(property_identifier) (private_property_identifier)] @function.method
122 value: [(function_expression) (arrow_function)])
123
124(assignment_expression
125 left: (member_expression
126 property: [(property_identifier) (private_property_identifier)] @function.method)
127 right: [(function_expression) (arrow_function)])
128
129(variable_declarator
130 name: (identifier) @function
131 value: [(function_expression) (arrow_function)])
132
133(assignment_expression
134 left: (identifier) @function
135 right: [(function_expression) (arrow_function)])
136
137(arrow_function) @function
138
139; Parameters
140
141(required_parameter
142 (identifier) @variable.parameter)
143
144(required_parameter
145 (_
146 ([
147 (identifier)
148 (shorthand_property_identifier_pattern)
149 ]) @variable.parameter))
150
151(optional_parameter
152 (identifier) @variable.parameter)
153
154(optional_parameter
155 (_
156 ([
157 (identifier)
158 (shorthand_property_identifier_pattern)
159 ]) @variable.parameter))
160
161(catch_clause
162 parameter: (identifier) @variable.parameter)
163
164(index_signature
165 name: (identifier) @variable.parameter)
166
167(arrow_function
168 parameter: (identifier) @variable.parameter)
169
170(type_predicate
171 name: (identifier) @variable.parameter)
172
173; Literals
174
175(this) @variable.special
176(super) @variable.special
177
178[
179 (null)
180 (undefined)
181] @constant.builtin
182
183[
184 (true)
185 (false)
186] @boolean
187
188(literal_type
189 [
190 (null)
191 (undefined)
192 (true)
193 (false)
194 ] @type.builtin
195)
196
197(comment) @comment
198
199(hash_bang_line) @comment
200
201[
202 (string)
203 (template_string)
204 (template_literal_type)
205] @string
206
207(escape_sequence) @string.escape
208
209(regex) @string.regex
210(regex_flags) @keyword.operator.regex
211(number) @number
212
213; Tokens
214
215[
216 ";"
217 "?."
218 "."
219 ","
220 ":"
221 "?"
222] @punctuation.delimiter
223
224[
225 "..."
226 "-"
227 "--"
228 "-="
229 "+"
230 "++"
231 "+="
232 "*"
233 "*="
234 "**"
235 "**="
236 "/"
237 "/="
238 "%"
239 "%="
240 "<"
241 "<="
242 "<<"
243 "<<="
244 "="
245 "=="
246 "==="
247 "!"
248 "!="
249 "!=="
250 "=>"
251 ">"
252 ">="
253 ">>"
254 ">>="
255 ">>>"
256 ">>>="
257 "~"
258 "^"
259 "&"
260 "|"
261 "^="
262 "&="
263 "|="
264 "&&"
265 "||"
266 "??"
267 "&&="
268 "||="
269 "??="
270 "..."
271] @operator
272
273(regex "/" @string.regex)
274
275(ternary_expression
276 [
277 "?"
278 ":"
279 ] @operator
280)
281
282[
283 "("
284 ")"
285 "["
286 "]"
287 "{"
288 "}"
289] @punctuation.bracket
290
291(template_substitution
292 "${" @punctuation.special
293 "}" @punctuation.special) @embedded
294
295(template_type
296 "${" @punctuation.special
297 "}" @punctuation.special) @embedded
298
299(type_arguments
300 "<" @punctuation.bracket
301 ">" @punctuation.bracket)
302
303(type_parameters
304 "<" @punctuation.bracket
305 ">" @punctuation.bracket)
306
307(decorator "@" @punctuation.special)
308
309(union_type
310 ("|") @punctuation.special)
311
312(intersection_type
313 ("&") @punctuation.special)
314
315(type_annotation
316 (":") @punctuation.special)
317
318(index_signature
319 (":") @punctuation.special)
320
321(type_predicate_annotation
322 (":") @punctuation.special)
323
324(public_field_definition
325 ("?") @punctuation.special)
326
327(property_signature
328 ("?") @punctuation.special)
329
330(method_signature
331 ("?") @punctuation.special)
332
333(optional_parameter
334 ([
335 "?"
336 ":"
337 ]) @punctuation.special)
338
339; Keywords
340
341[
342 "abstract"
343 "as"
344 "async"
345 "await"
346 "class"
347 "const"
348 "debugger"
349 "declare"
350 "default"
351 "delete"
352 "enum"
353 "export"
354 "extends"
355 "from"
356 "function"
357 "get"
358 "implements"
359 "import"
360 "in"
361 "infer"
362 "instanceof"
363 "interface"
364 "is"
365 "keyof"
366 "let"
367 "module"
368 "namespace"
369 "new"
370 "of"
371 "override"
372 "private"
373 "protected"
374 "public"
375 "readonly"
376 "satisfies"
377 "set"
378 "static"
379 "target"
380 "type"
381 "typeof"
382 "using"
383 "var"
384 "void"
385 "with"
386] @keyword
387
388[
389 "break"
390 "case"
391 "catch"
392 "continue"
393 "do"
394 "else"
395 "finally"
396 "for"
397 "if"
398 "return"
399 "switch"
400 "throw"
401 "try"
402 "while"
403 "yield"
404] @keyword.control
405
406(switch_default "default" @keyword.control)