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
//! Reactivity tracking for Vue templates.
//!
//! Tracks reactive sources (ref, reactive, computed) and their dependencies.
//! Also detects reactivity loss patterns.

use vize_carton::{CompactString, FxHashMap, FxHashSet};

/// Unique identifier for a reactive source
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct ReactiveId(u32);

impl ReactiveId {
    #[inline(always)]
    pub const fn new(id: u32) -> Self {
        Self(id)
    }

    #[inline(always)]
    pub const fn as_u32(self) -> u32 {
        self.0
    }
}

/// Kind of reactive source
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ReactiveKind {
    /// ref()
    Ref = 0,
    /// shallowRef()
    ShallowRef = 1,
    /// reactive()
    Reactive = 2,
    /// shallowReactive()
    ShallowReactive = 3,
    /// computed()
    Computed = 4,
    /// readonly()
    Readonly = 5,
    /// shallowReadonly()
    ShallowReadonly = 6,
    /// toRef()
    ToRef = 7,
    /// toRefs()
    ToRefs = 8,
}

impl ReactiveKind {
    /// Check if this kind requires .value access
    #[inline]
    pub const fn needs_value_access(self) -> bool {
        matches!(
            self,
            Self::Ref | Self::ShallowRef | Self::Computed | Self::ToRef
        )
    }

    /// Get the kind from a function name
    pub fn from_name(name: &str) -> Option<Self> {
        match name {
            "ref" => Some(Self::Ref),
            "shallowRef" => Some(Self::ShallowRef),
            "reactive" => Some(Self::Reactive),
            "shallowReactive" => Some(Self::ShallowReactive),
            "computed" => Some(Self::Computed),
            "readonly" => Some(Self::Readonly),
            "shallowReadonly" => Some(Self::ShallowReadonly),
            "toRef" => Some(Self::ToRef),
            "toRefs" => Some(Self::ToRefs),
            _ => None,
        }
    }

    /// Get display abbreviation for VIR output
    /// - st = state (ref)
    /// - ist = implicit state (reactive - no .value needed)
    /// - drv = derived (computed)
    #[inline]
    pub const fn to_display(self) -> &'static str {
        match self {
            Self::Ref | Self::ShallowRef | Self::ToRef | Self::ToRefs => "st",
            Self::Reactive | Self::ShallowReactive => "ist",
            Self::Computed => "drv",
            Self::Readonly | Self::ShallowReadonly => "ro",
        }
    }
}

/// A reactive source in the code
#[derive(Debug, Clone)]
pub struct ReactiveSource {
    pub id: ReactiveId,
    pub name: CompactString,
    pub kind: ReactiveKind,
    pub declaration_offset: u32,
}

/// Kind of reactivity loss
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReactivityLossKind {
    /// Destructuring reactive object: `const { x } = reactive({...})`
    ReactiveDestructure {
        source_name: CompactString,
        destructured_props: Vec<CompactString>,
    },
    /// Destructuring ref.value: `const { x } = ref({...}).value`
    RefValueDestructure {
        source_name: CompactString,
        destructured_props: Vec<CompactString>,
    },
    /// Extracting ref.value to plain variable: `const x = ref(0); const y = x.value`
    RefValueExtract {
        source_name: CompactString,
        target_name: CompactString,
    },
    /// Spreading reactive object: `{ ...state }`
    ReactiveSpread { source_name: CompactString },
    /// Reassigning reactive variable: `let state = reactive({}); state = {}`
    ReactiveReassign { source_name: CompactString },
}

/// A detected reactivity loss
#[derive(Debug, Clone)]
pub struct ReactivityLoss {
    pub kind: ReactivityLossKind,
    pub start: u32,
    pub end: u32,
}

/// Tracks reactive sources during analysis
#[derive(Debug, Default)]
pub struct ReactivityTracker {
    sources: Vec<ReactiveSource>,
    by_name: FxHashMap<CompactString, ReactiveId>,
    /// Set of reactive variable names for quick lookup
    reactive_names: FxHashSet<CompactString>,
    /// Detected reactivity losses
    losses: Vec<ReactivityLoss>,
    next_id: u32,
}

impl ReactivityTracker {
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a reactive source
    pub fn register(
        &mut self,
        name: CompactString,
        kind: ReactiveKind,
        declaration_offset: u32,
    ) -> ReactiveId {
        let id = ReactiveId::new(self.next_id);
        self.next_id += 1;

        self.reactive_names.insert(name.clone());
        self.by_name.insert(name.clone(), id);
        self.sources.push(ReactiveSource {
            id,
            name,
            kind,
            declaration_offset,
        });

        id
    }

    /// Look up a reactive source by name
    #[inline]
    pub fn lookup(&self, name: &str) -> Option<&ReactiveSource> {
        self.by_name
            .get(name)
            .and_then(|id| self.sources.get(id.as_u32() as usize))
    }

    /// Check if a name is a reactive source
    #[inline]
    pub fn is_reactive(&self, name: &str) -> bool {
        self.reactive_names.contains(name)
    }

    /// Check if a name needs .value access
    #[inline]
    pub fn needs_value_access(&self, name: &str) -> bool {
        self.lookup(name)
            .is_some_and(|s| s.kind.needs_value_access())
    }

    /// Get all reactive sources
    #[inline]
    pub fn sources(&self) -> &[ReactiveSource] {
        &self.sources
    }

    /// Get source count
    #[inline]
    pub fn count(&self) -> usize {
        self.sources.len()
    }

    /// Get reactive names set (for external lookup)
    #[inline]
    pub fn reactive_names(&self) -> &FxHashSet<CompactString> {
        &self.reactive_names
    }

    /// Add a reactivity loss
    #[inline]
    pub fn add_loss(&mut self, loss: ReactivityLoss) {
        self.losses.push(loss);
    }

    /// Record destructuring of a reactive variable
    pub fn record_destructure(
        &mut self,
        source_name: CompactString,
        destructured_props: Vec<CompactString>,
        start: u32,
        end: u32,
    ) {
        if let Some(source) = self.lookup(source_name.as_str()) {
            let kind = if source.kind.needs_value_access() {
                // ref type - destructuring ref.value
                ReactivityLossKind::RefValueDestructure {
                    source_name,
                    destructured_props,
                }
            } else {
                // reactive type
                ReactivityLossKind::ReactiveDestructure {
                    source_name,
                    destructured_props,
                }
            };
            self.losses.push(ReactivityLoss { kind, start, end });
        }
    }

    /// Record spreading of a reactive variable
    pub fn record_spread(&mut self, source_name: CompactString, start: u32, end: u32) {
        if self.is_reactive(source_name.as_str()) {
            self.losses.push(ReactivityLoss {
                kind: ReactivityLossKind::ReactiveSpread { source_name },
                start,
                end,
            });
        }
    }

    /// Record extracting ref.value to a plain variable
    pub fn record_ref_value_extract(
        &mut self,
        source_name: CompactString,
        target_name: CompactString,
        start: u32,
        end: u32,
    ) {
        if let Some(source) = self.lookup(source_name.as_str()) {
            if source.kind.needs_value_access() {
                self.losses.push(ReactivityLoss {
                    kind: ReactivityLossKind::RefValueExtract {
                        source_name,
                        target_name,
                    },
                    start,
                    end,
                });
            }
        }
    }

    /// Record reassignment of a reactive variable
    pub fn record_reassign(&mut self, source_name: CompactString, start: u32, end: u32) {
        if self.is_reactive(source_name.as_str()) {
            self.losses.push(ReactivityLoss {
                kind: ReactivityLossKind::ReactiveReassign { source_name },
                start,
                end,
            });
        }
    }

    /// Get all detected reactivity losses
    #[inline]
    pub fn losses(&self) -> &[ReactivityLoss] {
        &self.losses
    }

    /// Check if there are any reactivity losses
    #[inline]
    pub fn has_losses(&self) -> bool {
        !self.losses.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::{ReactiveKind, ReactivityLossKind, ReactivityTracker};
    use vize_carton::CompactString;

    #[test]
    fn test_reactivity_tracker() {
        let mut tracker = ReactivityTracker::new();

        tracker.register(CompactString::new("count"), ReactiveKind::Ref, 0);
        tracker.register(CompactString::new("state"), ReactiveKind::Reactive, 20);

        assert!(tracker.is_reactive("count"));
        assert!(tracker.needs_value_access("count"));

        assert!(tracker.is_reactive("state"));
        assert!(!tracker.needs_value_access("state"));

        assert!(!tracker.is_reactive("unknown"));
    }

    #[test]
    fn test_reactive_destructure_loss() {
        let mut tracker = ReactivityTracker::new();
        tracker.register(CompactString::new("state"), ReactiveKind::Reactive, 0);

        tracker.record_destructure(
            CompactString::new("state"),
            vec![CompactString::new("count"), CompactString::new("name")],
            10,
            20,
        );

        assert!(tracker.has_losses());
        assert_eq!(tracker.losses().len(), 1);
        match &tracker.losses()[0].kind {
            ReactivityLossKind::ReactiveDestructure {
                source_name,
                destructured_props,
            } => {
                assert_eq!(source_name.as_str(), "state");
                assert_eq!(destructured_props.len(), 2);
            }
            _ => panic!("Expected ReactiveDestructure"),
        }
    }

    #[test]
    fn test_reactive_spread_loss() {
        let mut tracker = ReactivityTracker::new();
        tracker.register(CompactString::new("state"), ReactiveKind::Reactive, 0);

        tracker.record_spread(CompactString::new("state"), 10, 20);

        assert!(tracker.has_losses());
        assert_eq!(tracker.losses().len(), 1);
        match &tracker.losses()[0].kind {
            ReactivityLossKind::ReactiveSpread { source_name } => {
                assert_eq!(source_name.as_str(), "state");
            }
            _ => panic!("Expected ReactiveSpread"),
        }
    }

    #[test]
    fn test_ref_value_extract_loss() {
        let mut tracker = ReactivityTracker::new();
        tracker.register(CompactString::new("count"), ReactiveKind::Ref, 0);

        tracker.record_ref_value_extract(
            CompactString::new("count"),
            CompactString::new("value"),
            10,
            20,
        );

        assert!(tracker.has_losses());
        assert_eq!(tracker.losses().len(), 1);
        match &tracker.losses()[0].kind {
            ReactivityLossKind::RefValueExtract {
                source_name,
                target_name,
            } => {
                assert_eq!(source_name.as_str(), "count");
                assert_eq!(target_name.as_str(), "value");
            }
            _ => panic!("Expected RefValueExtract"),
        }
    }

    #[test]
    fn test_non_reactive_no_loss() {
        let mut tracker = ReactivityTracker::new();
        tracker.register(CompactString::new("state"), ReactiveKind::Reactive, 0);

        // Destructuring non-reactive variable should not record a loss
        tracker.record_destructure(
            CompactString::new("other"),
            vec![CompactString::new("count")],
            10,
            20,
        );

        assert!(!tracker.has_losses());
    }
}