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