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
//! Dependency graph for tracking import/export relationships.
//!
//! This module builds a directed graph of module dependencies based on
//! import statements, component registrations, and provide/inject relationships.
//!
//! ## Performance Optimizations
//!
//! - Uses `FxHashMap` for O(1) node lookup
//! - Uses `SmallVec` for edges (stack-allocated for small counts)
//! - Iterative algorithms avoid stack overflow on deep graphs
//! - Early termination in path finding algorithms

use super::registry::FileId;
use vize_carton::{CompactString, FxHashMap, FxHashSet, SmallVec};

/// Edge type in the dependency graph.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum DependencyEdge {
    /// Static ES module import: `import Foo from './Foo.vue'`
    Import = 0,
    /// Dynamic import: `const Foo = () => import('./Foo.vue')`
    DynamicImport = 1,
    /// Component used in template: `<Foo />`
    ComponentUsage = 2,
    /// Provide/Inject relationship (provider -> consumer)
    ProvideInject = 3,
    /// Event emission (child -> parent event handler)
    EventEmit = 4,
    /// Slot content (parent -> child slot)
    SlotContent = 5,
    /// Re-export: `export { Foo } from './Foo.vue'`
    ReExport = 6,
    /// Type-only import: `import type { Foo } from './types'`
    TypeImport = 7,
}

impl DependencyEdge {
    /// Get display name for this edge type.
    #[inline]
    pub const fn display_name(&self) -> &'static str {
        match self {
            Self::Import => "import",
            Self::DynamicImport => "dynamic-import",
            Self::ComponentUsage => "component",
            Self::ProvideInject => "provide-inject",
            Self::EventEmit => "emit",
            Self::SlotContent => "slot",
            Self::ReExport => "re-export",
            Self::TypeImport => "type-import",
        }
    }
}

/// A node in the dependency graph representing a module.
#[derive(Debug, Clone)]
pub struct ModuleNode {
    /// File ID reference.
    pub file_id: FileId,
    /// Module path (relative to project root).
    pub path: CompactString,
    /// Outgoing edges (this module depends on these).
    pub imports: SmallVec<[(FileId, DependencyEdge); 8]>,
    /// Incoming edges (these modules depend on this).
    pub importers: SmallVec<[(FileId, DependencyEdge); 8]>,
    /// Exported names from this module.
    pub exports: FxHashSet<CompactString>,
    /// Component name (for Vue SFCs).
    pub component_name: Option<CompactString>,
    /// Whether this module is an entry point (App.vue, main.ts).
    pub is_entry: bool,
}

impl ModuleNode {
    /// Create a new module node.
    pub fn new(file_id: FileId, path: impl Into<CompactString>) -> Self {
        Self {
            file_id,
            path: path.into(),
            imports: SmallVec::new(),
            importers: SmallVec::new(),
            exports: FxHashSet::default(),
            component_name: None,
            is_entry: false,
        }
    }
}

/// Dependency graph for a Vue project.
#[derive(Debug, Default)]
pub struct DependencyGraph {
    /// Map from file ID to module node.
    nodes: FxHashMap<FileId, ModuleNode>,
    /// Map from component name to file ID (for resolving template usage).
    component_index: FxHashMap<CompactString, FileId>,
    /// Entry points (App.vue, main.ts, etc.).
    entries: SmallVec<[FileId; 4]>,
    /// Detected circular dependencies.
    circular_deps: Vec<Vec<FileId>>,
}

impl DependencyGraph {
    /// Create a new empty dependency graph.
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a module node to the graph.
    pub fn add_node(&mut self, node: ModuleNode) {
        let file_id = node.file_id;

        // Index component name if present
        if let Some(ref name) = node.component_name {
            self.component_index.insert(name.clone(), file_id);
        }

        // Track entry points
        if node.is_entry {
            self.entries.push(file_id);
        }

        self.nodes.insert(file_id, node);
    }

    /// Add a dependency edge between two modules.
    pub fn add_edge(&mut self, from: FileId, to: FileId, edge_type: DependencyEdge) {
        // Add to importer's imports
        if let Some(from_node) = self.nodes.get_mut(&from) {
            if !from_node.imports.iter().any(|(id, _)| *id == to) {
                from_node.imports.push((to, edge_type));
            }
        }

        // Add to importee's importers
        if let Some(to_node) = self.nodes.get_mut(&to) {
            if !to_node.importers.iter().any(|(id, _)| *id == from) {
                to_node.importers.push((from, edge_type));
            }
        }
    }

    /// Get a node by file ID.
    #[inline]
    pub fn get_node(&self, id: FileId) -> Option<&ModuleNode> {
        self.nodes.get(&id)
    }

    /// Get a mutable node by file ID.
    #[inline]
    pub fn get_node_mut(&mut self, id: FileId) -> Option<&mut ModuleNode> {
        self.nodes.get_mut(&id)
    }

    /// Find a file by component name.
    #[inline]
    pub fn find_by_component(&self, name: &str) -> Option<FileId> {
        self.component_index.get(name).copied()
    }

    /// Get all direct dependencies of a module.
    pub fn dependencies(&self, id: FileId) -> impl Iterator<Item = (FileId, DependencyEdge)> + '_ {
        self.nodes
            .get(&id)
            .into_iter()
            .flat_map(|n| n.imports.iter().copied())
    }

    /// Get all modules that depend on this module.
    pub fn dependents(&self, id: FileId) -> impl Iterator<Item = (FileId, DependencyEdge)> + '_ {
        self.nodes
            .get(&id)
            .into_iter()
            .flat_map(|n| n.importers.iter().copied())
    }

    /// Get transitive dependencies (all modules this depends on, recursively).
    pub fn transitive_dependencies(&self, id: FileId) -> FxHashSet<FileId> {
        let mut visited = FxHashSet::default();
        let mut stack = vec![id];

        while let Some(current) = stack.pop() {
            if visited.contains(&current) {
                continue;
            }
            visited.insert(current);

            if let Some(node) = self.nodes.get(&current) {
                for (dep_id, _) in &node.imports {
                    if !visited.contains(dep_id) {
                        stack.push(*dep_id);
                    }
                }
            }
        }

        visited.remove(&id);
        visited
    }

    /// Get transitive dependents (all modules that depend on this, recursively).
    pub fn transitive_dependents(&self, id: FileId) -> FxHashSet<FileId> {
        let mut visited = FxHashSet::default();
        let mut stack = vec![id];

        while let Some(current) = stack.pop() {
            if visited.contains(&current) {
                continue;
            }
            visited.insert(current);

            if let Some(node) = self.nodes.get(&current) {
                for (dep_id, _) in &node.importers {
                    if !visited.contains(dep_id) {
                        stack.push(*dep_id);
                    }
                }
            }
        }

        visited.remove(&id);
        visited
    }

    /// Detect circular dependencies using DFS.
    pub fn detect_circular_dependencies(&mut self) {
        self.circular_deps.clear();

        let mut visited = FxHashSet::default();
        let mut rec_stack = FxHashSet::default();
        let mut path = Vec::new();
        let mut cycles = Vec::new();

        // Collect all node IDs first to avoid borrow issues
        let node_ids: Vec<_> = self.nodes.keys().copied().collect();

        for start_id in node_ids {
            if !visited.contains(&start_id) {
                Self::dfs_cycle_static(
                    &self.nodes,
                    start_id,
                    &mut visited,
                    &mut rec_stack,
                    &mut path,
                    &mut cycles,
                );
            }
        }

        self.circular_deps = cycles;
    }

    fn dfs_cycle_static(
        nodes: &FxHashMap<FileId, ModuleNode>,
        id: FileId,
        visited: &mut FxHashSet<FileId>,
        rec_stack: &mut FxHashSet<FileId>,
        path: &mut Vec<FileId>,
        cycles: &mut Vec<Vec<FileId>>,
    ) {
        visited.insert(id);
        rec_stack.insert(id);
        path.push(id);

        if let Some(node) = nodes.get(&id) {
            for (dep_id, _) in &node.imports {
                if !visited.contains(dep_id) {
                    Self::dfs_cycle_static(nodes, *dep_id, visited, rec_stack, path, cycles);
                } else if rec_stack.contains(dep_id) {
                    // Found a cycle - extract the cycle from path
                    if let Some(start) = path.iter().position(|p| p == dep_id) {
                        let cycle: Vec<_> = path[start..].to_vec();
                        cycles.push(cycle);
                    }
                }
            }
        }

        path.pop();
        rec_stack.remove(&id);
    }

    /// Get detected circular dependencies.
    #[inline]
    pub fn circular_dependencies(&self) -> &[Vec<FileId>] {
        &self.circular_deps
    }

    /// Check if there's a path from one module to another.
    pub fn has_path(&self, from: FileId, to: FileId) -> bool {
        if from == to {
            return true;
        }
        self.transitive_dependencies(from).contains(&to)
    }

    /// Get the shortest path between two modules.
    pub fn shortest_path(&self, from: FileId, to: FileId) -> Option<Vec<FileId>> {
        if from == to {
            return Some(vec![from]);
        }

        let mut visited = FxHashSet::default();
        let mut queue = std::collections::VecDeque::new();
        let mut parent: FxHashMap<FileId, FileId> = FxHashMap::default();

        visited.insert(from);
        queue.push_back(from);

        while let Some(current) = queue.pop_front() {
            if let Some(node) = self.nodes.get(&current) {
                for (dep_id, _) in &node.imports {
                    if !visited.contains(dep_id) {
                        visited.insert(*dep_id);
                        parent.insert(*dep_id, current);
                        queue.push_back(*dep_id);

                        if *dep_id == to {
                            // Reconstruct path
                            let mut path = vec![to];
                            let mut curr = to;
                            while let Some(&p) = parent.get(&curr) {
                                path.push(p);
                                curr = p;
                            }
                            path.reverse();
                            return Some(path);
                        }
                    }
                }
            }
        }

        None
    }

    /// Get all entry points.
    #[inline]
    pub fn entries(&self) -> &[FileId] {
        &self.entries
    }

    /// Get the number of nodes in the graph.
    #[inline]
    pub fn len(&self) -> usize {
        self.nodes.len()
    }

    /// Check if the graph is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    /// Iterate over all nodes.
    pub fn nodes(&self) -> impl Iterator<Item = &ModuleNode> {
        self.nodes.values()
    }

    /// Get component usage edges (which components use which).
    pub fn component_usage(&self) -> impl Iterator<Item = (FileId, FileId)> + '_ {
        self.nodes.values().flat_map(|node| {
            node.imports
                .iter()
                .filter(|(_, edge)| *edge == DependencyEdge::ComponentUsage)
                .map(move |(dep_id, _)| (node.file_id, *dep_id))
        })
    }

    /// Get all provide/inject relationships.
    pub fn provide_inject_edges(&self) -> impl Iterator<Item = (FileId, FileId)> + '_ {
        self.nodes.values().flat_map(|node| {
            node.imports
                .iter()
                .filter(|(_, edge)| *edge == DependencyEdge::ProvideInject)
                .map(move |(dep_id, _)| (node.file_id, *dep_id))
        })
    }

    /// Topological sort of the graph (for analysis ordering).
    pub fn topological_sort(&self) -> Option<Vec<FileId>> {
        let mut in_degree: FxHashMap<FileId, usize> = FxHashMap::default();
        let mut result = Vec::with_capacity(self.nodes.len());
        let mut queue = std::collections::VecDeque::new();

        // Initialize in-degrees
        for (&id, node) in &self.nodes {
            in_degree.entry(id).or_insert(0);
            for (dep_id, _) in &node.imports {
                *in_degree.entry(*dep_id).or_insert(0) += 1;
            }
        }

        // Find nodes with no incoming edges
        for (&id, &degree) in &in_degree {
            if degree == 0 {
                queue.push_back(id);
            }
        }

        while let Some(id) = queue.pop_front() {
            result.push(id);

            if let Some(node) = self.nodes.get(&id) {
                for (dep_id, _) in &node.imports {
                    if let Some(degree) = in_degree.get_mut(dep_id) {
                        *degree -= 1;
                        if *degree == 0 {
                            queue.push_back(*dep_id);
                        }
                    }
                }
            }
        }

        if result.len() == self.nodes.len() {
            Some(result)
        } else {
            None // Graph has cycles
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{DependencyEdge, DependencyGraph, FileId, ModuleNode};

    #[test]
    fn test_graph_basic() {
        let mut graph = DependencyGraph::new();

        let id1 = FileId::new(0);
        let id2 = FileId::new(1);

        graph.add_node(ModuleNode::new(id1, "Parent.vue"));
        graph.add_node(ModuleNode::new(id2, "Child.vue"));

        graph.add_edge(id1, id2, DependencyEdge::ComponentUsage);

        assert!(graph.has_path(id1, id2));
        assert!(!graph.has_path(id2, id1));
    }

    #[test]
    fn test_cycle_detection() {
        let mut graph = DependencyGraph::new();

        let id1 = FileId::new(0);
        let id2 = FileId::new(1);
        let id3 = FileId::new(2);

        graph.add_node(ModuleNode::new(id1, "A.vue"));
        graph.add_node(ModuleNode::new(id2, "B.vue"));
        graph.add_node(ModuleNode::new(id3, "C.vue"));

        graph.add_edge(id1, id2, DependencyEdge::Import);
        graph.add_edge(id2, id3, DependencyEdge::Import);
        graph.add_edge(id3, id1, DependencyEdge::Import); // Cycle!

        graph.detect_circular_dependencies();

        assert!(!graph.circular_dependencies().is_empty());
    }
}