vize_croquis 0.76.0

Croquis - Semantic analysis layer for Vize. Quick sketches of meaning from Vue templates.
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Import resolution for TypeScript type definitions.
//!
//! Provides module resolution for external type imports used in Vue compiler macros
//! like `defineProps<Props>()` where `Props` is imported from another file.
//!
//! ## Features
//!
//! - **Path Resolution**: Resolves relative and absolute import paths
//! - **tsconfig.json Support**: Respects path mappings from tsconfig.json
//! - **Caching**: High-performance caching with DashMap for concurrent access
//! - **Type-Only Imports**: Handles `import type { X }` statements

use std::fs;
use std::path::{Path, PathBuf};

use dashmap::DashMap;
use serde::Deserialize;
use vize_carton::{cstr, profiler::CacheStats, CompactString, FxHashMap, String, ToCompactString};

/// Resolved module information
#[derive(Debug, Clone)]
pub struct ResolvedModule {
    /// Absolute path to the resolved file
    pub path: PathBuf,
    /// Module content (lazily loaded)
    pub content: Option<String>,
    /// Whether this is a type-only module (e.g., .d.ts)
    pub is_type_only: bool,
}

/// Import resolution error
#[derive(Debug, Clone)]
pub enum ImportResolveError {
    /// Module not found
    NotFound(String),
    /// Invalid specifier
    InvalidSpecifier(String),
    /// File read error
    ReadError(String),
    /// tsconfig.json parse error
    ConfigError(String),
}

impl std::fmt::Display for ImportResolveError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotFound(s) => write!(f, "Module not found: {}", s),
            Self::InvalidSpecifier(s) => write!(f, "Invalid specifier: {}", s),
            Self::ReadError(s) => write!(f, "Read error: {}", s),
            Self::ConfigError(s) => write!(f, "Config error: {}", s),
        }
    }
}

impl std::error::Error for ImportResolveError {}

/// tsconfig.json compiler options (partial)
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct TsConfigCompilerOptions {
    /// Base URL for module resolution
    base_url: Option<String>,
    /// Path mappings
    paths: Option<FxHashMap<String, Vec<String>>>,
    /// Root directory (reserved for future use)
    #[allow(dead_code)]
    root_dir: Option<String>,
}

/// tsconfig.json structure (partial)
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct TsConfig {
    compiler_options: Option<TsConfigCompilerOptions>,
    extends: Option<String>,
}

/// Import resolver for TypeScript modules
///
/// Resolves import specifiers to their actual file paths, supporting:
/// - Relative imports (`./types`, `../shared/types`)
/// - Absolute imports (via tsconfig paths)
/// - Node modules (basic support)
#[derive(Debug)]
pub struct ImportResolver {
    /// Project root directory
    project_root: PathBuf,
    /// Base URL from tsconfig
    base_url: Option<PathBuf>,
    /// Path mappings from tsconfig
    path_mappings: FxHashMap<String, Vec<String>>,
    /// Resolved module cache (thread-safe)
    cache: DashMap<String, Result<ResolvedModule, ImportResolveError>>,
    /// TypeScript file extensions to try
    extensions: Vec<&'static str>,
    /// Cache statistics
    cache_stats: CacheStats,
}

impl ImportResolver {
    /// Create a new import resolver
    ///
    /// # Arguments
    /// * `project_root` - The root directory of the project
    pub fn new(project_root: impl Into<PathBuf>) -> Self {
        let project_root = project_root.into();
        let mut resolver = Self {
            project_root: project_root.clone(),
            base_url: None,
            path_mappings: FxHashMap::default(),
            cache: DashMap::new(),
            extensions: vec![".ts", ".tsx", ".d.ts", ".js", ".jsx"],
            cache_stats: CacheStats::new(),
        };

        // Try to load tsconfig.json
        resolver.load_tsconfig(&project_root);

        resolver
    }

    /// Create a resolver with custom configuration
    pub fn with_config(
        project_root: impl Into<PathBuf>,
        base_url: Option<PathBuf>,
        path_mappings: FxHashMap<String, Vec<String>>,
    ) -> Self {
        Self {
            project_root: project_root.into(),
            base_url,
            path_mappings,
            cache: DashMap::new(),
            extensions: vec![".ts", ".tsx", ".d.ts", ".js", ".jsx"],
            cache_stats: CacheStats::new(),
        }
    }

    /// Load tsconfig.json and extract path mappings
    fn load_tsconfig(&mut self, dir: &Path) {
        let tsconfig_path = dir.join("tsconfig.json");
        if !tsconfig_path.exists() {
            return;
        }

        let content = match fs::read_to_string(&tsconfig_path) {
            Ok(c) => c,
            Err(_) => return,
        };

        let config: TsConfig = match serde_json::from_str(&content) {
            Ok(c) => c,
            Err(_) => return,
        };

        if let Some(ref compiler_options) = config.compiler_options {
            // Set base URL
            if let Some(ref base) = compiler_options.base_url {
                self.base_url = Some(dir.join(base));
            }

            // Set path mappings
            if let Some(ref paths) = compiler_options.paths {
                self.path_mappings = paths.clone();
            }
        }

        // Handle extends (basic support)
        if let Some(ref extends) = config.extends {
            let extended_path = dir.join(extends);
            if let Some(parent) = extended_path.parent() {
                self.load_tsconfig(parent);
            }
        }
    }

    /// Resolve an import specifier to a module
    ///
    /// # Arguments
    /// * `specifier` - The import specifier (e.g., `./types`, `@/types`)
    /// * `from_file` - The file containing the import statement
    ///
    /// # Returns
    /// The resolved module or an error
    pub fn resolve(
        &self,
        specifier: &str,
        from_file: &Path,
    ) -> Result<ResolvedModule, ImportResolveError> {
        // Create cache key
        #[allow(clippy::disallowed_macros)]
        let cache_key = format!("{}:{specifier}", from_file.display());

        // Check cache first
        if let Some(cached) = self.cache.get(cache_key.as_str()) {
            self.cache_stats.hit();
            return cached.clone();
        }

        self.cache_stats.miss();

        // Resolve the module
        let result = self.resolve_uncached(specifier, from_file);

        // Cache the result
        self.cache.insert(cache_key.into(), result.clone());
        self.cache_stats.set_entries(self.cache.len() as u64);

        result
    }

    /// Resolve without caching
    fn resolve_uncached(
        &self,
        specifier: &str,
        from_file: &Path,
    ) -> Result<ResolvedModule, ImportResolveError> {
        // Skip node_modules for now (future: support type definitions)
        if specifier.starts_with("node:") || !specifier.contains('/') && !specifier.starts_with('.')
        {
            return Err(ImportResolveError::NotFound({
                #[allow(clippy::disallowed_macros)]
                let s = format!("Node module resolution not supported: {specifier}");
                s.into()
            }));
        }

        // Try relative resolution
        if specifier.starts_with('.') {
            return self.resolve_relative(specifier, from_file);
        }

        // Try path mapping resolution
        if let Some(resolved) = self.resolve_with_paths(specifier)? {
            return Ok(resolved);
        }

        // Try base URL resolution
        if let Some(ref base_url) = self.base_url {
            if let Ok(resolved) = self.resolve_from_base(specifier, base_url) {
                return Ok(resolved);
            }
        }

        Err(ImportResolveError::NotFound(specifier.to_compact_string()))
    }

    /// Resolve a relative import
    fn resolve_relative(
        &self,
        specifier: &str,
        from_file: &Path,
    ) -> Result<ResolvedModule, ImportResolveError> {
        let from_dir = from_file
            .parent()
            .ok_or_else(|| ImportResolveError::InvalidSpecifier(specifier.to_compact_string()))?;

        let target = from_dir.join(specifier);
        self.try_resolve_file(&target)
    }

    /// Resolve using path mappings
    fn resolve_with_paths(
        &self,
        specifier: &str,
    ) -> Result<Option<ResolvedModule>, ImportResolveError> {
        for (pattern, replacements) in &self.path_mappings {
            // Handle wildcard patterns (e.g., "@/*" -> ["src/*"])
            if pattern.ends_with("/*") {
                let prefix = &pattern[..pattern.len() - 2];
                if let Some(suffix) = specifier.strip_prefix(prefix) {
                    for replacement in replacements {
                        let replacement_prefix = &replacement[..replacement.len() - 1];
                        let base = self.base_url.as_ref().unwrap_or(&self.project_root);
                        #[allow(clippy::disallowed_macros)]
                        let target = base.join(format!("{replacement_prefix}{suffix}"));
                        if let Ok(resolved) = self.try_resolve_file(&target) {
                            return Ok(Some(resolved));
                        }
                    }
                }
            }
            // Exact match
            else if specifier == pattern {
                for replacement in replacements {
                    let base = self.base_url.as_ref().unwrap_or(&self.project_root);
                    let target = base.join(replacement);
                    if let Ok(resolved) = self.try_resolve_file(&target) {
                        return Ok(Some(resolved));
                    }
                }
            }
        }
        Ok(None)
    }

    /// Resolve from base URL
    fn resolve_from_base(
        &self,
        specifier: &str,
        base_url: &Path,
    ) -> Result<ResolvedModule, ImportResolveError> {
        let target = base_url.join(specifier);
        self.try_resolve_file(&target)
    }

    /// Try to resolve a file path with various extensions
    fn try_resolve_file(&self, path: &Path) -> Result<ResolvedModule, ImportResolveError> {
        // Try exact path first
        if path.exists() && path.is_file() {
            return self.create_resolved_module(path);
        }

        // Try with extensions
        for ext in &self.extensions {
            let with_ext = path.with_extension(&ext[1..]); // Remove leading dot
            if with_ext.exists() && with_ext.is_file() {
                return self.create_resolved_module(&with_ext);
            }
        }

        // Try as directory with index file
        if path.exists() && path.is_dir() {
            for ext in &self.extensions {
                #[allow(clippy::disallowed_macros)]
                let index = path.join(format!("index{}", ext));
                if index.exists() && index.is_file() {
                    return self.create_resolved_module(&index);
                }
            }
        }

        // Try path.ts if no extension
        if path.extension().is_none() {
            for ext in &self.extensions {
                #[allow(clippy::disallowed_macros)]
                let with_ext = PathBuf::from(format!("{}{}", path.display(), ext));
                if with_ext.exists() && with_ext.is_file() {
                    return self.create_resolved_module(&with_ext);
                }
            }
        }

        Err(ImportResolveError::NotFound(
            path.display().to_compact_string(),
        ))
    }

    /// Create a resolved module from a path
    fn create_resolved_module(&self, path: &Path) -> Result<ResolvedModule, ImportResolveError> {
        let canonical = path
            .canonicalize()
            .map_err(|e| ImportResolveError::ReadError(e.to_compact_string()))?;

        let is_type_only = canonical
            .extension()
            .map(|ext| ext == "d.ts")
            .unwrap_or(false)
            || canonical
                .file_name()
                .and_then(|n| n.to_str())
                .map(|n| n.ends_with(".d.ts"))
                .unwrap_or(false);

        Ok(ResolvedModule {
            path: canonical,
            content: None, // Lazy loaded
            is_type_only,
        })
    }

    /// Get the content of a resolved module
    pub fn get_content(&self, module: &ResolvedModule) -> Result<String, ImportResolveError> {
        fs::read_to_string(&module.path)
            .map(|s| s.into())
            .map_err(|e| ImportResolveError::ReadError(e.to_compact_string()))
    }

    /// Extract type definitions from a module's content
    ///
    /// Extracts interface and type alias definitions that can be used
    /// for type resolution in defineProps/defineEmits.
    pub fn extract_type_definitions(
        &self,
        content: &str,
    ) -> FxHashMap<CompactString, CompactString> {
        let mut definitions = FxHashMap::default();

        // Simple regex-based extraction for common patterns
        // TODO: Use OXC for more accurate parsing

        // Extract interface definitions
        let interface_re = regex::Regex::new(
            r"(?s)export\s+interface\s+(\w+)(?:<[^>]*>)?\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}",
        );
        if let Ok(re) = interface_re {
            for cap in re.captures_iter(content) {
                if let (Some(name), Some(body)) = (cap.get(1), cap.get(2)) {
                    definitions.insert(
                        CompactString::new(name.as_str()),
                        cstr!("{{ {} }}", body.as_str().trim()),
                    );
                }
            }
        }

        // Extract type alias definitions
        let type_re = regex::Regex::new(r"export\s+type\s+(\w+)(?:<[^>]*>)?\s*=\s*([^;]+);");
        if let Ok(re) = type_re {
            for cap in re.captures_iter(content) {
                if let (Some(name), Some(body)) = (cap.get(1), cap.get(2)) {
                    definitions.insert(
                        CompactString::new(name.as_str()),
                        CompactString::new(body.as_str().trim()),
                    );
                }
            }
        }

        definitions
    }

    /// Clear the resolution cache
    pub fn clear_cache(&self) {
        self.cache.clear();
        self.cache_stats.reset();
        self.cache_stats.set_entries(0);
    }

    /// Get cache statistics
    #[inline]
    pub fn cache_stats(&self) -> &CacheStats {
        &self.cache_stats
    }

    /// Get the project root
    #[inline]
    pub fn project_root(&self) -> &Path {
        &self.project_root
    }

    /// Get the base URL
    #[inline]
    pub fn base_url(&self) -> Option<&Path> {
        self.base_url.as_deref()
    }

    /// Get path mappings
    #[inline]
    pub fn path_mappings(&self) -> &FxHashMap<String, Vec<String>> {
        &self.path_mappings
    }
}

impl Default for ImportResolver {
    fn default() -> Self {
        Self::new(std::env::current_dir().unwrap_or_default())
    }
}

#[cfg(test)]
mod tests {
    use super::ImportResolver;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_relative_resolution() {
        let dir = tempdir().unwrap();
        let types_file = dir.path().join("types.ts");
        fs::write(&types_file, "export interface Props { msg: string }").unwrap();

        let component_file = dir.path().join("Component.vue");
        fs::write(&component_file, "").unwrap();

        let resolver = ImportResolver::new(dir.path());
        let result = resolver.resolve("./types", &component_file);

        assert!(result.is_ok());
        let module = result.unwrap();
        assert_eq!(module.path, types_file.canonicalize().unwrap());
    }

    #[test]
    fn test_path_mapping_resolution() {
        let dir = tempdir().unwrap();
        let src_dir = dir.path().join("src");
        fs::create_dir(&src_dir).unwrap();

        let types_file = src_dir.join("types.ts");
        fs::write(&types_file, "export interface Props { msg: string }").unwrap();

        // Create tsconfig with path mapping
        let tsconfig = dir.path().join("tsconfig.json");
        fs::write(
            &tsconfig,
            r#"{
                "compilerOptions": {
                    "baseUrl": ".",
                    "paths": {
                        "@/*": ["src/*"]
                    }
                }
            }"#,
        )
        .unwrap();

        let component_file = dir.path().join("Component.vue");
        fs::write(&component_file, "").unwrap();

        let resolver = ImportResolver::new(dir.path());
        let result = resolver.resolve("@/types", &component_file);

        assert!(result.is_ok());
    }

    #[test]
    fn test_extract_type_definitions() {
        let resolver = ImportResolver::default();
        let content = r#"
            export interface Props {
                msg: string;
                count?: number;
            }

            export type Emits = {
                (e: 'click'): void;
            }
        "#;

        let defs = resolver.extract_type_definitions(content);
        assert!(defs.contains_key("Props"));
        assert!(defs.contains_key("Emits"));
    }

    #[test]
    fn test_caching() {
        let dir = tempdir().unwrap();
        let types_file = dir.path().join("types.ts");
        fs::write(&types_file, "export interface Props { msg: string }").unwrap();

        let component_file = dir.path().join("Component.vue");
        fs::write(&component_file, "").unwrap();

        let resolver = ImportResolver::new(dir.path());

        // First resolution
        let result1 = resolver.resolve("./types", &component_file);
        assert!(result1.is_ok());

        // Second resolution (should hit cache)
        let result2 = resolver.resolve("./types", &component_file);
        assert!(result2.is_ok());

        // Results should be equivalent
        assert_eq!(result1.unwrap().path, result2.unwrap().path);
    }
}