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
//! Cross-file analyzer implementation.

use super::super::analyzers;
use super::super::graph::{DependencyEdge, DependencyGraph, ModuleNode};
use super::super::registry::{FileId, ModuleRegistry};
use super::types::{CrossFileOptions, CrossFileResult, CrossFileStats};
use crate::{Analyzer, AnalyzerOptions, Croquis};
use std::path::Path;

/// Cross-file analyzer for Vue projects.
pub struct CrossFileAnalyzer {
    /// Analysis options.
    options: CrossFileOptions,
    /// Module registry.
    registry: ModuleRegistry,
    /// Dependency graph.
    graph: DependencyGraph,
    /// Single-file analyzer options.
    single_file_options: AnalyzerOptions,
}

impl CrossFileAnalyzer {
    /// Create a new cross-file analyzer.
    pub fn new(options: CrossFileOptions) -> Self {
        Self {
            options,
            registry: ModuleRegistry::new(),
            graph: DependencyGraph::new(),
            single_file_options: AnalyzerOptions::full(),
        }
    }

    /// Create with a project root directory.
    pub fn with_project_root(options: CrossFileOptions, root: impl AsRef<Path>) -> Self {
        Self {
            options,
            registry: ModuleRegistry::with_project_root(root.as_ref()),
            graph: DependencyGraph::new(),
            single_file_options: AnalyzerOptions::full(),
        }
    }

    /// Set single-file analyzer options.
    pub fn set_single_file_options(&mut self, options: AnalyzerOptions) {
        self.single_file_options = options;
    }

    /// Add a file to be analyzed.
    pub fn add_file(&mut self, path: impl AsRef<Path>, source: &str) -> FileId {
        let path = path.as_ref();

        // Analyze the file with single-file analyzer
        let analysis = self.analyze_single_file(source, path);

        // Register in module registry (takes ownership of analysis)
        let (file_id, is_new) = self.registry.register(path, source, analysis);

        if is_new {
            // Add to dependency graph
            let mut node = ModuleNode::new(file_id, path.to_string_lossy().as_ref());

            // Extract component name
            if let Some(entry) = self.registry.get(file_id) {
                node.component_name = entry.component_name.clone();
            }

            // Mark entry points
            let filename = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
            if filename == "App.vue"
                || filename == "main.ts"
                || filename == "main.js"
                || filename == "index.vue"
            {
                node.is_entry = true;
            }

            self.graph.add_node(node);
        }

        // Update dependencies based on imports (get from registry)
        if let Some(entry) = self.registry.get(file_id) {
            // Collect data we need before calling update_dependencies
            let imports_data: Vec<_> = entry
                .analysis
                .scopes
                .iter()
                .filter(|s| s.kind == crate::scope::ScopeKind::ExternalModule)
                .filter_map(|s| {
                    if let crate::scope::ScopeData::ExternalModule(data) = s.data() {
                        Some((data.source.clone(), data.is_type_only))
                    } else {
                        None
                    }
                })
                .collect();

            let used_components: Vec<_> = entry.analysis.used_components.iter().cloned().collect();

            // Now update dependencies
            for (source, is_type_only) in imports_data {
                if let Some(target_id) = self.resolve_import(&source) {
                    // TODO: Distinguish type-only imports when tracking is needed
                    let edge_type = if is_type_only {
                        DependencyEdge::TypeImport
                    } else {
                        DependencyEdge::Import
                    };
                    self.graph.add_edge(file_id, target_id, edge_type);
                }
            }

            for component in used_components {
                if let Some(target_id) = self.graph.find_by_component(component.as_str()) {
                    self.graph
                        .add_edge(file_id, target_id, DependencyEdge::ComponentUsage);
                }
            }
        }

        file_id
    }

    /// Add multiple files.
    pub fn add_files(&mut self, files: &[(&Path, &str)]) {
        for (path, source) in files {
            self.add_file(path, source);
        }
    }

    /// Add a file with pre-computed analysis.
    ///
    /// This is useful when the caller has already performed analysis (e.g., WASM bindings
    /// that parse both script and template content). The analysis should include
    /// `used_components` populated from template analysis for component usage edges.
    pub fn add_file_with_analysis(
        &mut self,
        path: impl AsRef<Path>,
        source: &str,
        analysis: Croquis,
    ) -> FileId {
        let path = path.as_ref();

        // Register in module registry (takes ownership of analysis)
        let (file_id, is_new) = self.registry.register(path, source, analysis);

        if is_new {
            // Add to dependency graph
            let mut node = ModuleNode::new(file_id, path.to_string_lossy().as_ref());

            // Extract component name
            if let Some(entry) = self.registry.get(file_id) {
                node.component_name = entry.component_name.clone();
            }

            // Mark entry points
            let filename = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
            if filename == "App.vue"
                || filename == "main.ts"
                || filename == "main.js"
                || filename == "index.vue"
            {
                node.is_entry = true;
            }

            self.graph.add_node(node);
        }

        // Update dependencies based on imports (get from registry)
        if let Some(entry) = self.registry.get(file_id) {
            // Collect data we need before calling update_dependencies
            let imports_data: Vec<_> = entry
                .analysis
                .scopes
                .iter()
                .filter(|s| s.kind == crate::scope::ScopeKind::ExternalModule)
                .filter_map(|s| {
                    if let crate::scope::ScopeData::ExternalModule(data) = s.data() {
                        Some((data.source.clone(), data.is_type_only))
                    } else {
                        None
                    }
                })
                .collect();

            let used_components: Vec<_> = entry.analysis.used_components.iter().cloned().collect();

            // Now update dependencies
            for (source, is_type_only) in imports_data {
                if let Some(target_id) = self.resolve_import(&source) {
                    let edge_type = if is_type_only {
                        DependencyEdge::TypeImport
                    } else {
                        DependencyEdge::Import
                    };
                    self.graph.add_edge(file_id, target_id, edge_type);
                }
            }

            for component in used_components {
                if let Some(target_id) = self.graph.find_by_component(component.as_str()) {
                    self.graph
                        .add_edge(file_id, target_id, DependencyEdge::ComponentUsage);
                }
            }
        }

        file_id
    }

    /// Rebuild component usage edges.
    ///
    /// This should be called after all files have been added to ensure
    /// that ComponentUsage edges are correctly established. When files
    /// are added one by one, component references might not resolve
    /// if the target component hasn't been added yet.
    pub fn rebuild_component_edges(&mut self) {
        // Collect all used_components from all files
        let component_data: Vec<_> = self
            .registry
            .iter()
            .map(|entry| {
                let components: Vec<_> = entry.analysis.used_components.iter().cloned().collect();
                (entry.id, components)
            })
            .collect();

        // Add ComponentUsage edges for any that were missed
        for (file_id, used_components) in component_data {
            for component in used_components {
                if let Some(target_id) = self.graph.find_by_component(component.as_str()) {
                    // add_edge checks for duplicates internally
                    self.graph
                        .add_edge(file_id, target_id, DependencyEdge::ComponentUsage);
                }
            }
        }
    }

    /// Run cross-file analysis.
    pub fn analyze(&mut self) -> CrossFileResult {
        // Note: std::time::Instant is not available in WASM, so we conditionally
        // compile time measurement only for non-WASM targets
        #[cfg(not(target_arch = "wasm32"))]
        let start_time = std::time::Instant::now();

        let mut result = CrossFileResult::default();

        // Detect circular dependencies first
        if self.options.circular_dependencies {
            self.graph.detect_circular_dependencies();
            result.circular_deps = self.graph.circular_dependencies().to_vec();
        }

        // Run enabled analyzers
        if self.options.fallthrough_attrs {
            let (info, diags) = analyzers::analyze_fallthrough(&self.registry, &self.graph);
            result.fallthrough_info = info;
            result.diagnostics.extend(diags);
        }

        if self.options.component_emits {
            let (flows, diags) = analyzers::analyze_emits(&self.registry, &self.graph);
            result.emit_flows = flows;
            result.diagnostics.extend(diags);
        }

        if self.options.event_bubbling {
            let (bubbles, diags) = analyzers::analyze_event_bubbling(&self.registry, &self.graph);
            result.event_bubbles = bubbles;
            result.diagnostics.extend(diags);
        }

        if self.options.provide_inject {
            let (matches, diags) = analyzers::analyze_provide_inject(&self.registry, &self.graph);
            result.provide_inject_matches = matches;
            result.diagnostics.extend(diags);
        }

        if self.options.unique_ids {
            let (issues, diags) = analyzers::analyze_element_ids(&self.registry);
            result.unique_id_issues = issues;
            result.diagnostics.extend(diags);
        }

        if self.options.server_client_boundary || self.options.error_suspense_boundary {
            let (boundaries, diags) = analyzers::analyze_boundaries(&self.registry, &self.graph);
            result.boundaries = boundaries;
            result.diagnostics.extend(diags);
        }

        if self.options.reactivity_tracking {
            // Single-file reactivity analysis
            let (issues, diags) = analyzers::analyze_reactivity(&self.registry, &self.graph);
            result.reactivity_issues = issues;
            result.diagnostics.extend(diags);

            // Cross-file reactivity analysis
            let (cross_issues, cross_diags) =
                analyzers::analyze_cross_file_reactivity(&self.registry, &self.graph);
            result.cross_file_reactivity_issues = cross_issues;
            result.diagnostics.extend(cross_diags);
        }

        if self.options.setup_context {
            // Setup context violation analysis (CSRP/memory leaks)
            let (issues, diags) = analyzers::analyze_setup_context(&self.registry, &self.graph);
            result.setup_context_issues = issues;
            result.diagnostics.extend(diags);
        }

        // Static validation analyzers
        if self.options.component_resolution {
            let (issues, diags) =
                analyzers::analyze_component_resolution(&self.registry, &self.graph);
            result.component_resolution_issues = issues;
            result.diagnostics.extend(diags);
        }

        if self.options.props_validation {
            let (issues, diags) = analyzers::analyze_props_validation(&self.registry, &self.graph);
            result.props_validation_issues = issues;
            result.diagnostics.extend(diags);
        }

        // Calculate statistics
        let error_count = result.diagnostics.iter().filter(|d| d.is_error()).count();
        let warning_count = result.diagnostics.iter().filter(|d| d.is_warning()).count();

        #[cfg(not(target_arch = "wasm32"))]
        let analysis_time_ms = start_time.elapsed().as_secs_f64() * 1000.0;
        #[cfg(target_arch = "wasm32")]
        let analysis_time_ms = 0.0; // Time measurement not available in WASM

        result.stats = CrossFileStats {
            files_analyzed: self.registry.len(),
            vue_components: self.registry.vue_components().count(),
            dependency_edges: self.count_edges(),
            error_count,
            warning_count,
            info_count: result.diagnostics.len() - error_count - warning_count,
            analysis_time_ms,
        };

        result
    }

    /// Get the module registry.
    #[inline]
    pub fn registry(&self) -> &ModuleRegistry {
        &self.registry
    }

    /// Get the dependency graph.
    #[inline]
    pub fn graph(&self) -> &DependencyGraph {
        &self.graph
    }

    /// Get analysis for a specific file.
    pub fn get_analysis(&self, file_id: FileId) -> Option<&Croquis> {
        self.registry.get(file_id).map(|e| &e.analysis)
    }

    /// Get file path by ID.
    pub fn get_file_path(&self, file_id: FileId) -> Option<&Path> {
        self.registry.get(file_id).map(|e| e.path.as_path())
    }

    /// Clear all data and reset.
    pub fn clear(&mut self) {
        self.registry.clear();
        self.graph = DependencyGraph::new();
    }

    // === Private methods ===

    fn analyze_single_file(&self, source: &str, path: &Path) -> Croquis {
        let mut analyzer = Analyzer::with_options(self.single_file_options);

        // Detect if it's a Vue SFC
        let is_vue = path
            .extension()
            .is_some_and(|e| e.eq_ignore_ascii_case("vue"));

        if is_vue {
            // For Vue SFC, we need the script content extracted.
            // The caller should pass just the script content, or use
            // the WASM bindings which properly parse SFC.
            // For cross-file analysis, we treat Vue SFC source as script setup.
            analyzer.analyze_script_setup(source);
        } else {
            analyzer.analyze_script_plain(source);
        }

        analyzer.finish()
    }

    fn resolve_import(&self, specifier: &str) -> Option<FileId> {
        // Simple resolution - check if we have this file in the registry
        // A full implementation would use import_resolver

        // Handle relative imports
        if specifier.starts_with('.') {
            // Would need current file context to resolve
            return None;
        }

        // Check by filename
        for entry in self.registry.iter() {
            if entry.filename.as_str() == specifier || {
                #[allow(clippy::disallowed_macros)]
                let vue_name = format!("{}.vue", specifier);
                entry.filename.as_str() == vue_name
            } {
                return Some(entry.id);
            }
        }

        None
    }

    fn count_edges(&self) -> usize {
        self.graph.nodes().map(|n| n.imports.len()).sum()
    }
}

impl Default for CrossFileAnalyzer {
    fn default() -> Self {
        Self::new(CrossFileOptions::default())
    }
}