macroforge_ts_quote 0.1.63

Quote macro for generating TypeScript code at compile time
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
//! Rust-style templating for TypeScript code generation (AST-based)
//!
//! Provides a template syntax with interpolation and control flow:
//! - `@{expr}` - Interpolate expressions (calls `.to_string()`)
//! - ` content ` - Ident block: concatenates content without spaces (e.g., `get@{name}` → `getUser`)
//! - `{> "comment" <}` - Line comment: outputs `// comment` (string preserves whitespace)
//! - `{>> "comment" <<}` - Block comment: outputs `/* comment */` (string preserves whitespace)
//! - `///` or `/** */` - Rust doc comments in the template emit JSDoc blocks (`/** ... */`)
//! - `@@{` - Escape for literal `@{` (e.g., `"@@{foo}"` → `@{foo}`)
//! - `"string @{expr}"` - String interpolation (auto-detected)
//! - `"'^template ${expr}^'"` - JS backtick template literal (outputs `` `template ${expr}` ``)
//! - `{#if cond}...{/if}` - Conditional blocks
//! - `{#if let pattern = expr}...{/if}` - Pattern matching if-let blocks
//! - `{:else}` - Else clause
//! - `{:else if cond}` - Else-if clause
//! - `{#match expr}{:case pattern}...{/match}` - Match blocks with case arms
//! - `{#for item in list}...{/for}` - Iteration
//! - `{#while cond}...{/while}` - While loop
//! - `{#while let pattern = expr}...{/while}` - While-let pattern matching loop
//! - `{$let name = expr}` - Local constants
//! - `{$let mut name = expr}` - Mutable local binding
//! - `{$do expr}` - Execute side-effectful expression (discard result)
//! - `{$typescript stream}` - Inject a TsStream, preserving its source and runtime_patches (imports)
//!
//! Note: A single `@` not followed by `{` passes through unchanged (e.g., `email@domain.com`).

use proc_macro2::TokenStream as TokenStream2;
use quote::quote;

use crate::compiler::compile_template;

/// Converts Rust doc attributes back to JSDoc comments.
///
/// When `/** Doc */` goes through Rust's TokenStream, it becomes `# [doc = r" Doc "]`.
/// This function converts them back to `/** Doc */` for valid TypeScript.
pub(crate) fn convert_doc_attributes_to_jsdoc(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let chars: Vec<char> = input.chars().collect();
    let len = chars.len();
    let mut i = 0;

    #[cfg(debug_assertions)]
    let debug = std::env::var("MF_DEBUG_TEMPLATE").is_ok();

    while i < len {
        // Look for pattern: # [doc = ...]
        if i + 7 < len && chars[i] == '#' {
            #[cfg(debug_assertions)]
            if debug {
                let context: String = chars[i..std::cmp::min(i + 20, len)].iter().collect();
                eprintln!(
                    "[MF_DEBUG_DOC] Found # at pos {}, context: {:?}",
                    i, context
                );
            }
            // Skip whitespace after #
            let mut j = i + 1;
            while j < len && chars[j].is_whitespace() {
                j += 1;
            }

            // Check for [
            if j < len && chars[j] == '[' {
                j += 1;
                // Skip whitespace
                while j < len && chars[j].is_whitespace() {
                    j += 1;
                }

                // Check for "doc"
                if j + 3 <= len && chars[j] == 'd' && chars[j + 1] == 'o' && chars[j + 2] == 'c' {
                    j += 3;
                    // Skip whitespace
                    while j < len && chars[j].is_whitespace() {
                        j += 1;
                    }

                    // Check for =
                    if j < len && chars[j] == '=' {
                        j += 1;
                        // Skip whitespace
                        while j < len && chars[j].is_whitespace() {
                            j += 1;
                        }

                        // Check for optional r prefix
                        if j < len && chars[j] == 'r' {
                            j += 1;
                        }

                        // Check for opening "
                        if j < len && chars[j] == '"' {
                            j += 1;
                            let doc_start = j;

                            // Find closing "
                            while j < len && chars[j] != '"' {
                                j += 1;
                            }
                            let doc_end = j;

                            if j < len && chars[j] == '"' {
                                j += 1;
                                // Skip whitespace
                                while j < len && chars[j].is_whitespace() {
                                    j += 1;
                                }

                                // Check for ]
                                if j < len && chars[j] == ']' {
                                    j += 1;

                                    // Extract and trim doc text
                                    let doc_text: String =
                                        chars[doc_start..doc_end].iter().collect();
                                    let mut doc_text = doc_text.trim().to_string();
                                    if let Some(stripped) = doc_text.strip_prefix('*') {
                                        doc_text = stripped.trim_start().to_string();
                                    }
                                    if let Some(stripped) = doc_text.strip_suffix("*/") {
                                        doc_text = stripped.trim_end().to_string();
                                    }

                                    // Output JSDoc comment
                                    result.push_str("/** ");
                                    result.push_str(&doc_text);
                                    result.push_str(" */");

                                    i = j;
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
        }

        result.push(chars[i]);
        i += 1;
    }

    result
}

/// Normalizes template spacing that Rust's tokenizer introduces.
///
/// Rust's proc_macro tokenizer adds spaces around punctuation, so:
/// - `@{expr}` becomes `@ { expr }`
/// - `{#if cond}` becomes `{ # if cond }`
/// - `{/if}` becomes `{ / if }`
/// - `{:else}` becomes `{ : else }`
/// - `{$let x = 1}` becomes `{ $ let x = 1 }`
/// - `ident` becomes `{ | ident | }`
/// - `===` becomes `= = =` (JavaScript strict equality)
/// - `!==` becomes `! = =` (JavaScript strict inequality)
///
/// This function normalizes these back to the expected format.
pub(crate) fn normalize_template_spacing(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let chars: Vec<char> = input.chars().collect();
    let len = chars.len();
    let mut i = 0;

    while i < len {
        let c = chars[i];

        // Handle JavaScript operators that get space-separated by Rust tokenizer
        // Check for `= = =` → `===` (strict equality)
        if c == '='
            && i + 4 < len
            && chars[i + 1] == ' '
            && chars[i + 2] == '='
            && chars[i + 3] == ' '
            && chars[i + 4] == '='
        {
            result.push_str("===");
            i += 5;
            continue;
        }

        // Check for `! = =` → `!==` (strict inequality)
        // Note: Rust tokenizes `!==` as `!=` (Ne) followed by `=`, so it becomes `! = =` with spaces
        if c == '!'
            && i + 4 < len
            && chars[i + 1] == ' '
            && chars[i + 2] == '='
            && chars[i + 3] == ' '
            && chars[i + 4] == '='
        {
            result.push_str("!==");
            i += 5;
            continue;
        }

        // Check for `! =` → `!=` (Rust Ne token becomes space-separated)
        if c == '!' && i + 2 < len && chars[i + 1] == ' ' && chars[i + 2] == '=' {
            // But first check it's not part of `! = =` (already handled above)
            if !(i + 4 < len && chars[i + 3] == ' ' && chars[i + 4] == '=') {
                result.push_str("!=");
                i += 3;
                continue;
            }
        }

        // Check for `= =` → `==` (equality, but not if part of `= = =`)
        if c == '=' && i + 2 < len && chars[i + 1] == ' ' && chars[i + 2] == '=' {
            // Check if this is part of `= = =`
            if i + 4 < len && chars[i + 3] == ' ' && chars[i + 4] == '=' {
                // Part of ===, will be handled by the === check
            } else {
                result.push_str("==");
                i += 3;
                continue;
            }
        }

        // Check for `& &` → `&&` (logical AND)
        if c == '&' && i + 2 < len && chars[i + 1] == ' ' && chars[i + 2] == '&' {
            result.push_str("&&");
            i += 3;
            continue;
        }

        // Check for `| |` → `||` (logical OR)
        if c == '|' && i + 2 < len && chars[i + 1] == ' ' && chars[i + 2] == '|' {
            result.push_str("||");
            i += 3;
            continue;
        }

        // Check for `: :` → `::` (TypeScript namespace/module access)
        if c == ':' && i + 2 < len && chars[i + 1] == ' ' && chars[i + 2] == ':' {
            result.push_str("::");
            i += 3;
            continue;
        }

        // Check for `= >` → `=>` (arrow function)
        if c == '=' && i + 2 < len && chars[i + 1] == ' ' && chars[i + 2] == '>' {
            result.push_str("=>");
            i += 3;
            continue;
        }

        // Check for `< =` → `<=` (less than or equal)
        if c == '<' && i + 2 < len && chars[i + 1] == ' ' && chars[i + 2] == '=' {
            result.push_str("<=");
            i += 3;
            continue;
        }

        // Check for `> =` → `>=` (greater than or equal)
        if c == '>' && i + 2 < len && chars[i + 1] == ' ' && chars[i + 2] == '=' {
            result.push_str(">=");
            i += 3;
            continue;
        }

        // Check for `+ =` → `+=`
        if c == '+' && i + 2 < len && chars[i + 1] == ' ' && chars[i + 2] == '=' {
            result.push_str("+=");
            i += 3;
            continue;
        }

        // Check for `- =` → `-=`
        if c == '-' && i + 2 < len && chars[i + 1] == ' ' && chars[i + 2] == '=' {
            result.push_str("-=");
            i += 3;
            continue;
        }

        // Check for `- >` → `->` (not common in TS but used in comments/generics)
        if c == '-' && i + 2 < len && chars[i + 1] == ' ' && chars[i + 2] == '>' {
            result.push_str("->");
            i += 3;
            continue;
        }

        // Check for `. . .` → `...` (spread operator)
        if c == '.'
            && i + 4 < len
            && chars[i + 1] == ' '
            && chars[i + 2] == '.'
            && chars[i + 3] == ' '
            && chars[i + 4] == '.'
        {
            result.push_str("...");
            i += 5;
            continue;
        }

        // Restore missing '/' for doc comments that were tokenized without it.
        if c == '*' && i + 1 < len && chars[i + 1] == '*' {
            let mut j = i;
            while j > 0 {
                let prev = chars[j - 1];
                if prev == '\n' || prev == '\r' {
                    break;
                }
                if prev.is_whitespace() {
                    j -= 1;
                    continue;
                }
                break;
            }
            if j == 0 || chars[j.saturating_sub(1)] == '\n' || chars[j.saturating_sub(1)] == '\r' {
                result.push_str("/**");
                i += 2;
                continue;
            }
        }

        // Handle @ followed by optional whitespace then { or @
        if c == '@' {
            // Skip whitespace after @
            let mut peek = i + 1;
            while peek < len && chars[peek].is_whitespace() {
                peek += 1;
            }

            // Check if this is @{ or @@ (interpolation or escape)
            if peek < len && (chars[peek] == '{' || chars[peek] == '@') {
                result.push('@');
                i = peek; // Skip to the { or @
                continue;
            }

            result.push('@');
            i += 1;
        }
        // Handle { followed by optional whitespace then #, /, :, $, or |
        else if c == '{' {
            let start = i;
            i += 1;
            // Skip whitespace after {
            while i < len && chars[i].is_whitespace() {
                i += 1;
            }
            // Check if this is a control flow or ident block
            if i < len
                && (chars[i] == '#'
                    || chars[i] == '/'
                    || chars[i] == ':'
                    || chars[i] == '$'
                    || chars[i] == '|')
            {
                result.push('{');
                // The next char will be handled in the next iteration
                continue;
            } else {
                // Not a special construct, output as-is
                result.push('{');
                // Re-add the whitespace we skipped
                for &ch in chars.iter().take(i).skip(start + 1) {
                    result.push(ch);
                }
                continue;
            }
        }
        // Handle | followed by optional whitespace then }
        else if c == '|' {
            result.push('|');
            i += 1;
            // Check if followed by whitespace then }
            let ws_start = i;
            while i < len && chars[i].is_whitespace() {
                i += 1;
            }
            if i < len && chars[i] == '}' {
                // Don't add the whitespace, just continue
                continue;
            } else {
                // Re-add the whitespace we skipped
                for &ch in chars.iter().take(i).skip(ws_start) {
                    result.push(ch);
                }
                continue;
            }
        } else {
            result.push(c);
            i += 1;
        }
    }

    result
}

pub(crate) fn collapse_template_newlines(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let chars: Vec<char> = input.chars().collect();
    let len = chars.len();
    let mut i = 0;

    let mut in_string = false;
    let mut string_char = '\0';
    let mut escape_next = false;
    let mut in_line_comment = false;
    let mut in_block_comment = false;

    while i < len {
        let ch = chars[i];

        if in_line_comment {
            result.push(ch);
            if ch == '\n' {
                in_line_comment = false;
            }
            i += 1;
            continue;
        }

        if in_block_comment {
            result.push(ch);
            if ch == '*' && i + 1 < len && chars[i + 1] == '/' {
                result.push('/');
                i += 2;
                in_block_comment = false;
                continue;
            }
            i += 1;
            continue;
        }

        if in_string {
            result.push(ch);
            if escape_next {
                escape_next = false;
            } else if ch == '\\' {
                escape_next = true;
            } else if ch == string_char {
                in_string = false;
            }
            i += 1;
            continue;
        }

        if ch == '/' && i + 1 < len {
            if chars[i + 1] == '/' {
                in_line_comment = true;
                result.push(ch);
                result.push('/');
                i += 2;
                continue;
            }
            if chars[i + 1] == '*' {
                in_block_comment = true;
                result.push(ch);
                result.push('*');
                i += 2;
                continue;
            }
        }

        if ch == '\n' || ch == '\r' {
            if !result.ends_with(' ') {
                result.push(' ');
            }
            i += 1;
            continue;
        }

        if ch == '"' || ch == '\'' || ch == '`' {
            in_string = true;
            string_char = ch;
        }

        result.push(ch);
        i += 1;
    }

    result
}

/// Parses a template token stream into Rust that builds TypeScript AST output.
///
/// This function takes the raw token stream from the macro invocation,
/// converts it to a template string, and compiles it using the Rowan-based
/// compiler.
///
/// # Example
///
/// ```ignore
/// ts_template! {
///     const x = @{expr};
///     {#for item in items}
///         console.log(@{item});
///     {/for}
/// }
/// ```
pub fn parse_template(input: TokenStream2) -> syn::Result<TokenStream2> {
    // Convert the token stream to a template string
    // The Rowan parser will handle @{expr} interpolations and control flow
    let template_str = input.to_string();

    // Debug: print the raw tokenized string (only in debug builds during tests)
    #[cfg(debug_assertions)]
    if std::env::var("MF_DEBUG_TEMPLATE").is_ok() {
        eprintln!(
            "[MF_DEBUG] Raw tokenized ({} chars): {:?}",
            template_str.len(),
            template_str
        );
    }

    // Convert doc attributes back to JSDoc comments
    // When `/** Doc */` goes through TokenStream, it becomes `# [doc = r" Doc "]`
    let template_str = convert_doc_attributes_to_jsdoc(&template_str);

    #[cfg(debug_assertions)]
    if std::env::var("MF_DEBUG_TEMPLATE").is_ok() {
        eprintln!(
            "[MF_DEBUG] After doc conversion ({} chars): {:?}",
            template_str.len(),
            template_str
        );
    }

    // Normalize spacing: Rust tokenizer adds spaces around punctuation,
    // so "@ { expr }" needs to become "@{expr}" for the lexer to recognize it.
    // Also normalize control flow tags: "{ # if" -> "{#if", "{ / if" -> "{/if", etc.
    let template_str = normalize_template_spacing(&template_str);
    let template_str = collapse_template_newlines(&template_str);

    #[cfg(debug_assertions)]
    if std::env::var("MF_DEBUG_TEMPLATE").is_ok() {
        eprintln!(
            "[MF_DEBUG] After normalization ({} chars): {:?}",
            template_str.len(),
            template_str
        );
    }

    // Compile using the Rowan-based compiler
    let stmts_builder = compile_template(&template_str, "__stmts")?;

    // Wrap in the expected output structure
    Ok(quote! {
        {
            let mut __stmts: Vec<macroforge_ts::swc_core::ecma::ast::ModuleItem> = Vec::new();
            let mut __patches: Vec<macroforge_ts::ts_syn::abi::Patch> = Vec::new();
            let __comments = macroforge_ts::swc_core::common::comments::SingleThreadedComments::default();
            let mut __pending_comments: Vec<macroforge_ts::swc_core::common::comments::Comment> = Vec::new();
            // Collect injected TsStreams for merging at output
            let mut __injected_streams: Vec<macroforge_ts::ts_syn::TsStream> = Vec::new();
            let __mf_items: Vec<macroforge_ts::swc_core::ecma::ast::ModuleItem> = #stmts_builder;
            __stmts.extend(__mf_items);
            (__stmts, __patches, __comments, __injected_streams)
        }
    })
}

pub(crate) fn strip_doc_comments(input: &str) -> String {
    let bytes = input.as_bytes();
    let mut result = String::with_capacity(input.len());
    let mut i = 0;

    while i < bytes.len() {
        if bytes[i..].starts_with(b"/**")
            && let Some(end) = input[i + 3..].find("*/")
        {
            i += 3 + end + 2;
            continue;
        }

        if bytes[i..].starts_with(b"///") {
            if let Some(end) = input[i + 3..].find('\n') {
                i += 3 + end + 1;
                continue;
            } else {
                break;
            }
        }

        result.push(bytes[i] as char);
        i += 1;
    }

    result
}

pub(crate) fn parse_template_str(template_str: &str) -> syn::Result<TokenStream2> {
    let stmts_builder = compile_template(template_str, "__stmts")?;

    Ok(quote! {
        {
            let mut __stmts: Vec<macroforge_ts::swc_core::ecma::ast::ModuleItem> = Vec::new();
            let mut __patches: Vec<macroforge_ts::ts_syn::abi::Patch> = Vec::new();
            let __comments = macroforge_ts::swc_core::common::comments::SingleThreadedComments::default();
            let mut __pending_comments: Vec<macroforge_ts::swc_core::common::comments::Comment> = Vec::new();
            // Collect injected TsStreams for merging at output
            let mut __injected_streams: Vec<macroforge_ts::ts_syn::TsStream> = Vec::new();
            let __mf_items: Vec<macroforge_ts::swc_core::ecma::ast::ModuleItem> = #stmts_builder;
            __stmts.extend(__mf_items);
            (__stmts, __patches, __comments, __injected_streams)
        }
    })
}