verter_core 0.0.1-beta.1

Vue 3 SFC compiler - transforms Vue Single File Components to render functions with TypeScript support
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
//! IDE code generation for the Vue SFC compiler.
//!
//! Generates valid `.tsx` or `.jsx` output from a parsed SFC for type checking.
//! Unlike the VDOM/Vapor codegen backends (which produce render functions), this
//! module produces a single file with:
//!
//! - **Script block**: preserves types, macros, and imports (TS or JSDoc)
//! - **Template block**: converts Vue template syntax to valid JSX
//!
//! The output mode depends on the SFC's script language:
//! - **TypeScript** (`<script lang="ts">` or `<script setup lang="ts">`): `.tsx`
//!   with TypeScript annotations and generics.
//! - **JavaScript** (`<script>` or `<script lang="js">`): `.jsx` with JSDoc
//!   annotations instead of TypeScript syntax.
//!
//! The IDE output is used by the LSP for hover, completions, go-to-definition,
//! and diagnostics, and by the playground's "Types" tab.
//!
//! ## Architecture
//!
//! This is a **top-level module**, not a template codegen variant. It receives
//! the full `Syntax` AST (script blocks + template AST) and generates two
//! independent output blocks with source maps. It does NOT implement
//! `TemplateCodeGen` or use the shared walker.
//!
//! ```text
//! compile() orchestrator
//!   ├── generate_script()      → JS/TS script block (existing)
//!   ├── generate_template()    → render function (existing)
//!   ├── ide::script::generate_ide_script()    → IDE script block
//!   └── ide::template::generate_ide_template() → IDE template JSX
//! ```

pub mod condition;
pub mod condition_narrowing;
pub mod script;
pub mod script_recover;
pub mod template;

/// Options for IDE script generation.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct IdeScriptOptions<'a> {
    /// Component name extracted from filename.
    pub component_name: &'a str,
    /// Sanitized JS identifier for the component (e.g., `_123Widget` for `123-widget.vue`).
    pub js_component_name: &'a str,
    /// Original filename (e.g., `"App.vue"`) used for self-import.
    pub filename: &'a str,
    /// Scoped style ID (e.g., `"data-v-abc123"`).
    pub scope_id: &'a str,
    /// Whether any `<style scoped>` block exists.
    pub has_scoped_style: bool,
    /// Runtime module name (e.g., `"vue"`).
    pub runtime_module_name: &'a str,
    /// Types module name (e.g., `"$verter/types"` or `"@verter/types"`).
    pub types_module_name: &'a str,
    /// Whether the SFC uses Vapor mode.
    pub is_vapor: bool,
    /// Embed `declare module "@verter/types"` in TSX output.
    /// When `false`, the ambient block is omitted (requires real package).
    pub embed_ambient_types: bool,
    /// When `true`, emit JavaScript + JSDoc instead of TypeScript.
    /// `false` → `.tsx` with type annotations and generics.
    /// `true` → `.jsx` with JSDoc annotations, no TS syntax.
    pub is_jsx: bool,
    /// Experimental: Enable conditional root generic narrowing.
    /// When a root v-if references a prop, the component's type signature
    /// narrows based on the prop value passed by the parent.
    pub conditional_root_narrowing: bool,
    /// Binding names referenced in style `v-bind()` expressions.
    /// Used to emit `void(name)` and prevent false unused diagnostics.
    pub style_v_bind_vars: Vec<String>,
    /// CSS module info: `(module_name, class_names)` pairs.
    /// `module_name` is `"$style"` for `<style module>` or the custom name
    /// from `<style module="classes">`.
    pub css_modules: Vec<CssModuleInfo>,
}

/// CSS module information for IDE codegen.
#[derive(Debug, Clone)]
pub struct CssModuleInfo {
    /// Binding name (e.g., `"$style"` or `"classes"`)
    pub binding_name: String,
    /// CSS class names found in the module block.
    pub class_names: Vec<String>,
}

/// Options for IDE template generation.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct IdeTemplateOptions<'a> {
    /// Self-referencing component name (PascalCase).
    pub self_name: &'a str,
    /// Whether to preserve HTML comments in JSX output.
    pub comments: bool,
    /// When `true`, emit JavaScript (no TS annotations in template).
    pub is_jsx: bool,
    /// Experimental: strict slot children type checking.
    /// Emits `strictRenderSlot` calls to enforce typed slot children.
    pub strict_slots: bool,
}

// ── Generic info ─────────────────────────────────────────────────

/// Prefix for sanitised generic type parameter names.
const GENERIC_SANITISE_PREFIX: &str = "__VERTER__TS__";

/// Parsed and processed generic type parameters for IDE emission.
///
/// Built from the raw `generic="..."` attribute, this struct holds the
/// original source, extracted names, sanitised names (prefixed to avoid
/// collisions), and a full declaration string for the public API types.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct IdeGenericInfo {
    /// Original generic source (e.g., `"T extends object"`).
    pub source: String,
    /// Extracted parameter names (e.g., `["T"]`).
    pub names: Vec<String>,
    /// Sanitised parameter names with `__VERTER__TS__` prefix (e.g., `["__VERTER__TS__T"]`).
    pub sanitised_names: Vec<String>,
    /// Full declaration string with sanitised names, constraints, and defaults.
    /// (e.g., `"__VERTER__TS__T extends object = any"`).
    pub declaration: String,
}

#[allow(dead_code)]
impl IdeGenericInfo {
    /// Build generic info from the raw generic attribute source string.
    ///
    /// Returns `None` if the source is empty or parsing fails.
    pub fn from_source(generic_str: &str) -> Option<Self> {
        let trimmed = generic_str.trim();
        if trimmed.is_empty() {
            return None;
        }

        let alloc = oxc_allocator::Allocator::default();
        let result = crate::utils::oxc::vue::parse_generic(&alloc, trimmed, 0);

        if !result.is_ok() {
            return None;
        }

        let source_bytes = trimmed.as_bytes();

        let names: Vec<String> = result
            .params
            .iter()
            .map(|p| String::from_utf8_lossy(p.name(source_bytes)).into_owned())
            .collect();

        let sanitised_names: Vec<String> = names
            .iter()
            .map(|n| format!("{}{}", GENERIC_SANITISE_PREFIX, n))
            .collect();

        // Build declaration: for each param, emit `{sanitised_name} [extends {sanitised_constraint}] [= {sanitised_default} | = any]`
        let mut declaration_parts: Vec<String> = Vec::with_capacity(result.params.len());
        for (i, param) in result.params.iter().enumerate() {
            let mut part = sanitised_names[i].clone();

            if let Some(constraint_bytes) = param.constraint(source_bytes) {
                let constraint_str = std::str::from_utf8(constraint_bytes).unwrap_or("");
                let sanitised_constraint =
                    sanitise_type_references(constraint_str, &names, &sanitised_names);
                part.push_str(" extends ");
                part.push_str(&sanitised_constraint);
            }

            if let Some(default_bytes) = param.default_type(source_bytes) {
                let default_str = std::str::from_utf8(default_bytes).unwrap_or("");
                let sanitised_default =
                    sanitise_type_references(default_str, &names, &sanitised_names);
                part.push_str(" = ");
                part.push_str(&sanitised_default);
            } else {
                part.push_str(" = any");
            }

            declaration_parts.push(part);
        }

        let declaration = declaration_parts.join(", ");

        Some(IdeGenericInfo {
            source: trimmed.to_string(),
            names,
            sanitised_names,
            declaration,
        })
    }

    /// Returns `<{source}>` or empty string.
    pub fn source_bracket(&self) -> String {
        format!("<{}>", self.source)
    }

    /// Returns `<{names}>` (comma-separated) or empty string.
    pub fn names_bracket(&self) -> String {
        format!("<{}>", self.names.join(", "))
    }

    /// Returns `<{sanitised_names}>` (comma-separated) or empty string.
    pub fn sanitised_names_bracket(&self) -> String {
        format!("<{}>", self.sanitised_names.join(", "))
    }

    /// Returns `<{declaration}>` or empty string.
    pub fn declaration_bracket(&self) -> String {
        format!("<{}>", self.declaration)
    }
}

/// Replace generic name references in a type string with sanitised names.
fn sanitise_type_references(
    type_str: &str,
    names: &[String],
    sanitised_names: &[String],
) -> String {
    let mut result = type_str.to_string();
    for (name, sanitised) in names.iter().zip(sanitised_names.iter()) {
        result = replace_word_boundary(&result, name, sanitised);
    }
    result
}

/// Replace all occurrences of `needle` in `haystack` where the needle is at a
/// word boundary (not preceded/followed by `[a-zA-Z0-9_]`).
fn replace_word_boundary(haystack: &str, needle: &str, replacement: &str) -> String {
    if needle.is_empty() || haystack.is_empty() {
        return haystack.to_string();
    }

    let haystack_bytes = haystack.as_bytes();
    let needle_bytes = needle.as_bytes();
    let needle_len = needle_bytes.len();
    let mut result = String::with_capacity(haystack.len());
    let mut pos = 0;

    while pos + needle_len <= haystack_bytes.len() {
        if &haystack_bytes[pos..pos + needle_len] == needle_bytes {
            // Check word boundary before
            let before_ok = pos == 0 || !is_ident_char(haystack_bytes[pos - 1]);
            // Check word boundary after
            let after_ok = pos + needle_len >= haystack_bytes.len()
                || !is_ident_char(haystack_bytes[pos + needle_len]);

            if before_ok && after_ok {
                result.push_str(replacement);
                pos += needle_len;
                continue;
            }
        }
        result.push(haystack_bytes[pos] as char);
        pos += 1;
    }

    // Append remaining bytes
    while pos < haystack_bytes.len() {
        result.push(haystack_bytes[pos] as char);
        pos += 1;
    }

    result
}

#[inline]
fn is_ident_char(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_'
}

// ── Shared IDE Helpers ──────────────────────────────────────────

use crate::types::NodeProp;

/// Extract the directive name from a prop.
///
/// Handles shorthand (`:`→`"bind"`, `@`→`"on"`, `#`→`"slot"`) and
/// full `v-*` prefix.
pub(crate) fn get_directive_name<'a>(prop: &NodeProp, source: &'a str) -> &'a str {
    let name = &source[prop.start as usize..prop.name_end as usize];

    if name.starts_with(':') || name.starts_with('.') {
        return "bind";
    }
    if name.starts_with('@') {
        return "on";
    }
    if name.starts_with('#') {
        return "slot";
    }

    name.strip_prefix("v-").unwrap_or(name)
}

/// Convert a Vue event name to JSX event prop name (PascalCase segments).
///
/// - `click` → `onClick`
/// - `update:modelValue` → `onUpdate:modelValue`
/// - `custom-event` → `onCustomEvent`
pub(crate) fn event_to_jsx_name(event_name: &str) -> String {
    if let Some(rest) = event_name.strip_prefix("update:") {
        return format!("onUpdate:{}", rest);
    }

    // Prepend "on" and capitalize first letter, preserving the rest as-is.
    // Hyphens are NOT removed — kebab-case events stay kebab-case.
    // Names with hyphens (e.g. "onCustom-event") are not valid JSX identifiers
    // and will be emitted using spread syntax by the caller.
    let mut result = String::with_capacity(event_name.len() + 2);
    result.push_str("on");
    let mut chars = event_name.chars();
    if let Some(first) = chars.next() {
        for upper in first.to_uppercase() {
            result.push(upper);
        }
        result.extend(chars);
    }
    result
}

// ── Utilities ──────────────────────────────────────────────────────

/// Sanitize a filename into a valid JavaScript identifier.
///
/// Rules:
/// - Strip file extension (`.vue`, `.setup.vue`)
/// - Convert kebab-case and dot-separated to PascalCase
/// - Strip non-alphanumeric characters
/// - Prefix with `_` if starts with a digit
/// - Fallback to `"Component"` if empty
pub fn sanitize_js_identifier(filename: &str) -> String {
    // Extract basename (strip directory separators)
    let basename = filename.rsplit(['/', '\\']).next().unwrap_or(filename);

    // Strip extensions: .setup.vue, .vue, or just last extension
    let stem = basename
        .strip_suffix(".setup.vue")
        .or_else(|| basename.strip_suffix(".vue"))
        .or_else(|| basename.rsplit_once('.').map(|(stem, _)| stem))
        .unwrap_or(basename);

    if stem.is_empty() {
        return "Component".to_string();
    }

    // Convert to PascalCase: split on non-alphanumeric, capitalize each segment
    let mut result = String::with_capacity(stem.len());
    let mut capitalize_next = true;

    for ch in stem.chars() {
        if ch.is_alphanumeric() {
            if capitalize_next {
                for upper in ch.to_uppercase() {
                    result.push(upper);
                }
                capitalize_next = false;
            } else {
                result.push(ch);
            }
        } else {
            // Non-alphanumeric characters act as separators
            capitalize_next = true;
        }
    }

    if result.is_empty() {
        return "Component".to_string();
    }

    // Prefix with `_` if starts with a digit
    if result.as_bytes()[0].is_ascii_digit() {
        result.insert(0, '_');
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sanitize_basic() {
        assert_eq!(sanitize_js_identifier("my-component.vue"), "MyComponent");
    }

    #[test]
    fn sanitize_numeric_start() {
        assert_eq!(sanitize_js_identifier("123-widget.vue"), "_123Widget");
    }

    #[test]
    fn sanitize_index() {
        assert_eq!(sanitize_js_identifier("index.vue"), "Index");
    }

    #[test]
    fn sanitize_setup_extension() {
        assert_eq!(sanitize_js_identifier("my-comp.setup.vue"), "MyComp");
    }

    #[test]
    fn sanitize_special_chars() {
        assert_eq!(sanitize_js_identifier("special@chars.vue"), "SpecialChars");
    }

    #[test]
    fn sanitize_empty_stem() {
        assert_eq!(sanitize_js_identifier(".vue"), "Component");
    }

    #[test]
    fn sanitize_no_extension() {
        assert_eq!(sanitize_js_identifier("App"), "App");
    }

    #[test]
    fn sanitize_with_path() {
        assert_eq!(
            sanitize_js_identifier("src/components/my-button.vue"),
            "MyButton"
        );
    }

    #[test]
    fn sanitize_dot_separated() {
        assert_eq!(sanitize_js_identifier("my.comp.vue"), "MyComp");
    }

    #[test]
    fn sanitize_already_pascal() {
        assert_eq!(sanitize_js_identifier("MyComponent.vue"), "MyComponent");
    }

    #[test]
    fn sanitize_all_special_chars() {
        assert_eq!(sanitize_js_identifier("----.vue"), "Component");
    }

    // ── replace_word_boundary tests ──────────────────────────────

    #[test]
    fn word_boundary_simple_replace() {
        assert_eq!(
            replace_word_boundary("T extends T", "T", "X"),
            "X extends X"
        );
    }

    #[test]
    fn word_boundary_no_replace_in_prefix() {
        assert_eq!(
            replace_word_boundary("T extends T | TFoo", "T", "X"),
            "X extends X | TFoo"
        );
    }

    #[test]
    fn word_boundary_no_replace_in_suffix() {
        assert_eq!(
            replace_word_boundary("FooT extends T", "T", "X"),
            "FooT extends X"
        );
    }

    #[test]
    fn word_boundary_underscore_is_ident() {
        assert_eq!(
            replace_word_boundary("_T extends T", "T", "X"),
            "_T extends X"
        );
    }

    #[test]
    fn word_boundary_generic_angle_brackets() {
        assert_eq!(
            replace_word_boundary("Array<T>", "T", "__X__"),
            "Array<__X__>"
        );
    }

    #[test]
    fn word_boundary_empty_needle() {
        assert_eq!(replace_word_boundary("hello", "", "X"), "hello");
    }

    #[test]
    fn word_boundary_empty_haystack() {
        assert_eq!(replace_word_boundary("", "T", "X"), "");
    }

    #[test]
    fn word_boundary_multiple_occurrences() {
        assert_eq!(replace_word_boundary("T | T & T", "T", "Y"), "Y | Y & Y");
    }

    // ── IdeGenericInfo tests ─────────────────────────────────────

    #[test]
    fn generic_info_simple_param() {
        let info = IdeGenericInfo::from_source("T").unwrap();
        assert_eq!(info.names, vec!["T"]);
        assert_eq!(info.sanitised_names, vec!["__VERTER__TS__T"]);
        assert_eq!(info.declaration, "__VERTER__TS__T = any");
    }

    #[test]
    fn generic_info_constraint() {
        let info = IdeGenericInfo::from_source("T extends string").unwrap();
        assert_eq!(info.names, vec!["T"]);
        assert_eq!(info.declaration, "__VERTER__TS__T extends string = any");
    }

    #[test]
    fn generic_info_constraint_and_default() {
        let info = IdeGenericInfo::from_source("T extends object = {}").unwrap();
        assert_eq!(info.declaration, "__VERTER__TS__T extends object = {}");
    }

    #[test]
    fn generic_info_cross_reference_sanitisation() {
        let info = IdeGenericInfo::from_source("T, U extends Array<T>").unwrap();
        assert_eq!(info.names, vec!["T", "U"]);
        assert_eq!(
            info.declaration,
            "__VERTER__TS__T = any, __VERTER__TS__U extends Array<__VERTER__TS__T> = any"
        );
    }

    #[test]
    fn generic_info_multiple_mixed() {
        let info = IdeGenericInfo::from_source("K extends string, V").unwrap();
        assert_eq!(info.names, vec!["K", "V"]);
        assert_eq!(
            info.declaration,
            "__VERTER__TS__K extends string = any, __VERTER__TS__V = any"
        );
    }

    #[test]
    fn generic_info_default_type() {
        let info = IdeGenericInfo::from_source("T = string").unwrap();
        assert_eq!(info.declaration, "__VERTER__TS__T = string");
    }

    #[test]
    fn generic_info_empty_returns_none() {
        assert!(IdeGenericInfo::from_source("").is_none());
        assert!(IdeGenericInfo::from_source("  ").is_none());
    }

    #[test]
    fn generic_info_brackets() {
        let info = IdeGenericInfo::from_source("T extends string").unwrap();
        assert_eq!(info.source_bracket(), "<T extends string>");
        assert_eq!(info.names_bracket(), "<T>");
        assert_eq!(info.sanitised_names_bracket(), "<__VERTER__TS__T>");
        assert_eq!(
            info.declaration_bracket(),
            "<__VERTER__TS__T extends string = any>"
        );
    }

    #[test]
    fn generic_info_keyof_cross_ref() {
        let info = IdeGenericInfo::from_source("T extends object, K extends keyof T").unwrap();
        assert_eq!(
            info.declaration,
            "__VERTER__TS__T extends object = any, __VERTER__TS__K extends keyof __VERTER__TS__T = any"
        );
    }
}