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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Reactivity tracking and loss detection.
//!
//! Detects issues where reactivity is accidentally broken:
//! - Destructuring reactive objects or refs
//! - Passing reactive values to non-reactive contexts
//! - Reactivity loss through function calls
//! - Ref unwrapping issues
//!
//! ## Performance Optimizations
//!
//! - Uses FxHashSet for O(1) lookups
//! - Early termination when possible
//! - Minimal allocations during analysis

use crate::cross_file::diagnostics::{
    CrossFileDiagnostic, CrossFileDiagnosticKind, DiagnosticSeverity,
};
use crate::cross_file::graph::DependencyGraph;
use crate::cross_file::registry::{FileId, ModuleRegistry};
use crate::reactivity::ReactiveKind;
use vize_carton::{cstr, CompactString, FxHashSet};

/// Kind of reactivity issue.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReactivityIssueKind {
    /// Destructuring a reactive object loses reactivity.
    DestructuredReactive {
        source_name: CompactString,
        destructured_props: Vec<CompactString>,
    },
    /// Destructuring a ref without .value loses reactivity.
    DestructuredRef { ref_name: CompactString },
    /// Reactive value passed to non-reactive context.
    ReactivityLost {
        value_name: CompactString,
        context: CompactString,
    },
    /// Ref used without .value in script.
    MissingValueAccess { ref_name: CompactString },
    /// toRef/toRefs should be used instead of destructuring.
    ShouldUseToRefs { source_name: CompactString },
    /// Reactive value assigned to plain variable.
    ReactiveToPlain {
        source_name: CompactString,
        target_name: CompactString,
    },
    /// storeToRefs should be used for Pinia store.
    ShouldUseStoreToRefs { store_name: CompactString },
    /// Computed without return statement.
    ComputedWithoutReturn { computed_name: CompactString },
    /// Watch source is not reactive.
    NonReactiveWatchSource { source_expression: CompactString },
    /// Prop passed to ref() which creates a copy.
    PropPassedToRef { prop_name: CompactString },
}

/// Information about a reactivity issue.
#[derive(Debug, Clone)]
pub struct ReactivityIssue {
    /// File where the issue occurs.
    pub file_id: FileId,
    /// Kind of issue.
    pub kind: ReactivityIssueKind,
    /// Offset in source.
    pub offset: u32,
    /// The reactive source involved.
    pub source: Option<CompactString>,
}

/// Analyze reactivity issues across components.
pub fn analyze_reactivity(
    registry: &ModuleRegistry,
    _graph: &DependencyGraph,
) -> (Vec<ReactivityIssue>, Vec<CrossFileDiagnostic>) {
    let mut issues = Vec::new();
    let mut diagnostics = Vec::new();

    for entry in registry.vue_components() {
        let analysis = &entry.analysis;
        let file_id = entry.id;

        // Analyze each component for reactivity issues
        let component_issues = analyze_component_reactivity(analysis);

        for issue in component_issues {
            let diag = create_diagnostic(file_id, &issue);
            diagnostics.push(diag);

            issues.push(ReactivityIssue {
                file_id,
                kind: issue.kind,
                offset: issue.offset,
                source: issue.source,
            });
        }
    }

    (issues, diagnostics)
}

/// Internal issue representation during analysis.
struct InternalIssue {
    kind: ReactivityIssueKind,
    offset: u32,
    end_offset: Option<u32>,
    source: Option<CompactString>,
}

/// Analyze a single component for reactivity issues.
/// Uses precise static analysis from the Croquis data - no heuristics.
#[inline]
fn analyze_component_reactivity(analysis: &crate::Croquis) -> Vec<InternalIssue> {
    let mut issues = Vec::new();

    // Track which identifiers come from 'vue' imports (ref, reactive, toRefs, etc.)
    let vue_imports = extract_vue_imports(analysis);

    // Check for destructured inject() calls - these lose reactivity
    // This is precise: we check the actual InjectPattern from the tracker
    for inject in analysis.provide_inject.injects() {
        use crate::provide::InjectPattern;
        match &inject.pattern {
            InjectPattern::ObjectDestructure(props) => {
                issues.push(InternalIssue {
                    kind: ReactivityIssueKind::DestructuredReactive {
                        source_name: inject.local_name.clone(),
                        destructured_props: props.clone(),
                    },
                    offset: inject.start,
                    end_offset: None,
                    source: Some(inject.local_name.clone()),
                });
            }
            InjectPattern::ArrayDestructure(_items) => {
                issues.push(InternalIssue {
                    kind: ReactivityIssueKind::DestructuredReactive {
                        source_name: inject.local_name.clone(),
                        destructured_props: vec![CompactString::new("(array items)")],
                    },
                    offset: inject.start,
                    end_offset: None,
                    source: Some(inject.local_name.clone()),
                });
            }
            InjectPattern::IndirectDestructure {
                inject_var,
                props,
                offset,
            } => {
                // Indirect destructuring also loses reactivity
                // e.g., const state = inject('state'); const { count } = state;
                issues.push(InternalIssue {
                    kind: ReactivityIssueKind::DestructuredReactive {
                        source_name: inject_var.clone(),
                        destructured_props: props.clone(),
                    },
                    offset: *offset,
                    end_offset: None,
                    source: Some(inject_var.clone()),
                });
            }
            InjectPattern::Simple => {
                // No issue - inject is stored properly
            }
        }
    }

    // Check for toRefs usage - this is the correct pattern, no warning needed
    // Check for reactive sources that indicate proper usage
    let torefs_sources: FxHashSet<&str> = analysis
        .reactivity
        .sources()
        .iter()
        .filter(|s| matches!(s.kind, ReactiveKind::ToRef | ReactiveKind::ToRefs))
        .map(|s| s.name.as_str())
        .collect();

    // Build a set of all reactive sources (from vue imports)
    let _reactive_sources: FxHashSet<&str> = analysis
        .reactivity
        .sources()
        .iter()
        .map(|s| s.name.as_str())
        .collect();

    // Track props defined via defineProps
    let props: FxHashSet<&str> = analysis
        .macros
        .props()
        .iter()
        .map(|p| p.name.as_str())
        .collect();

    // Check if props are properly wrapped with toRef/toRefs when destructured
    if let Some(props_destructure) = analysis.macros.props_destructure() {
        for (key, _binding) in props_destructure.bindings.iter() {
            // Check if this destructured prop has a corresponding toRef
            if !torefs_sources.contains(key.as_str()) {
                // This prop is destructured without toRefs - Vue handles this with
                // reactive props destructure transform, so this is actually OK in modern Vue
                // We don't warn here as it's handled by the compiler
            }
        }
    }

    // Check for reactivity loss patterns detected by the parser
    // These are strict, AST-based detections
    for loss in analysis.reactivity.losses() {
        use crate::reactivity::ReactivityLossKind;
        match &loss.kind {
            ReactivityLossKind::ReactiveDestructure {
                source_name,
                destructured_props,
            } => {
                issues.push(InternalIssue {
                    kind: ReactivityIssueKind::DestructuredReactive {
                        source_name: source_name.clone(),
                        destructured_props: destructured_props.clone(),
                    },
                    offset: loss.start,
                    end_offset: Some(loss.end),
                    source: Some(source_name.clone()),
                });
            }
            ReactivityLossKind::RefValueDestructure {
                source_name,
                destructured_props,
            } => {
                issues.push(InternalIssue {
                    kind: ReactivityIssueKind::DestructuredRef {
                        ref_name: source_name.clone(),
                    },
                    offset: loss.start,
                    end_offset: Some(loss.end),
                    source: Some(cstr!(
                        "{}.value (destructured: {})",
                        source_name,
                        destructured_props.join(", ")
                    )),
                });
            }
            ReactivityLossKind::RefValueExtract {
                source_name,
                target_name,
            } => {
                issues.push(InternalIssue {
                    kind: ReactivityIssueKind::ReactiveToPlain {
                        source_name: cstr!("{source_name}.value"),
                        target_name: target_name.clone(),
                    },
                    offset: loss.start,
                    end_offset: Some(loss.end),
                    source: Some(source_name.clone()),
                });
            }
            ReactivityLossKind::ReactiveSpread { source_name } => {
                issues.push(InternalIssue {
                    kind: ReactivityIssueKind::ShouldUseToRefs {
                        source_name: source_name.clone(),
                    },
                    offset: loss.start,
                    end_offset: Some(loss.end),
                    source: Some(source_name.clone()),
                });
            }
            ReactivityLossKind::ReactiveReassign { source_name } => {
                // Get the original reactive type for better diagnostics
                let original_type = analysis
                    .reactivity
                    .lookup(source_name.as_str())
                    .map(|s| match s.kind {
                        ReactiveKind::Ref => "ref",
                        ReactiveKind::ShallowRef => "shallowRef",
                        ReactiveKind::Reactive => "reactive",
                        ReactiveKind::ShallowReactive => "shallowReactive",
                        ReactiveKind::Computed => "computed",
                        ReactiveKind::Readonly => "readonly",
                        ReactiveKind::ShallowReadonly => "shallowReadonly",
                        ReactiveKind::ToRef => "toRef",
                        ReactiveKind::ToRefs => "toRefs",
                    })
                    .unwrap_or("reactive");

                issues.push(InternalIssue {
                    kind: ReactivityIssueKind::ReactivityLost {
                        value_name: source_name.clone(),
                        context: CompactString::new(original_type),
                    },
                    offset: loss.start,
                    end_offset: Some(loss.end),
                    source: Some(source_name.clone()),
                });
            }
        }
    }

    // Report if vue imports are present but not used properly
    if !vue_imports.is_empty() {
        // Check if reactive sources are actually used
        for source in analysis.reactivity.sources() {
            // Verify the reactive function was imported from 'vue'
            let function_name = match source.kind {
                ReactiveKind::Ref => "ref",
                ReactiveKind::ShallowRef => "shallowRef",
                ReactiveKind::Reactive => "reactive",
                ReactiveKind::ShallowReactive => "shallowReactive",
                ReactiveKind::Computed => "computed",
                ReactiveKind::Readonly => "readonly",
                ReactiveKind::ShallowReadonly => "shallowReadonly",
                ReactiveKind::ToRef => "toRef",
                ReactiveKind::ToRefs => "toRefs",
            };

            // Verify it comes from vue
            if !vue_imports.contains(function_name) {
                // The reactive function might be a local implementation or from another library
                // This is a potential issue but not necessarily an error
            }
        }
    }

    // Check for prop passed to ref() which creates a copy
    for source in analysis.reactivity.sources() {
        if source.kind == ReactiveKind::Ref {
            // Check if this ref is initialized with a prop
            if props.contains(source.name.as_str()) {
                issues.push(InternalIssue {
                    kind: ReactivityIssueKind::PropPassedToRef {
                        prop_name: source.name.clone(),
                    },
                    offset: source.declaration_offset,
                    end_offset: None,
                    source: Some(source.name.clone()),
                });
            }
        }
    }

    issues
}

/// Extract identifiers imported from 'vue'.
fn extract_vue_imports(analysis: &crate::Croquis) -> FxHashSet<&str> {
    use crate::scope::ScopeKind;

    let mut vue_imports = FxHashSet::default();

    for scope in analysis.scopes.iter() {
        if scope.kind == ScopeKind::ExternalModule {
            if let crate::scope::ScopeData::ExternalModule(data) = scope.data() {
                // Check if this is a vue import
                if data.source.as_str() == "vue" || data.source.starts_with("vue/") {
                    // Collect all bindings from this import
                    for (name, _) in scope.bindings() {
                        vue_imports.insert(name);
                    }
                }
            }
        }
    }

    vue_imports
}

/// Create a diagnostic from an internal issue.
fn create_diagnostic(file_id: FileId, issue: &InternalIssue) -> CrossFileDiagnostic {
    match &issue.kind {
        ReactivityIssueKind::DestructuredReactive {
            source_name,
            destructured_props,
        } => {
            let mut diag = CrossFileDiagnostic::new(
                CrossFileDiagnosticKind::DestructuringBreaksReactivity {
                    source_name: source_name.clone(),
                    destructured_keys: destructured_props.clone(),
                    suggestion: CompactString::new("toRefs"),
                },
                DiagnosticSeverity::Warning,
                file_id,
                issue.offset,
                cstr!(
                    "Destructuring reactive object '{}' breaks reactivity connection",
                    source_name
                ),
            )
            .with_suggestion(cstr!(
                "Use toRefs({}) or access properties directly as {}.prop",
                source_name,
                source_name
            ));
            if let Some(end) = issue.end_offset {
                diag = diag.with_end_offset(end);
            }
            diag
        }

        ReactivityIssueKind::DestructuredRef { ref_name } => {
            let mut diag = CrossFileDiagnostic::new(
                CrossFileDiagnosticKind::DestructuringBreaksReactivity {
                    source_name: ref_name.clone(),
                    destructured_keys: vec![CompactString::new("value")],
                    suggestion: CompactString::new("computed"),
                },
                DiagnosticSeverity::Warning,
                file_id,
                issue.offset,
                cstr!(
                    "Destructuring ref '{}' creates a non-reactive copy",
                    ref_name
                ),
            )
            .with_suggestion(cstr!(
                "Access {}.value directly or use computed(() => {}.value.prop)",
                ref_name,
                ref_name
            ));
            if let Some(end) = issue.end_offset {
                diag = diag.with_end_offset(end);
            }
            diag
        }

        ReactivityIssueKind::ReactivityLost {
            value_name,
            context,
        } => {
            // Check if this is a reassignment (context is a reactive type name)
            let is_reassignment = matches!(
                context.as_str(),
                "ref"
                    | "shallowRef"
                    | "reactive"
                    | "shallowReactive"
                    | "computed"
                    | "readonly"
                    | "shallowReadonly"
                    | "toRef"
                    | "toRefs"
            );

            if is_reassignment {
                let mut diag = CrossFileDiagnostic::new(
                    CrossFileDiagnosticKind::ReassignmentBreaksReactivity {
                        variable_name: value_name.clone(),
                        original_type: context.clone(),
                    },
                    DiagnosticSeverity::Warning,
                    file_id,
                    issue.offset,
                    cstr!("Reassigning '{value_name}' breaks reactivity tracking",),
                )
                .with_suggestion(
                    "Mutate the object's properties instead, or use ref() for replaceable values",
                );
                if let Some(end) = issue.end_offset {
                    diag = diag.with_end_offset(end);
                }
                diag
            } else {
                CrossFileDiagnostic::new(
                    CrossFileDiagnosticKind::HydrationMismatchRisk {
                        reason: cstr!("'{value_name}' loses reactivity in {context}",),
                    },
                    DiagnosticSeverity::Warning,
                    file_id,
                    issue.offset,
                    cstr!(
                        "Reactive value '{value_name}' loses reactivity when passed to {context}",
                    ),
                )
            }
        }

        ReactivityIssueKind::MissingValueAccess { ref_name } => CrossFileDiagnostic::new(
            CrossFileDiagnosticKind::HydrationMismatchRisk {
                reason: cstr!("Ref '{ref_name}' used without .value"),
            },
            DiagnosticSeverity::Error,
            file_id,
            issue.offset,
            cstr!("Ref '{ref_name}' should be accessed with .value in script context",),
        )
        .with_suggestion(cstr!("Use {ref_name}.value instead of {ref_name}",)),

        ReactivityIssueKind::ShouldUseToRefs { source_name } => {
            let mut diag = CrossFileDiagnostic::new(
                CrossFileDiagnosticKind::SpreadBreaksReactivity {
                    source_name: source_name.clone(),
                    source_type: CompactString::new("reactive"),
                },
                DiagnosticSeverity::Warning,
                file_id,
                issue.offset,
                cstr!("Spreading '{source_name}' creates a non-reactive copy"),
            )
            .with_suggestion(cstr!(
                "Use toRefs({source_name}) to maintain reactivity, or toRaw({source_name}) for intentional copy",
            ));
            if let Some(end) = issue.end_offset {
                diag = diag.with_end_offset(end);
            }
            diag
        }

        ReactivityIssueKind::ReactiveToPlain {
            source_name,
            target_name,
        } => {
            let mut diag = CrossFileDiagnostic::new(
                CrossFileDiagnosticKind::ValueExtractionBreaksReactivity {
                    source_name: source_name.clone(),
                    extracted_value: target_name.clone(),
                },
                DiagnosticSeverity::Warning,
                file_id,
                issue.offset,
                cstr!(
                    "Assigning reactive '{}' to '{}' creates a non-reactive copy",
                    source_name,
                    target_name
                ),
            )
            .with_suggestion("Use computed() or keep the reactive reference");
            if let Some(end) = issue.end_offset {
                diag = diag.with_end_offset(end);
            }
            diag
        }

        ReactivityIssueKind::ShouldUseStoreToRefs { store_name } => CrossFileDiagnostic::new(
            CrossFileDiagnosticKind::DestructuringBreaksReactivity {
                source_name: store_name.clone(),
                destructured_keys: vec![],
                suggestion: CompactString::new("storeToRefs"),
            },
            DiagnosticSeverity::Warning,
            file_id,
            issue.offset,
            cstr!(
                "Destructuring Pinia store '{store_name}' - use storeToRefs() for state/getters"
            ),
        )
        .with_suggestion(cstr!(
            "const {{ state, getter }} = storeToRefs({store_name})"
        )),

        ReactivityIssueKind::ComputedWithoutReturn { computed_name } => CrossFileDiagnostic::new(
            CrossFileDiagnosticKind::HydrationMismatchRisk {
                reason: cstr!("Computed '{computed_name}' may not return value"),
            },
            DiagnosticSeverity::Warning,
            file_id,
            issue.offset,
            cstr!("Computed property '{computed_name}' should return a value"),
        ),

        ReactivityIssueKind::NonReactiveWatchSource { source_expression } => {
            CrossFileDiagnostic::new(
                CrossFileDiagnosticKind::HydrationMismatchRisk {
                    reason: cstr!("Watch source '{source_expression}' is not reactive"),
                },
                DiagnosticSeverity::Warning,
                file_id,
                issue.offset,
                cstr!(
                    "Watch source '{source_expression}' is not reactive, changes won't trigger the callback"
                ),
            )
            .with_suggestion("Use () => value or a ref/reactive object as the watch source")
        }

        ReactivityIssueKind::PropPassedToRef { prop_name } => CrossFileDiagnostic::new(
            CrossFileDiagnosticKind::HydrationMismatchRisk {
                reason: cstr!("Prop '{prop_name}' passed to ref() creates a copy"),
            },
            DiagnosticSeverity::Warning,
            file_id,
            issue.offset,
            cstr!("Passing prop '{prop_name}' to ref() creates a non-reactive copy"),
        )
        .with_suggestion(cstr!(
            "Use toRef(props, '{prop_name}') or computed(() => props.{prop_name})"
        )),
    }
}

#[cfg(test)]
mod tests {
    use super::ReactivityIssueKind;
    use vize_carton::CompactString;

    #[test]
    fn test_reactivity_issue_kind() {
        let kind = ReactivityIssueKind::DestructuredReactive {
            source_name: CompactString::new("state"),
            destructured_props: vec![CompactString::new("count")],
        };

        match kind {
            ReactivityIssueKind::DestructuredReactive { source_name, .. } => {
                assert_eq!(source_name.as_str(), "state");
            }
            _ => panic!("Wrong kind"),
        }
    }
}