tsz-core 0.1.9

Core TypeScript compiler and type checker library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
272
273
274
275
276
277
278
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Import Tracking and Analysis
//!
//! This module provides data structures and utilities for tracking imports
//! in TypeScript/JavaScript source files, including:
//! - ES6 imports (named, default, namespace)
//! - CommonJS `require()` calls
//! - Dynamic imports
//! - Type-only imports

use crate::binder::SymbolId;
use crate::parser::NodeIndex;
use rustc_hash::{FxHashMap, FxHashSet};
use std::path::PathBuf;

/// Kind of import statement
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ImportKind {
    /// Named import: `import { foo } from 'mod'`
    Named,
    /// Default import: `import foo from 'mod'`
    Default,
    /// Namespace import: `import * as foo from 'mod'`
    Namespace,
    /// Side-effect import: `import 'mod'`
    SideEffect,
    /// Dynamic import: `import('mod')`
    Dynamic,
    /// CommonJS require: `require('mod')`
    Require,
    /// Type-only import: `import type { Foo } from 'mod'`
    TypeOnly,
    /// Type-only named import: `import { type Foo } from 'mod'`
    TypeOnlyNamed,
}

/// Information about a single imported binding
#[derive(Debug, Clone)]
pub struct ImportedBinding {
    /// Local name used in this file
    pub local_name: String,
    /// Exported name from the source module (may differ from `local_name`)
    pub imported_name: String,
    /// The module specifier
    pub module_specifier: String,
    /// Kind of import
    pub kind: ImportKind,
    /// AST node index of the import declaration
    pub declaration_node: NodeIndex,
    /// AST node index of the binding itself (import specifier)
    pub binding_node: NodeIndex,
    /// Symbol ID for this import binding
    pub symbol_id: SymbolId,
    /// Whether this is a type-only import
    pub is_type_only: bool,
    /// Resolved path (if resolution succeeded)
    pub resolved_path: Option<PathBuf>,
}

/// Information about an import declaration
#[derive(Debug, Clone)]
pub struct ImportDeclaration {
    /// Module specifier string
    pub module_specifier: String,
    /// AST node index
    pub node: NodeIndex,
    /// Individual bindings from this import
    pub bindings: Vec<ImportedBinding>,
    /// Whether the entire import is type-only
    pub is_type_only: bool,
    /// Whether this is a side-effect only import
    pub is_side_effect_only: bool,
    /// Source position (start)
    pub start: u32,
    /// Source position (end)
    pub end: u32,
}

/// Tracks all imports in a source file
#[derive(Debug, Default)]
pub struct ImportTracker {
    /// All import declarations in this file
    pub declarations: Vec<ImportDeclaration>,
    /// Map from local name to imported binding
    pub bindings_by_name: FxHashMap<String, ImportedBinding>,
    /// Map from module specifier to import declarations
    pub declarations_by_specifier: FxHashMap<String, Vec<usize>>,
    /// All module specifiers imported in this file
    pub imported_modules: FxHashSet<String>,
    /// Type-only imports by local name
    pub type_only_imports: FxHashSet<String>,
    /// Default imports by module specifier
    pub default_imports: FxHashMap<String, ImportedBinding>,
    /// Namespace imports by module specifier
    pub namespace_imports: FxHashMap<String, ImportedBinding>,
    /// Dynamic imports
    pub dynamic_imports: Vec<DynamicImport>,
    /// CommonJS require calls
    pub require_calls: Vec<RequireCall>,
}

/// Information about a dynamic import expression
#[derive(Debug, Clone)]
pub struct DynamicImport {
    /// The module specifier (if statically known)
    pub module_specifier: Option<String>,
    /// AST node index
    pub node: NodeIndex,
    /// Whether the specifier is dynamic (non-literal)
    pub is_dynamic_specifier: bool,
    /// Source position
    pub start: u32,
    pub end: u32,
}

/// Information about a `require()` call
#[derive(Debug, Clone)]
pub struct RequireCall {
    /// The module specifier (if statically known)
    pub module_specifier: Option<String>,
    /// AST node index
    pub node: NodeIndex,
    /// Whether this is a destructuring require: `const { foo } = require('mod')`
    pub is_destructuring: bool,
    /// Local binding name (if simple assignment)
    pub binding_name: Option<String>,
    /// Source position
    pub start: u32,
    pub end: u32,
}

impl ImportTracker {
    /// Create a new import tracker
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an import declaration
    pub fn add_declaration(&mut self, decl: ImportDeclaration) {
        let idx = self.declarations.len();

        // Track by specifier
        self.declarations_by_specifier
            .entry(decl.module_specifier.clone())
            .or_default()
            .push(idx);

        // Add to imported modules set
        self.imported_modules.insert(decl.module_specifier.clone());

        // Process bindings
        for binding in &decl.bindings {
            self.bindings_by_name
                .insert(binding.local_name.clone(), binding.clone());

            if binding.is_type_only {
                self.type_only_imports.insert(binding.local_name.clone());
            }

            match binding.kind {
                ImportKind::Default => {
                    self.default_imports
                        .insert(binding.module_specifier.clone(), binding.clone());
                }
                ImportKind::Namespace => {
                    self.namespace_imports
                        .insert(binding.module_specifier.clone(), binding.clone());
                }
                _ => {}
            }
        }

        self.declarations.push(decl);
    }

    /// Add a dynamic import
    pub fn add_dynamic_import(&mut self, import: DynamicImport) {
        if let Some(ref spec) = import.module_specifier {
            self.imported_modules.insert(spec.clone());
        }
        self.dynamic_imports.push(import);
    }

    /// Add a require call
    pub fn add_require_call(&mut self, require: RequireCall) {
        if let Some(ref spec) = require.module_specifier {
            self.imported_modules.insert(spec.clone());
        }
        self.require_calls.push(require);
    }

    /// Get binding by local name
    pub fn get_binding(&self, name: &str) -> Option<&ImportedBinding> {
        self.bindings_by_name.get(name)
    }

    /// Check if a name is imported
    pub fn is_imported(&self, name: &str) -> bool {
        self.bindings_by_name.contains_key(name)
    }

    /// Check if a name is a type-only import
    pub fn is_type_only_import(&self, name: &str) -> bool {
        self.type_only_imports.contains(name)
    }

    /// Get all imports from a specific module
    pub fn get_imports_from_module(&self, specifier: &str) -> Vec<&ImportedBinding> {
        self.bindings_by_name
            .values()
            .filter(|b| b.module_specifier == specifier)
            .collect()
    }

    /// Get the default import for a module
    pub fn get_default_import(&self, specifier: &str) -> Option<&ImportedBinding> {
        self.default_imports.get(specifier)
    }

    /// Get the namespace import for a module
    pub fn get_namespace_import(&self, specifier: &str) -> Option<&ImportedBinding> {
        self.namespace_imports.get(specifier)
    }

    /// Get all imported module specifiers
    pub fn get_imported_modules(&self) -> impl Iterator<Item = &String> {
        self.imported_modules.iter()
    }

    /// Check if this file imports from a specific module
    pub fn imports_from(&self, specifier: &str) -> bool {
        self.imported_modules.contains(specifier)
    }

    /// Get statistics about imports
    pub fn stats(&self) -> ImportStats {
        let mut stats = ImportStats::default();

        for binding in self.bindings_by_name.values() {
            match binding.kind {
                ImportKind::Named => stats.named_imports += 1,
                ImportKind::Default => stats.default_imports += 1,
                ImportKind::Namespace => stats.namespace_imports += 1,
                ImportKind::SideEffect => stats.side_effect_imports += 1,
                ImportKind::TypeOnly | ImportKind::TypeOnlyNamed => stats.type_only_imports += 1,
                _ => {}
            }
        }

        stats.dynamic_imports = self.dynamic_imports.len();
        stats.require_calls = self.require_calls.len();
        stats.total_modules = self.imported_modules.len();

        stats
    }

    /// Clear all tracked imports
    pub fn clear(&mut self) {
        self.declarations.clear();
        self.bindings_by_name.clear();
        self.declarations_by_specifier.clear();
        self.imported_modules.clear();
        self.type_only_imports.clear();
        self.default_imports.clear();
        self.namespace_imports.clear();
        self.dynamic_imports.clear();
        self.require_calls.clear();
    }
}

/// Statistics about imports in a file
#[derive(Debug, Clone, Default)]
pub struct ImportStats {
    pub named_imports: usize,
    pub default_imports: usize,
    pub namespace_imports: usize,
    pub side_effect_imports: usize,
    pub type_only_imports: usize,
    pub dynamic_imports: usize,
    pub require_calls: usize,
    pub total_modules: usize,
}

/// Builder for creating `ImportedBinding`
pub struct ImportedBindingBuilder {
    local_name: String,
    imported_name: String,
    module_specifier: String,
    kind: ImportKind,
    declaration_node: NodeIndex,
    binding_node: NodeIndex,
    symbol_id: SymbolId,
    is_type_only: bool,
    resolved_path: Option<PathBuf>,
}

impl ImportedBindingBuilder {
    pub fn new(local_name: impl Into<String>, module_specifier: impl Into<String>) -> Self {
        let local = local_name.into();
        Self {
            imported_name: local.clone(),
            local_name: local,
            module_specifier: module_specifier.into(),
            kind: ImportKind::Named,
            declaration_node: NodeIndex::NONE,
            binding_node: NodeIndex::NONE,
            symbol_id: SymbolId::NONE,
            is_type_only: false,
            resolved_path: None,
        }
    }

    pub fn imported_name(mut self, name: impl Into<String>) -> Self {
        self.imported_name = name.into();
        self
    }

    pub const fn kind(mut self, kind: ImportKind) -> Self {
        self.kind = kind;
        self
    }

    pub const fn declaration_node(mut self, node: NodeIndex) -> Self {
        self.declaration_node = node;
        self
    }

    pub const fn binding_node(mut self, node: NodeIndex) -> Self {
        self.binding_node = node;
        self
    }

    pub const fn symbol_id(mut self, id: SymbolId) -> Self {
        self.symbol_id = id;
        self
    }

    pub const fn type_only(mut self, is_type_only: bool) -> Self {
        self.is_type_only = is_type_only;
        self
    }

    pub fn resolved_path(mut self, path: PathBuf) -> Self {
        self.resolved_path = Some(path);
        self
    }

    pub fn build(self) -> ImportedBinding {
        ImportedBinding {
            local_name: self.local_name,
            imported_name: self.imported_name,
            module_specifier: self.module_specifier,
            kind: self.kind,
            declaration_node: self.declaration_node,
            binding_node: self.binding_node,
            symbol_id: self.symbol_id,
            is_type_only: self.is_type_only,
            resolved_path: self.resolved_path,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_import_tracker_basic() {
        let mut tracker = ImportTracker::new();

        let binding = ImportedBindingBuilder::new("useState", "react")
            .kind(ImportKind::Named)
            .imported_name("useState")
            .build();

        let decl = ImportDeclaration {
            module_specifier: "react".to_string(),
            node: NodeIndex::NONE,
            bindings: vec![binding],
            is_type_only: false,
            is_side_effect_only: false,
            start: 0,
            end: 30,
        };

        tracker.add_declaration(decl);

        assert!(tracker.is_imported("useState"));
        assert!(!tracker.is_imported("useEffect"));
        assert!(tracker.imports_from("react"));
    }

    #[test]
    fn test_import_tracker_type_only() {
        let mut tracker = ImportTracker::new();

        let binding = ImportedBindingBuilder::new("Props", "./types")
            .kind(ImportKind::TypeOnly)
            .type_only(true)
            .build();

        let decl = ImportDeclaration {
            module_specifier: "./types".to_string(),
            node: NodeIndex::NONE,
            bindings: vec![binding],
            is_type_only: true,
            is_side_effect_only: false,
            start: 0,
            end: 35,
        };

        tracker.add_declaration(decl);

        assert!(tracker.is_type_only_import("Props"));
    }

    #[test]
    fn test_import_stats() {
        let mut tracker = ImportTracker::new();

        // Add named import
        tracker.add_declaration(ImportDeclaration {
            module_specifier: "lodash".to_string(),
            node: NodeIndex::NONE,
            bindings: vec![
                ImportedBindingBuilder::new("map", "lodash")
                    .kind(ImportKind::Named)
                    .build(),
            ],
            is_type_only: false,
            is_side_effect_only: false,
            start: 0,
            end: 0,
        });

        // Add default import
        tracker.add_declaration(ImportDeclaration {
            module_specifier: "react".to_string(),
            node: NodeIndex::NONE,
            bindings: vec![
                ImportedBindingBuilder::new("React", "react")
                    .kind(ImportKind::Default)
                    .build(),
            ],
            is_type_only: false,
            is_side_effect_only: false,
            start: 0,
            end: 0,
        });

        let stats = tracker.stats();
        assert_eq!(stats.named_imports, 1);
        assert_eq!(stats.default_imports, 1);
        assert_eq!(stats.total_modules, 2);
    }

    #[test]
    fn test_get_imports_from_module() {
        let mut tracker = ImportTracker::new();

        tracker.add_declaration(ImportDeclaration {
            module_specifier: "lodash".to_string(),
            node: NodeIndex::NONE,
            bindings: vec![
                ImportedBindingBuilder::new("map", "lodash")
                    .kind(ImportKind::Named)
                    .build(),
                ImportedBindingBuilder::new("filter", "lodash")
                    .kind(ImportKind::Named)
                    .build(),
            ],
            is_type_only: false,
            is_side_effect_only: false,
            start: 0,
            end: 0,
        });

        let lodash_imports = tracker.get_imports_from_module("lodash");
        assert_eq!(lodash_imports.len(), 2);
    }
}