tree-sitter-analyzer 1.4.0__py3-none-any.whl → 1.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of tree-sitter-analyzer might be problematic. Click here for more details.

@@ -1,9 +1,523 @@
1
1
  #!/usr/bin/env python3
2
2
  """
3
- JavaScript Tree-sitter queries for code analysis.
3
+ JavaScript Language Queries
4
+
5
+ Comprehensive Tree-sitter queries for JavaScript language constructs.
6
+ Covers functions, classes, variables, imports, exports, and modern JavaScript features.
7
+ Equivalent to Java query coverage for consistent language support.
4
8
  """
5
9
 
6
- # Function declarations and expressions
10
+ # JavaScript-specific query library
11
+ JAVASCRIPT_QUERIES: dict[str, str] = {
12
+ # --- Basic Structure ---
13
+ "function": """
14
+ (function_declaration) @function
15
+ """,
16
+ "function_declaration": """
17
+ (function_declaration
18
+ name: (identifier) @function_name
19
+ parameters: (formal_parameters) @parameters
20
+ body: (statement_block) @body) @function_declaration
21
+ """,
22
+ "function_expression": """
23
+ (function_expression
24
+ name: (identifier)? @function_name
25
+ parameters: (formal_parameters) @parameters
26
+ body: (statement_block) @body) @function_expression
27
+ """,
28
+ "arrow_function": """
29
+ (arrow_function
30
+ parameters: (formal_parameters) @parameters
31
+ body: (_) @body) @arrow_function
32
+ """,
33
+ "method_definition": """
34
+ (method_definition
35
+ name: (property_identifier) @method_name
36
+ parameters: (formal_parameters) @parameters
37
+ body: (statement_block) @body) @method_definition
38
+ """,
39
+ "async_function": """
40
+ (function_declaration
41
+ "async" @async_keyword
42
+ name: (identifier) @function_name
43
+ parameters: (formal_parameters) @parameters
44
+ body: (statement_block) @body) @async_function
45
+ """,
46
+ "generator_function": """
47
+ (generator_function_declaration
48
+ name: (identifier) @function_name
49
+ parameters: (formal_parameters) @parameters
50
+ body: (statement_block) @body) @generator_function
51
+ """,
52
+ # --- Classes ---
53
+ "class": """
54
+ (class_declaration) @class
55
+ """,
56
+ "class_declaration": """
57
+ (class_declaration
58
+ name: (identifier) @class_name
59
+ superclass: (class_heritage)? @superclass
60
+ body: (class_body) @body) @class_declaration
61
+ """,
62
+ "class_expression": """
63
+ (class_expression
64
+ name: (identifier)? @class_name
65
+ superclass: (class_heritage)? @superclass
66
+ body: (class_body) @body) @class_expression
67
+ """,
68
+ "class_method": """
69
+ (class_body
70
+ (method_definition
71
+ name: (property_identifier) @method_name
72
+ parameters: (formal_parameters) @parameters
73
+ body: (statement_block) @body)) @class_method
74
+ """,
75
+ "constructor": """
76
+ (method_definition
77
+ name: (property_identifier) @constructor_name
78
+ (#match? @constructor_name "constructor")
79
+ parameters: (formal_parameters) @parameters
80
+ body: (statement_block) @body) @constructor
81
+ """,
82
+ "getter": """
83
+ (method_definition
84
+ "get" @get_keyword
85
+ name: (property_identifier) @getter_name
86
+ body: (statement_block) @body) @getter
87
+ """,
88
+ "setter": """
89
+ (method_definition
90
+ "set" @set_keyword
91
+ name: (property_identifier) @setter_name
92
+ parameters: (formal_parameters) @parameters
93
+ body: (statement_block) @body) @setter
94
+ """,
95
+ "static_method": """
96
+ (method_definition
97
+ "static" @static_keyword
98
+ name: (property_identifier) @method_name
99
+ parameters: (formal_parameters) @parameters
100
+ body: (statement_block) @body) @static_method
101
+ """,
102
+ "private_method": """
103
+ (method_definition
104
+ name: (private_property_identifier) @method_name
105
+ parameters: (formal_parameters) @parameters
106
+ body: (statement_block) @body) @private_method
107
+ """,
108
+ # --- Variables ---
109
+ "variable": """
110
+ (variable_declaration) @variable
111
+ """,
112
+ "var_declaration": """
113
+ (variable_declaration
114
+ (variable_declarator
115
+ name: (identifier) @variable_name
116
+ value: (_)? @value)) @var_declaration
117
+ """,
118
+ "let_declaration": """
119
+ (lexical_declaration
120
+ "let" @let_keyword
121
+ (variable_declarator
122
+ name: (identifier) @variable_name
123
+ value: (_)? @value)) @let_declaration
124
+ """,
125
+ "const_declaration": """
126
+ (lexical_declaration
127
+ "const" @const_keyword
128
+ (variable_declarator
129
+ name: (identifier) @variable_name
130
+ value: (_) @value)) @const_declaration
131
+ """,
132
+ "destructuring_assignment": """
133
+ (variable_declarator
134
+ name: (array_pattern) @array_destructuring
135
+ value: (_) @value) @destructuring_assignment
136
+ """,
137
+ "object_destructuring": """
138
+ (variable_declarator
139
+ name: (object_pattern) @object_destructuring
140
+ value: (_) @value) @object_destructuring
141
+ """,
142
+ # --- Imports and Exports ---
143
+ "import": """
144
+ (import_statement) @import
145
+ """,
146
+ "import_statement": """
147
+ (import_statement
148
+ source: (string) @source) @import_statement
149
+ """,
150
+ "import_default": """
151
+ (import_statement
152
+ (import_clause
153
+ (import_default_specifier
154
+ (identifier) @default_name))
155
+ source: (string) @source) @import_default
156
+ """,
157
+ "import_named": """
158
+ (import_statement
159
+ (import_clause
160
+ (named_imports
161
+ (import_specifier
162
+ name: (identifier) @import_name
163
+ alias: (identifier)? @alias)))
164
+ source: (string) @source) @import_named
165
+ """,
166
+ "import_namespace": """
167
+ (import_statement
168
+ (import_clause
169
+ (namespace_import
170
+ (identifier) @namespace_name))
171
+ source: (string) @source) @import_namespace
172
+ """,
173
+ "dynamic_import": """
174
+ (call_expression
175
+ function: (identifier) @import_function
176
+ (#match? @import_function "import")
177
+ arguments: (arguments (string) @source)) @dynamic_import
178
+ """,
179
+ "export": """
180
+ (export_statement) @export
181
+ """,
182
+ "export_default": """
183
+ (export_statement
184
+ "default" @default_keyword
185
+ declaration: (_) @declaration) @export_default
186
+ """,
187
+ "export_named": """
188
+ (export_statement
189
+ (export_clause
190
+ (export_specifier
191
+ name: (identifier) @export_name
192
+ alias: (identifier)? @alias))) @export_named
193
+ """,
194
+ "export_all": """
195
+ (export_statement
196
+ "*" @star
197
+ source: (string) @source) @export_all
198
+ """,
199
+ # --- Objects and Properties ---
200
+ "object": """
201
+ (object) @object
202
+ """,
203
+ "object_literal": """
204
+ (object
205
+ (pair
206
+ key: (_) @key
207
+ value: (_) @value)*) @object_literal
208
+ """,
209
+ "property_definition": """
210
+ (property_definition
211
+ property: (_) @property_name
212
+ value: (_)? @value) @property_definition
213
+ """,
214
+ "computed_property": """
215
+ (pair
216
+ key: (computed_property_name) @computed_key
217
+ value: (_) @value) @computed_property
218
+ """,
219
+ "shorthand_property": """
220
+ (shorthand_property_identifier) @shorthand_property
221
+ """,
222
+ "method_property": """
223
+ (pair
224
+ key: (_) @method_name
225
+ value: (function_expression) @method_function) @method_property
226
+ """,
227
+ # --- Control Flow ---
228
+ "if_statement": """
229
+ (if_statement
230
+ condition: (_) @condition
231
+ consequence: (_) @then_branch
232
+ alternative: (_)? @else_branch) @if_statement
233
+ """,
234
+ "for_statement": """
235
+ (for_statement
236
+ initializer: (_)? @init
237
+ condition: (_)? @condition
238
+ increment: (_)? @update
239
+ body: (_) @body) @for_statement
240
+ """,
241
+ "for_in_statement": """
242
+ (for_in_statement
243
+ left: (_) @variable
244
+ right: (_) @object
245
+ body: (_) @body) @for_in_statement
246
+ """,
247
+ "for_of_statement": """
248
+ (for_of_statement
249
+ left: (_) @variable
250
+ right: (_) @iterable
251
+ body: (_) @body) @for_of_statement
252
+ """,
253
+ "while_statement": """
254
+ (while_statement
255
+ condition: (_) @condition
256
+ body: (_) @body) @while_statement
257
+ """,
258
+ "do_statement": """
259
+ (do_statement
260
+ body: (_) @body
261
+ condition: (_) @condition) @do_statement
262
+ """,
263
+ "switch_statement": """
264
+ (switch_statement
265
+ discriminant: (_) @discriminant
266
+ body: (switch_body) @body) @switch_statement
267
+ """,
268
+ "case_clause": """
269
+ (switch_case
270
+ value: (_) @case_value
271
+ body: (_)* @case_body) @case_clause
272
+ """,
273
+ "default_clause": """
274
+ (switch_default
275
+ body: (_)* @default_body) @default_clause
276
+ """,
277
+ # --- Error Handling ---
278
+ "try_statement": """
279
+ (try_statement
280
+ body: (statement_block) @try_body
281
+ handler: (catch_clause)? @catch_handler
282
+ finalizer: (finally_clause)? @finally_block) @try_statement
283
+ """,
284
+ "catch_clause": """
285
+ (catch_clause
286
+ parameter: (identifier)? @error_parameter
287
+ body: (statement_block) @catch_body) @catch_clause
288
+ """,
289
+ "finally_clause": """
290
+ (finally_clause
291
+ body: (statement_block) @finally_body) @finally_clause
292
+ """,
293
+ "throw_statement": """
294
+ (throw_statement
295
+ argument: (_) @thrown_expression) @throw_statement
296
+ """,
297
+ # --- Modern JavaScript Features ---
298
+ "template_literal": """
299
+ (template_literal) @template_literal
300
+ """,
301
+ "template_substitution": """
302
+ (template_substitution
303
+ expression: (_) @substitution_expr) @template_substitution
304
+ """,
305
+ "spread_element": """
306
+ (spread_element
307
+ argument: (_) @spread_argument) @spread_element
308
+ """,
309
+ "rest_parameter": """
310
+ (rest_parameter
311
+ pattern: (identifier) @rest_name) @rest_parameter
312
+ """,
313
+ "await_expression": """
314
+ (await_expression
315
+ argument: (_) @awaited_expression) @await_expression
316
+ """,
317
+ "yield_expression": """
318
+ (yield_expression
319
+ argument: (_)? @yielded_value) @yield_expression
320
+ """,
321
+ # --- JSX (React) ---
322
+ "jsx_element": """
323
+ (jsx_element
324
+ open_tag: (jsx_opening_element) @open_tag
325
+ close_tag: (jsx_closing_element)? @close_tag) @jsx_element
326
+ """,
327
+ "jsx_self_closing": """
328
+ (jsx_self_closing_element
329
+ name: (_) @element_name) @jsx_self_closing
330
+ """,
331
+ "jsx_attribute": """
332
+ (jsx_attribute
333
+ name: (property_identifier) @attribute_name
334
+ value: (_)? @attribute_value) @jsx_attribute
335
+ """,
336
+ "jsx_expression": """
337
+ (jsx_expression
338
+ expression: (_) @jsx_expression_content) @jsx_expression
339
+ """,
340
+ # --- Comments and Documentation ---
341
+ "comment": """
342
+ (comment) @comment
343
+ """,
344
+ "jsdoc_comment": """
345
+ (comment) @jsdoc_comment
346
+ (#match? @jsdoc_comment "^/\\*\\*")
347
+ """,
348
+ "line_comment": """
349
+ (comment) @line_comment
350
+ (#match? @line_comment "^//")
351
+ """,
352
+ "block_comment": """
353
+ (comment) @block_comment
354
+ (#match? @block_comment "^/\\*(?!\\*)")
355
+ """,
356
+ # --- Framework-specific Patterns ---
357
+ "react_component": """
358
+ (function_declaration
359
+ name: (identifier) @component_name
360
+ (#match? @component_name "^[A-Z]")
361
+ body: (statement_block
362
+ (return_statement
363
+ argument: (jsx_element)))) @react_component
364
+ """,
365
+ "react_hook": """
366
+ (call_expression
367
+ function: (identifier) @hook_name
368
+ (#match? @hook_name "^use[A-Z]")) @react_hook
369
+ """,
370
+ "node_require": """
371
+ (call_expression
372
+ function: (identifier) @require_function
373
+ (#match? @require_function "require")
374
+ arguments: (arguments (string) @module_path)) @node_require
375
+ """,
376
+ "module_exports": """
377
+ (assignment_expression
378
+ left: (member_expression
379
+ object: (identifier) @module_object
380
+ (#match? @module_object "module")
381
+ property: (property_identifier) @exports_property
382
+ (#match? @exports_property "exports"))
383
+ right: (_) @exported_value) @module_exports
384
+ """,
385
+ # --- Name-only Extraction ---
386
+ "function_name": """
387
+ (function_declaration
388
+ name: (identifier) @function_name)
389
+ """,
390
+ "class_name": """
391
+ (class_declaration
392
+ name: (identifier) @class_name)
393
+ """,
394
+ "variable_name": """
395
+ (variable_declarator
396
+ name: (identifier) @variable_name)
397
+ """,
398
+ # --- Advanced Patterns ---
399
+ "closure": """
400
+ (function_expression
401
+ body: (statement_block
402
+ (return_statement
403
+ argument: (function_expression)))) @closure
404
+ """,
405
+ "callback_function": """
406
+ (call_expression
407
+ arguments: (arguments
408
+ (function_expression) @callback_function)) @callback_call
409
+ """,
410
+ "promise_chain": """
411
+ (call_expression
412
+ function: (member_expression
413
+ object: (_) @promise_object
414
+ property: (property_identifier) @chain_method
415
+ (#match? @chain_method "^(then|catch|finally)$"))) @promise_chain
416
+ """,
417
+ "event_listener": """
418
+ (call_expression
419
+ function: (member_expression
420
+ property: (property_identifier) @listener_method
421
+ (#match? @listener_method "^(addEventListener|on)$"))
422
+ arguments: (arguments
423
+ (string) @event_type
424
+ (function_expression) @event_handler)) @event_listener
425
+ """,
426
+ "iife": """
427
+ (call_expression
428
+ function: (function_expression) @iife_function) @iife
429
+ """,
430
+ "module_pattern": """
431
+ (variable_declarator
432
+ name: (identifier) @module_name
433
+ value: (call_expression
434
+ function: (function_expression) @module_function)) @module_pattern
435
+ """,
436
+ }
437
+
438
+ # Query descriptions
439
+ JAVASCRIPT_QUERY_DESCRIPTIONS: dict[str, str] = {
440
+ "function": "Search JavaScript function declarations",
441
+ "function_declaration": "Search function declarations with details",
442
+ "function_expression": "Search function expressions",
443
+ "arrow_function": "Search arrow functions",
444
+ "method_definition": "Search method definitions",
445
+ "async_function": "Search async function declarations",
446
+ "generator_function": "Search generator functions",
447
+ "class": "Search JavaScript class declarations",
448
+ "class_declaration": "Search class declarations with details",
449
+ "class_expression": "Search class expressions",
450
+ "class_method": "Search class methods",
451
+ "constructor": "Search class constructors",
452
+ "getter": "Search getter methods",
453
+ "setter": "Search setter methods",
454
+ "static_method": "Search static methods",
455
+ "private_method": "Search private methods",
456
+ "variable": "Search variable declarations",
457
+ "var_declaration": "Search var declarations",
458
+ "let_declaration": "Search let declarations",
459
+ "const_declaration": "Search const declarations",
460
+ "destructuring_assignment": "Search destructuring assignments",
461
+ "object_destructuring": "Search object destructuring",
462
+ "import": "Search import statements",
463
+ "import_statement": "Search import statements with details",
464
+ "import_default": "Search default imports",
465
+ "import_named": "Search named imports",
466
+ "import_namespace": "Search namespace imports",
467
+ "dynamic_import": "Search dynamic imports",
468
+ "export": "Search export statements",
469
+ "export_default": "Search default exports",
470
+ "export_named": "Search named exports",
471
+ "export_all": "Search export all statements",
472
+ "object": "Search object literals",
473
+ "object_literal": "Search object literals with properties",
474
+ "property_definition": "Search property definitions",
475
+ "computed_property": "Search computed properties",
476
+ "shorthand_property": "Search shorthand properties",
477
+ "method_property": "Search method properties",
478
+ "if_statement": "Search if statements",
479
+ "for_statement": "Search for loops",
480
+ "for_in_statement": "Search for-in loops",
481
+ "for_of_statement": "Search for-of loops",
482
+ "while_statement": "Search while loops",
483
+ "do_statement": "Search do-while loops",
484
+ "switch_statement": "Search switch statements",
485
+ "case_clause": "Search switch case clauses",
486
+ "default_clause": "Search switch default clauses",
487
+ "try_statement": "Search try-catch statements",
488
+ "catch_clause": "Search catch clauses",
489
+ "finally_clause": "Search finally clauses",
490
+ "throw_statement": "Search throw statements",
491
+ "template_literal": "Search template literals",
492
+ "template_substitution": "Search template substitutions",
493
+ "spread_element": "Search spread elements",
494
+ "rest_parameter": "Search rest parameters",
495
+ "await_expression": "Search await expressions",
496
+ "yield_expression": "Search yield expressions",
497
+ "jsx_element": "Search JSX elements",
498
+ "jsx_self_closing": "Search self-closing JSX elements",
499
+ "jsx_attribute": "Search JSX attributes",
500
+ "jsx_expression": "Search JSX expressions",
501
+ "comment": "Search all comments",
502
+ "jsdoc_comment": "Search JSDoc comments",
503
+ "line_comment": "Search line comments",
504
+ "block_comment": "Search block comments",
505
+ "react_component": "Search React components",
506
+ "react_hook": "Search React hooks",
507
+ "node_require": "Search Node.js require statements",
508
+ "module_exports": "Search Node.js module exports",
509
+ "function_name": "Search function names only",
510
+ "class_name": "Search class names only",
511
+ "variable_name": "Search variable names only",
512
+ "closure": "Search closures",
513
+ "callback_function": "Search callback functions passed as arguments",
514
+ "promise_chain": "Search Promise chain patterns (.then, .catch, .finally)",
515
+ "event_listener": "Search event listener registrations",
516
+ "iife": "Search immediately invoked function expressions",
517
+ "module_pattern": "Search module patterns",
518
+ }
519
+
520
+ # Legacy query definitions for backward compatibility
7
521
  FUNCTIONS = """
8
522
  (function_declaration
9
523
  name: (identifier) @function.name
@@ -25,7 +539,6 @@ FUNCTIONS = """
25
539
  body: (statement_block) @function.body) @method.definition
26
540
  """
27
541
 
28
- # Class declarations
29
542
  CLASSES = """
30
543
  (class_declaration
31
544
  name: (identifier) @class.name
@@ -38,7 +551,6 @@ CLASSES = """
38
551
  body: (class_body) @class.body) @class.expression
39
552
  """
40
553
 
41
- # Variable declarations
42
554
  VARIABLES = """
43
555
  (variable_declaration
44
556
  (variable_declarator
@@ -51,7 +563,6 @@ VARIABLES = """
51
563
  value: (_)? @variable.value)) @variable.lexical
52
564
  """
53
565
 
54
- # Import and export statements
55
566
  IMPORTS = """
56
567
  (import_statement
57
568
  source: (string) @import.source) @import.statement
@@ -85,7 +596,6 @@ EXPORTS = """
85
596
  alias: (identifier)? @export.alias))) @export.named
86
597
  """
87
598
 
88
- # Object and property definitions
89
599
  OBJECTS = """
90
600
  (object
91
601
  (pair
@@ -97,36 +607,77 @@ OBJECTS = """
97
607
  value: (_)? @property.value) @property.definition
98
608
  """
99
609
 
100
- # Comments
101
610
  COMMENTS = """
102
611
  (comment) @comment
103
612
  """
104
613
 
105
- # All queries combined
106
- ALL_QUERIES = {
107
- "functions": {
108
- "query": FUNCTIONS,
109
- "description": "Search all function declarations, expressions, and methods",
110
- },
111
- "classes": {
112
- "query": CLASSES,
113
- "description": "Search all class declarations and expressions",
114
- },
115
- "variables": {
116
- "query": VARIABLES,
117
- "description": "Search all variable declarations (var, let, const)",
118
- },
119
- "imports": {
120
- "query": IMPORTS,
121
- "description": "Search all import statements and clauses",
122
- },
123
- "exports": {"query": EXPORTS, "description": "Search all export statements"},
124
- "objects": {
125
- "query": OBJECTS,
126
- "description": "Search object literals and property definitions",
127
- },
128
- "comments": {"query": COMMENTS, "description": "Search all comments"},
614
+ # Convert to ALL_QUERIES format for dynamic loader compatibility
615
+ ALL_QUERIES = {}
616
+ for query_name, query_string in JAVASCRIPT_QUERIES.items():
617
+ description = JAVASCRIPT_QUERY_DESCRIPTIONS.get(query_name, "No description")
618
+ ALL_QUERIES[query_name] = {"query": query_string, "description": description}
619
+
620
+ # Add legacy queries for backward compatibility
621
+ ALL_QUERIES["functions"] = {
622
+ "query": FUNCTIONS,
623
+ "description": "Search all function declarations, expressions, and methods",
624
+ }
625
+ ALL_QUERIES["classes"] = {
626
+ "query": CLASSES,
627
+ "description": "Search all class declarations and expressions",
628
+ }
629
+ ALL_QUERIES["variables"] = {
630
+ "query": VARIABLES,
631
+ "description": "Search all variable declarations (var, let, const)",
632
+ }
633
+ ALL_QUERIES["imports"] = {
634
+ "query": IMPORTS,
635
+ "description": "Search all import statements and clauses",
129
636
  }
637
+ ALL_QUERIES["exports"] = {
638
+ "query": EXPORTS,
639
+ "description": "Search all export statements",
640
+ }
641
+ ALL_QUERIES["objects"] = {
642
+ "query": OBJECTS,
643
+ "description": "Search object literals and property definitions",
644
+ }
645
+ ALL_QUERIES["comments"] = {"query": COMMENTS, "description": "Search all comments"}
646
+
647
+
648
+ def get_javascript_query(name: str) -> str:
649
+ """
650
+ Get the specified JavaScript query
651
+
652
+ Args:
653
+ name: Query name
654
+
655
+ Returns:
656
+ Query string
657
+
658
+ Raises:
659
+ ValueError: When query is not found
660
+ """
661
+ if name not in JAVASCRIPT_QUERIES:
662
+ available = list(JAVASCRIPT_QUERIES.keys())
663
+ raise ValueError(
664
+ f"JavaScript query '{name}' does not exist. Available: {available}"
665
+ )
666
+
667
+ return JAVASCRIPT_QUERIES[name]
668
+
669
+
670
+ def get_javascript_query_description(name: str) -> str:
671
+ """
672
+ Get the description of the specified JavaScript query
673
+
674
+ Args:
675
+ name: Query name
676
+
677
+ Returns:
678
+ Query description
679
+ """
680
+ return JAVASCRIPT_QUERY_DESCRIPTIONS.get(name, "No description")
130
681
 
131
682
 
132
683
  def get_query(name: str) -> str:
@@ -146,3 +697,13 @@ def get_all_queries() -> dict:
146
697
  def list_queries() -> list:
147
698
  """List all available query names."""
148
699
  return list(ALL_QUERIES.keys())
700
+
701
+
702
+ def get_available_javascript_queries() -> list[str]:
703
+ """
704
+ Get list of available JavaScript queries
705
+
706
+ Returns:
707
+ List of query names
708
+ """
709
+ return list(JAVASCRIPT_QUERIES.keys())