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
//! VIR (Vize Intermediate Representation) text format output.
//!
//! Generates a TOML-like human-readable representation of the analysis
//! for debugging and inspection purposes.

use super::{Croquis, TypeExportKind};
use crate::macros::MacroKind;
use std::fmt::Write;
use vize_carton::FxHashMap;
use vize_carton::String;
use vize_relief::BindingType;

impl Croquis {
    /// Convert analysis summary to VIR (Vize Intermediate Representation) text format.
    ///
    /// This generates a TOML-like human-readable representation of the analysis.
    ///
    /// # Important
    ///
    /// **VIR is a display format only, not a portable representation.**
    ///
    /// - VIR output is intended for debugging and human inspection
    /// - The format may change between versions without notice
    /// - Do not parse VIR output or use it as a stable interface
    /// - For programmatic access, use the `Croquis` struct fields directly
    ///
    /// Performance: Pre-allocates buffer, uses write! macro for zero-copy formatting.
    pub fn to_vir(&self) -> String {
        // Pre-allocate with estimated capacity
        let mut output = String::with_capacity(4096);

        // [vir]
        writeln!(output, "[vir]").ok();
        writeln!(output, "script_setup={}", self.bindings.is_script_setup).ok();
        writeln!(output, "scopes={}", self.scopes.len()).ok();
        writeln!(output, "bindings={}", self.bindings.bindings.len()).ok();
        writeln!(output).ok();

        self.write_surface(&mut output);
        self.write_macros(&mut output);
        self.write_reactivity(&mut output);
        self.write_extern(&mut output);
        self.write_types(&mut output);
        self.write_bindings(&mut output);
        self.write_scopes(&mut output);
        self.write_errors(&mut output);

        output
    }

    fn write_surface(&self, output: &mut String) {
        let has_surface = !self.macros.props().is_empty()
            || !self.macros.emits().is_empty()
            || !self.macros.models().is_empty()
            || self
                .macros
                .all_calls()
                .iter()
                .any(|c| matches!(c.kind, MacroKind::DefineExpose | MacroKind::DefineSlots));

        if !has_surface {
            return;
        }

        // [surface.props]
        if !self.macros.props().is_empty() {
            writeln!(output, "[surface.props]").ok();
            for prop in self.macros.props() {
                let req = if prop.required { "!" } else { "?" };
                let def = if prop.default_value.is_some() {
                    "="
                } else {
                    ""
                };
                if let Some(ref ty) = prop.prop_type {
                    writeln!(output, "{}{}:{}{}", prop.name, req, ty, def).ok();
                } else {
                    writeln!(output, "{}{}{}", prop.name, req, def).ok();
                }
            }
            writeln!(output).ok();
        }

        // [surface.emits]
        if !self.macros.emits().is_empty() {
            writeln!(output, "[surface.emits]").ok();
            for emit in self.macros.emits() {
                if let Some(ref ty) = emit.payload_type {
                    writeln!(output, "{}:{}", emit.name, ty).ok();
                } else {
                    writeln!(output, "{}", emit.name).ok();
                }
            }
            writeln!(output).ok();
        }

        // [surface.models]
        if !self.macros.models().is_empty() {
            writeln!(output, "[surface.models]").ok();
            for model in self.macros.models() {
                let name = if model.name.is_empty() {
                    "modelValue"
                } else {
                    model.name.as_str()
                };
                if let Some(ref ty) = model.model_type {
                    writeln!(output, "{}:{}", name, ty).ok();
                } else {
                    writeln!(output, "{}", name).ok();
                }
            }
            writeln!(output).ok();
        }

        // [surface.expose]
        let expose_calls: Vec<_> = self
            .macros
            .all_calls()
            .iter()
            .filter(|c| c.kind == MacroKind::DefineExpose)
            .collect();
        if !expose_calls.is_empty() {
            writeln!(output, "[surface.expose]").ok();
            for call in &expose_calls {
                if let Some(args) = &call.runtime_args {
                    writeln!(output, "{}", args).ok();
                } else {
                    writeln!(output, "@{}:{}", call.start, call.end).ok();
                }
            }
            writeln!(output).ok();
        }

        // [surface.slots]
        let slots_calls: Vec<_> = self
            .macros
            .all_calls()
            .iter()
            .filter(|c| c.kind == MacroKind::DefineSlots)
            .collect();
        if !slots_calls.is_empty() {
            writeln!(output, "[surface.slots]").ok();
            for call in &slots_calls {
                if let Some(type_args) = &call.type_args {
                    writeln!(output, "{}", type_args).ok();
                } else {
                    writeln!(output, "@{}:{}", call.start, call.end).ok();
                }
            }
            writeln!(output).ok();
        }
    }

    fn write_macros(&self, output: &mut String) {
        if self.macros.all_calls().is_empty() {
            return;
        }

        writeln!(output, "[macros]").ok();
        for call in self.macros.all_calls() {
            if let Some(ref ty) = call.type_args {
                writeln!(
                    output,
                    "@{}<{}> @{}:{}",
                    call.name, ty, call.start, call.end
                )
                .ok();
            } else {
                writeln!(output, "@{} @{}:{}", call.name, call.start, call.end).ok();
            }
        }
        writeln!(output).ok();
    }

    fn write_reactivity(&self, output: &mut String) {
        if self.reactivity.count() == 0 {
            return;
        }

        writeln!(output, "[reactivity]").ok();
        for src in self.reactivity.sources() {
            writeln!(output, "{}={}", src.name, src.kind.to_display()).ok();
        }
        writeln!(output).ok();
    }

    fn write_extern(&self, output: &mut String) {
        let extern_scopes: Vec<_> = self
            .scopes
            .iter()
            .filter(|s| s.kind == crate::scope::ScopeKind::ExternalModule)
            .collect();

        if extern_scopes.is_empty() {
            return;
        }

        writeln!(output, "[extern]").ok();
        for scope in &extern_scopes {
            if let crate::scope::ScopeData::ExternalModule(data) = scope.data() {
                let type_only = if data.is_type_only { "^" } else { "" };
                let bd: Vec<_> = scope.bindings().map(|(n, _)| n).collect();
                if bd.is_empty() {
                    writeln!(output, "{}{}", data.source, type_only).ok();
                } else {
                    writeln!(output, "{}{} {{{}}}", data.source, type_only, bd.join(",")).ok();
                }
            }
        }
        writeln!(output).ok();
    }

    fn write_types(&self, output: &mut String) {
        if self.type_exports.is_empty() {
            return;
        }

        writeln!(output, "[types]").ok();
        for te in &self.type_exports {
            let hoist = if te.hoisted { "^" } else { "" };
            let kind = match te.kind {
                TypeExportKind::Type => "t",
                TypeExportKind::Interface => "i",
            };
            writeln!(
                output,
                "{}{}{}@{}:{}",
                te.name, hoist, kind, te.start, te.end
            )
            .ok();
        }
        writeln!(output).ok();
    }

    fn write_bindings(&self, output: &mut String) {
        if self.bindings.bindings.is_empty() {
            return;
        }

        writeln!(output, "[bindings]").ok();

        // Group bindings by type for compact output
        let mut by_type: FxHashMap<BindingType, Vec<&str>> = FxHashMap::default();
        for (name, bt) in &self.bindings.bindings {
            by_type.entry(*bt).or_default().push(name.as_str());
        }

        // Output in a consistent order
        let type_order = [
            BindingType::SetupConst,
            BindingType::SetupRef,
            BindingType::SetupMaybeRef,
            BindingType::SetupReactiveConst,
            BindingType::SetupLet,
            BindingType::Props,
            BindingType::PropsAliased,
            BindingType::Data,
            BindingType::Options,
            BindingType::LiteralConst,
            BindingType::JsGlobalUniversal,
            BindingType::JsGlobalBrowser,
            BindingType::JsGlobalNode,
            BindingType::JsGlobalDeno,
            BindingType::JsGlobalBun,
            BindingType::VueGlobal,
            BindingType::ExternalModule,
        ];

        for bt in type_order {
            if let Some(names) = by_type.get(&bt) {
                writeln!(output, "{}:{}", bt.to_vir(), names.join(",")).ok();
            }
        }
        writeln!(output).ok();
    }

    fn write_scopes(&self, output: &mut String) {
        if self.scopes.is_empty() {
            return;
        }

        // Build a map from scope ID -> prefixed display ID
        // Separate counters for ~, !, # prefixes
        let mut prefix_counters: FxHashMap<&str, u32> = FxHashMap::default();
        let mut id_to_display: FxHashMap<u32, String> = FxHashMap::default();

        // Helper to determine effective prefix by checking parent chain
        // If any ancestor is ClientOnly, child scopes should also be !
        // If any ancestor is server-only, child scopes should also be #
        let get_effective_prefix = |scope: &crate::scope::Scope| -> &'static str {
            // First check the scope's own prefix
            let own_prefix = scope.kind.prefix();
            if own_prefix != "~" {
                return own_prefix;
            }

            // Check parent chain for client-only or server-only context
            let mut visited: vize_carton::SmallVec<[crate::scope::ScopeId; 8]> =
                vize_carton::SmallVec::new();
            let mut queue: vize_carton::SmallVec<[crate::scope::ScopeId; 8]> =
                scope.parents.iter().copied().collect();

            while let Some(parent_id) = queue.pop() {
                if visited.contains(&parent_id) {
                    continue;
                }
                visited.push(parent_id);

                if let Some(parent) = self.scopes.get_scope(parent_id) {
                    let parent_prefix = parent.kind.prefix();
                    if parent_prefix == "!" {
                        return "!"; // Client-only context propagates down
                    }
                    if parent_prefix == "#" {
                        return "#"; // Server-only context propagates down
                    }
                    // Add grandparents to queue
                    for &gp in &parent.parents {
                        if !visited.contains(&gp) {
                            queue.push(gp);
                        }
                    }
                }
            }

            "~" // Default to universal
        };

        for scope in self.scopes.iter() {
            let prefix = get_effective_prefix(scope);
            let counter = prefix_counters.entry(prefix).or_insert(0);
            #[allow(clippy::disallowed_macros)]
            let display_id = format!("{}{}", prefix, *counter);
            id_to_display.insert(scope.id.as_u32(), display_id.into());
            *counter += 1;
        }

        writeln!(output, "[scopes]").ok();
        for scope in self.scopes.iter() {
            let bd_count = scope.bindings().count();

            // Get scope display ID with prefix
            let scope_id_display = id_to_display
                .get(&scope.id.as_u32())
                .map(|s| s.as_str())
                .unwrap_or("?");

            // Build parent references from the parents list using display IDs
            let par = if scope.parents.is_empty() {
                String::default()
            } else {
                let refs: Vec<_> = scope
                    .parents
                    .iter()
                    .filter_map(|p| id_to_display.get(&p.as_u32()))
                    .map(|s| s.as_str())
                    .collect();
                if refs.is_empty() {
                    String::default()
                } else {
                    {
                        #[allow(clippy::disallowed_macros)]
                        let s = format!(" < {}", refs.join(", "));
                        s.into()
                    }
                }
            };

            if bd_count > 0 {
                let bd: Vec<_> = scope.bindings().map(|(n, _)| n).collect();
                writeln!(
                    output,
                    "{} {} @{}:{} [{}]{}",
                    scope_id_display,
                    scope.display_name(),
                    scope.span.start,
                    scope.span.end,
                    bd.join(","),
                    par
                )
                .ok();
            } else {
                writeln!(
                    output,
                    "{} {} @{}:{}{}",
                    scope_id_display,
                    scope.display_name(),
                    scope.span.start,
                    scope.span.end,
                    par
                )
                .ok();
            }
        }
        writeln!(output).ok();
    }

    fn write_errors(&self, output: &mut String) {
        if self.invalid_exports.is_empty() {
            return;
        }

        writeln!(output, "[errors]").ok();
        for ie in &self.invalid_exports {
            writeln!(output, "{}={:?}@{}:{}", ie.name, ie.kind, ie.start, ie.end).ok();
        }
        writeln!(output).ok();
    }
}