/ queries / typescript / imports.scm
imports.scm
 1  ;; ═══════════════════════════════════════════════════════════════════════════
 2  ;; TypeScript Import Queries
 3  ;; ═══════════════════════════════════════════════════════════════════════════
 4  
 5  ;; ───────────────────────────────────────────────────────────────────────────
 6  ;; import "module" (side-effect import)
 7  ;; ───────────────────────────────────────────────────────────────────────────
 8  ;; (import_statement
 9  ;;   source: (string
10  ;;     (string_fragment) @import_path)
11  ;;   !import_clause) @import_stmt
12  
13  ;; ───────────────────────────────────────────────────────────────────────────
14  ;; import x from "module" (default import)
15  ;; ───────────────────────────────────────────────────────────────────────────
16  (import_statement
17    (import_clause
18      (identifier) @alias_name)
19    source: (string
20      (string_fragment) @import_path)) @import_stmt
21  
22  ;; ───────────────────────────────────────────────────────────────────────────
23  ;; import * as x from "module" (namespace import)
24  ;; ───────────────────────────────────────────────────────────────────────────
25  (import_statement
26    (import_clause
27      (namespace_import
28        (identifier) @alias_name))
29    source: (string
30      (string_fragment) @import_path)) @import_stmt
31  
32  ;; ───────────────────────────────────────────────────────────────────────────
33  ;; import { x, y as z } from "module" (named imports)
34  ;; ───────────────────────────────────────────────────────────────────────────
35  (import_statement
36    (import_clause
37      (named_imports
38        (import_specifier
39          name: (identifier) @original_name
40          alias: (identifier)? @alias_name)))
41    source: (string
42      (string_fragment) @import_path)) @import_stmt
43  
44  
45  ;; ───────────────────────────────────────────────────────────────────────────
46  ;; const x = require("module")
47  ;; ───────────────────────────────────────────────────────────────────────────
48  (variable_declarator
49    name: (identifier) @alias_name
50    value: (call_expression
51      function: (identifier) @function_name
52      arguments: (arguments
53        (string
54          (string_fragment) @import_path)))
55    (#eq? @function_name "require")) @import_stmt
56  
57  ;; ───────────────────────────────────────────────────────────────────────────
58  ;; const { x } = require("module")
59  ;; ───────────────────────────────────────────────────────────────────────────
60  (variable_declarator
61    name: (object_pattern
62      (shorthand_property_identifier_pattern) @alias_name)
63    value: (call_expression
64      function: (identifier) @function_name
65      arguments: (arguments
66        (string
67          (string_fragment) @import_path)))
68    (#eq? @function_name "require")) @import_stmt
69  
70  (variable_declarator
71    name: (object_pattern
72      (pair_pattern
73        key: (property_identifier) @original_name
74        value: (identifier) @alias_name))
75    value: (call_expression
76      function: (identifier) @function_name
77      arguments: (arguments
78        (string
79          (string_fragment) @import_path)))
80    (#eq? @function_name "require")) @import_stmt